return TCResult from tc_uuid_from_str

This commit is contained in:
Dustin J. Mitchell 2022-02-09 23:43:23 +00:00
parent 28a4599a6a
commit c9c72b4fd3
4 changed files with 15 additions and 14 deletions

View file

@ -79,7 +79,7 @@ static void test_replica_task_import(void) {
TEST_ASSERT_NULL(tc_replica_error(rep));
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);
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));
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);
TEST_ASSERT_NULL(task);
TEST_ASSERT_NULL(tc_replica_error(rep));

View file

@ -33,7 +33,7 @@ static void test_uuid_to_str(void) {
static void test_uuid_valid_from_str(void) {
TCUuid u;
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(0x43, u.bytes[15]);
}
@ -42,20 +42,20 @@ static void test_uuid_valid_from_str(void) {
static void test_uuid_invalid_string_fails(void) {
TCUuid u;
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
static void test_uuid_bad_utf8(void) {
TCUuid u;
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
static void test_uuid_embedded_nul(void) {
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) {

View file

@ -1,5 +1,5 @@
use crate::string::TCString;
use crate::traits::*;
use crate::{result::TCResult, string::TCString};
use libc;
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() }
}
/// 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]
pub unsafe extern "C" fn tc_uuid_from_str<'a>(s: *mut TCString, uuid_out: *mut TCUuid) -> bool {
// TODO: TCResult instead
pub unsafe extern "C" fn tc_uuid_from_str<'a>(s: *mut TCString, uuid_out: *mut TCUuid) -> TCResult {
debug_assert!(!s.is_null());
debug_assert!(!uuid_out.is_null());
// 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)
// - alignment is not required
unsafe { TCUuid::to_arg_out(u, uuid_out) };
return true;
return TCResult::Ok;
}
}
false
TCResult::Error
}

View file

@ -456,9 +456,10 @@ void tc_uuid_to_buf(struct TCUuid tcuuid, char *buf);
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
} // extern "C"