From c9c72b4fd33e439b2d4bbd65d2c2b59f7469b8d1 Mon Sep 17 00:00:00 2001 From: "Dustin J. Mitchell" Date: Wed, 9 Feb 2022 23:43:23 +0000 Subject: [PATCH] return TCResult from tc_uuid_from_str --- integration-tests/src/bindings_tests/replica.c | 4 ++-- integration-tests/src/bindings_tests/uuid.c | 8 ++++---- lib/src/uuid.rs | 12 ++++++------ lib/taskchampion.h | 5 +++-- 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/integration-tests/src/bindings_tests/replica.c b/integration-tests/src/bindings_tests/replica.c index bf3cdaa43..11fc6d85c 100644 --- a/integration-tests/src/bindings_tests/replica.c +++ b/integration-tests/src/bindings_tests/replica.c @@ -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)); diff --git a/integration-tests/src/bindings_tests/uuid.c b/integration-tests/src/bindings_tests/uuid.c index 572a85322..0c9b72be1 100644 --- a/integration-tests/src/bindings_tests/uuid.c +++ b/integration-tests/src/bindings_tests/uuid.c @@ -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) { diff --git a/lib/src/uuid.rs b/lib/src/uuid.rs index cef445e40..415e9501a 100644 --- a/lib/src/uuid.rs +++ b/lib/src/uuid.rs @@ -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 } diff --git a/lib/taskchampion.h b/lib/taskchampion.h index fa15ae95d..4ac06acfd 100644 --- a/lib/taskchampion.h +++ b/lib/taskchampion.h @@ -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"