add some UUID support

This commit is contained in:
Dustin J. Mitchell 2022-01-23 19:45:39 +00:00
parent e590dc7c98
commit 46e08bc040
8 changed files with 136 additions and 1 deletions

1
Cargo.lock generated
View file

@ -3035,6 +3035,7 @@ dependencies = [
"cbindgen", "cbindgen",
"libc", "libc",
"taskchampion", "taskchampion",
"uuid",
] ]
[[package]] [[package]]

View file

@ -3,7 +3,7 @@ INC=-I ../lib
LIB=-L ../target/debug LIB=-L ../target/debug
RPATH=-Wl,-rpath,../target/debug RPATH=-Wl,-rpath,../target/debug
TESTS = replica.cpp TESTS = replica.cpp uuid.cpp
.PHONY: all test .PHONY: all test

38
binding-tests/uuid.cpp Normal file
View file

@ -0,0 +1,38 @@
#include <string.h>
#include "doctest.h"
#include "taskchampion.h"
TEST_CASE("creating UUIDs does not crash") {
Uuid u1 = tc_uuid_new_v4();
Uuid u2 = tc_uuid_nil();
}
TEST_CASE("converting UUIDs to string works") {
Uuid u2 = tc_uuid_nil();
REQUIRE(TC_UUID_STRING_BYTES == 36);
char u2str[TC_UUID_STRING_BYTES];
tc_uuid_to_str(u2, u2str);
CHECK(strncmp(u2str, "00000000-0000-0000-0000-000000000000", TC_UUID_STRING_BYTES) == 0);
}
TEST_CASE("converting UUIDs from string works") {
Uuid u;
char ustr[TC_UUID_STRING_BYTES] = "fdc314b7-f938-4845-b8d1-95716e4eb762";
CHECK(tc_uuid_from_str(ustr, &u));
CHECK(u._0[0] == 0xfd);
// .. if these two are correct, probably it worked :)
CHECK(u._0[15] == 0x62);
}
TEST_CASE("converting invalid UUIDs from string fails as expected") {
Uuid u;
char ustr[TC_UUID_STRING_BYTES] = "not-a-valid-uuid";
CHECK(!tc_uuid_from_str(ustr, &u));
}
TEST_CASE("converting invalid UTF-8 UUIDs from string fails as expected") {
Uuid u;
char ustr[TC_UUID_STRING_BYTES] = "\xf0\x28\x8c\xbc";
CHECK(!tc_uuid_from_str(ustr, &u));
}

View file

@ -11,6 +11,7 @@ crate-type = ["cdylib"]
[dependencies] [dependencies]
libc = "0.2.113" libc = "0.2.113"
taskchampion = { path = "../taskchampion" } taskchampion = { path = "../taskchampion" }
uuid = { version = "^0.8.2", features = ["serde", "v4"] }
anyhow = "1.0" anyhow = "1.0"
[build-dependencies] [build-dependencies]

View file

@ -10,6 +10,16 @@ fn main() {
.with_language(Language::C) .with_language(Language::C)
.with_config(Config { .with_config(Config {
cpp_compat: true, cpp_compat: true,
export: ExportConfig {
item_types: vec![
ItemType::Structs,
ItemType::Globals,
ItemType::Functions,
ItemType::Constants,
ItemType::OpaqueItems,
],
..Default::default()
},
..Default::default() ..Default::default()
}) })
.generate() .generate()

View file

@ -1 +1,2 @@
pub mod replica; pub mod replica;
pub mod uuid;

63
lib/src/uuid.rs Normal file
View file

@ -0,0 +1,63 @@
use libc;
use taskchampion::Uuid as TcUuid;
/// Uuid is used as a task identifier. Uuids do not contain any pointers and need not be freed.
#[repr(C)]
pub struct Uuid([u8; 16]);
impl From<TcUuid> for Uuid {
fn from(uuid: TcUuid) -> Uuid {
// TODO: can we avoid clone here?
Uuid(uuid.as_bytes().clone())
}
}
impl From<Uuid> for TcUuid {
fn from(uuid: Uuid) -> TcUuid {
TcUuid::from_bytes(uuid.0)
}
}
/// Create a new, randomly-generated UUID.
#[no_mangle]
pub extern "C" fn tc_uuid_new_v4() -> Uuid {
TcUuid::new_v4().into()
}
/// Create a new UUID with the nil value.
#[no_mangle]
pub extern "C" fn tc_uuid_nil() -> Uuid {
TcUuid::nil().into()
}
/// Length, in bytes, of a C string containing a Uuid.
#[no_mangle]
pub static TC_UUID_STRING_BYTES: usize = ::uuid::adapter::Hyphenated::LENGTH;
/// Write the string representation of a Uuid into the given buffer, which must be
/// at least TC_UUID_STRING_BYTES long. No NUL terminator is added.
#[no_mangle]
pub extern "C" fn tc_uuid_to_str<'a>(uuid: Uuid, out: *mut libc::c_char) {
debug_assert!(!out.is_null());
let buf: &'a mut [u8] = unsafe {
std::slice::from_raw_parts_mut(out as *mut u8, ::uuid::adapter::Hyphenated::LENGTH)
};
let uuid: TcUuid = uuid.into();
uuid.to_hyphenated().encode_lower(buf);
}
/// Parse the given value as a UUID. The value must be exactly TC_UUID_STRING_BYTES long. Returns
/// false on failure.
#[no_mangle]
pub extern "C" fn tc_uuid_from_str<'a>(val: *const libc::c_char, out: *mut Uuid) -> bool {
debug_assert!(!val.is_null());
debug_assert!(!out.is_null());
let slice = unsafe { std::slice::from_raw_parts(val as *const u8, TC_UUID_STRING_BYTES) };
if let Ok(s) = std::str::from_utf8(slice) {
if let Ok(u) = TcUuid::parse_str(s) {
unsafe { *out = u.into() };
return true;
}
}
false
}

View file

@ -8,8 +8,15 @@
/// for querying and modifying that data. /// for querying and modifying that data.
struct Replica; struct Replica;
/// Uuid is used as a task identifier. Uuids do not contain any pointers and need not be freed.
struct Uuid {
uint8_t _0[16];
};
extern "C" { extern "C" {
extern const uintptr_t TC_UUID_STRING_BYTES;
/// Create a new Replica. /// Create a new Replica.
/// ///
/// If path is NULL, then an in-memory replica is created. Otherwise, path is the path to the /// If path is NULL, then an in-memory replica is created. Otherwise, path is the path to the
@ -34,4 +41,18 @@ const char *tc_replica_error(Replica *rep);
/// Free a Replica. /// Free a Replica.
void tc_replica_free(Replica *rep); void tc_replica_free(Replica *rep);
/// Create a new, randomly-generated UUID.
Uuid tc_uuid_new_v4();
/// Create a new UUID with the nil value.
Uuid tc_uuid_nil();
/// Write the string representation of a Uuid into the given buffer, which must be
/// at least TC_UUID_STRING_BYTES long. No NUL terminator is added.
void tc_uuid_to_str(Uuid uuid, char *out);
/// Parse the given value as a UUID. The value must be exactly TC_UUID_STRING_BYTES long. Returns
/// false on failure.
bool tc_uuid_from_str(const char *val, Uuid *out);
} // extern "C" } // extern "C"