mirror of
https://github.com/GothenburgBitFactory/taskwarrior.git
synced 2025-06-26 10:54:26 +02:00
return TCResult from tc_uuid_from_str
This commit is contained in:
parent
28a4599a6a
commit
c9c72b4fd3
4 changed files with 15 additions and 14 deletions
|
@ -79,7 +79,7 @@ static void test_replica_task_import(void) {
|
||||||
TEST_ASSERT_NULL(tc_replica_error(rep));
|
TEST_ASSERT_NULL(tc_replica_error(rep));
|
||||||
|
|
||||||
TCUuid uuid;
|
TCUuid uuid;
|
||||||
TEST_ASSERT_TRUE(tc_uuid_from_str(tc_string_borrow("23cb25e0-5d1a-4932-8131-594ac6d3a843"), &uuid));
|
TEST_ASSERT_EQUAL(TC_RESULT_OK, tc_uuid_from_str(tc_string_borrow("23cb25e0-5d1a-4932-8131-594ac6d3a843"), &uuid));
|
||||||
TCTask *task = tc_replica_import_task_with_uuid(rep, uuid);
|
TCTask *task = tc_replica_import_task_with_uuid(rep, uuid);
|
||||||
TEST_ASSERT_NOT_NULL(task);
|
TEST_ASSERT_NOT_NULL(task);
|
||||||
|
|
||||||
|
@ -110,7 +110,7 @@ static void test_replica_get_task_not_found(void) {
|
||||||
TEST_ASSERT_NULL(tc_replica_error(rep));
|
TEST_ASSERT_NULL(tc_replica_error(rep));
|
||||||
|
|
||||||
TCUuid uuid;
|
TCUuid uuid;
|
||||||
TEST_ASSERT_TRUE(tc_uuid_from_str(tc_string_borrow("23cb25e0-5d1a-4932-8131-594ac6d3a843"), &uuid));
|
TEST_ASSERT_EQUAL(TC_RESULT_OK, tc_uuid_from_str(tc_string_borrow("23cb25e0-5d1a-4932-8131-594ac6d3a843"), &uuid));
|
||||||
TCTask *task = tc_replica_get_task(rep, uuid);
|
TCTask *task = tc_replica_get_task(rep, uuid);
|
||||||
TEST_ASSERT_NULL(task);
|
TEST_ASSERT_NULL(task);
|
||||||
TEST_ASSERT_NULL(tc_replica_error(rep));
|
TEST_ASSERT_NULL(tc_replica_error(rep));
|
||||||
|
|
|
@ -33,7 +33,7 @@ static void test_uuid_to_str(void) {
|
||||||
static void test_uuid_valid_from_str(void) {
|
static void test_uuid_valid_from_str(void) {
|
||||||
TCUuid u;
|
TCUuid u;
|
||||||
char *ustr = "23cb25e0-5d1a-4932-8131-594ac6d3a843";
|
char *ustr = "23cb25e0-5d1a-4932-8131-594ac6d3a843";
|
||||||
TEST_ASSERT_TRUE(tc_uuid_from_str(tc_string_borrow(ustr), &u));
|
TEST_ASSERT_EQUAL(TC_RESULT_OK, tc_uuid_from_str(tc_string_borrow(ustr), &u));
|
||||||
TEST_ASSERT_EQUAL(0x23, u.bytes[0]);
|
TEST_ASSERT_EQUAL(0x23, u.bytes[0]);
|
||||||
TEST_ASSERT_EQUAL(0x43, u.bytes[15]);
|
TEST_ASSERT_EQUAL(0x43, u.bytes[15]);
|
||||||
}
|
}
|
||||||
|
@ -42,20 +42,20 @@ static void test_uuid_valid_from_str(void) {
|
||||||
static void test_uuid_invalid_string_fails(void) {
|
static void test_uuid_invalid_string_fails(void) {
|
||||||
TCUuid u;
|
TCUuid u;
|
||||||
char *ustr = "not-a-valid-uuid";
|
char *ustr = "not-a-valid-uuid";
|
||||||
TEST_ASSERT_FALSE(tc_uuid_from_str(tc_string_borrow(ustr), &u));
|
TEST_ASSERT_EQUAL(TC_RESULT_ERROR, tc_uuid_from_str(tc_string_borrow(ustr), &u));
|
||||||
}
|
}
|
||||||
|
|
||||||
// converting invalid UTF-8 UUIDs from string fails as expected
|
// converting invalid UTF-8 UUIDs from string fails as expected
|
||||||
static void test_uuid_bad_utf8(void) {
|
static void test_uuid_bad_utf8(void) {
|
||||||
TCUuid u;
|
TCUuid u;
|
||||||
char *ustr = "\xf0\x28\x8c\xbc";
|
char *ustr = "\xf0\x28\x8c\xbc";
|
||||||
TEST_ASSERT_FALSE(tc_uuid_from_str(tc_string_borrow(ustr), &u));
|
TEST_ASSERT_EQUAL(TC_RESULT_ERROR, tc_uuid_from_str(tc_string_borrow(ustr), &u));
|
||||||
}
|
}
|
||||||
|
|
||||||
// converting a string with embedded NUL fails as expected
|
// converting a string with embedded NUL fails as expected
|
||||||
static void test_uuid_embedded_nul(void) {
|
static void test_uuid_embedded_nul(void) {
|
||||||
TCUuid u;
|
TCUuid u;
|
||||||
TEST_ASSERT_FALSE(tc_uuid_from_str(tc_string_clone_with_len("ab\0de", 5), &u));
|
TEST_ASSERT_EQUAL(TC_RESULT_ERROR, tc_uuid_from_str(tc_string_clone_with_len("ab\0de", 5), &u));
|
||||||
}
|
}
|
||||||
|
|
||||||
int uuid_tests(void) {
|
int uuid_tests(void) {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use crate::string::TCString;
|
|
||||||
use crate::traits::*;
|
use crate::traits::*;
|
||||||
|
use crate::{result::TCResult, string::TCString};
|
||||||
use libc;
|
use libc;
|
||||||
use taskchampion::Uuid;
|
use taskchampion::Uuid;
|
||||||
|
|
||||||
|
@ -72,10 +72,10 @@ pub unsafe extern "C" fn tc_uuid_to_str(tcuuid: TCUuid) -> *mut TCString<'static
|
||||||
unsafe { TCString::from(s).return_val() }
|
unsafe { TCString::from(s).return_val() }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parse the given string as a UUID. Returns false on failure.
|
/// Parse the given string as a UUID. Returns TC_RESULT_ERROR on parse failure or if the given
|
||||||
|
/// string is not valid.
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub unsafe extern "C" fn tc_uuid_from_str<'a>(s: *mut TCString, uuid_out: *mut TCUuid) -> bool {
|
pub unsafe extern "C" fn tc_uuid_from_str<'a>(s: *mut TCString, uuid_out: *mut TCUuid) -> TCResult {
|
||||||
// TODO: TCResult instead
|
|
||||||
debug_assert!(!s.is_null());
|
debug_assert!(!s.is_null());
|
||||||
debug_assert!(!uuid_out.is_null());
|
debug_assert!(!uuid_out.is_null());
|
||||||
// SAFETY: see TCString docstring
|
// SAFETY: see TCString docstring
|
||||||
|
@ -86,8 +86,8 @@ pub unsafe extern "C" fn tc_uuid_from_str<'a>(s: *mut TCString, uuid_out: *mut T
|
||||||
// - uuid_out is not NULL (promised by caller)
|
// - uuid_out is not NULL (promised by caller)
|
||||||
// - alignment is not required
|
// - alignment is not required
|
||||||
unsafe { TCUuid::to_arg_out(u, uuid_out) };
|
unsafe { TCUuid::to_arg_out(u, uuid_out) };
|
||||||
return true;
|
return TCResult::Ok;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
false
|
TCResult::Error
|
||||||
}
|
}
|
||||||
|
|
|
@ -456,9 +456,10 @@ void tc_uuid_to_buf(struct TCUuid tcuuid, char *buf);
|
||||||
struct TCString *tc_uuid_to_str(struct TCUuid tcuuid);
|
struct TCString *tc_uuid_to_str(struct TCUuid tcuuid);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse the given string as a UUID. Returns false on failure.
|
* Parse the given string as a UUID. Returns TC_RESULT_ERROR on parse failure or if the given
|
||||||
|
* string is not valid.
|
||||||
*/
|
*/
|
||||||
bool tc_uuid_from_str(struct TCString *s, struct TCUuid *uuid_out);
|
TCResult tc_uuid_from_str(struct TCString *s, struct TCUuid *uuid_out);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
} // extern "C"
|
} // extern "C"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue