From f3b73ca0e4f120df773a69ccc7d8d6515ac62159 Mon Sep 17 00:00:00 2001 From: "Dustin J. Mitchell" Date: Wed, 26 Jan 2022 01:49:06 +0000 Subject: [PATCH] add task_import_with_uuid --- integration-tests/src/bindings_tests/task.c | 23 ++++++++++++++++++ lib/src/replica.rs | 26 +++++++++++++++++---- lib/taskchampion.h | 7 ++++++ 3 files changed, 52 insertions(+), 4 deletions(-) diff --git a/integration-tests/src/bindings_tests/task.c b/integration-tests/src/bindings_tests/task.c index b0bbc4117..bd9e14f4b 100644 --- a/integration-tests/src/bindings_tests/task.c +++ b/integration-tests/src/bindings_tests/task.c @@ -26,9 +26,32 @@ static void test_task_creation(void) { tc_replica_free(rep); } +// importing a task succeeds and the resulting task looks good +static void test_task_import(void) { + TCReplica *rep = tc_replica_new_in_memory(); + 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)); + TCTask *task = tc_replica_import_task_with_uuid(rep, uuid); + TEST_ASSERT_NOT_NULL(task); + + TEST_ASSERT_EQUAL(TC_STATUS_PENDING, tc_task_get_status(task)); + + TCString *desc = tc_task_get_description(task); + TEST_ASSERT_NOT_NULL(desc); + TEST_ASSERT_EQUAL_STRING("", tc_string_content(desc)); // default value + tc_string_free(desc); + + tc_task_free(task); + + tc_replica_free(rep); +} + int task_tests(void) { UNITY_BEGIN(); // each test case above should be named here, in order. RUN_TEST(test_task_creation); + RUN_TEST(test_task_import); return UNITY_END(); } diff --git a/lib/src/replica.rs b/lib/src/replica.rs index 3d2eafd2f..f77612b41 100644 --- a/lib/src/replica.rs +++ b/lib/src/replica.rs @@ -1,5 +1,5 @@ -use crate::{result::TCResult, status::TCStatus, string::TCString, task::TCTask}; -use taskchampion::{Replica, StorageConfig}; +use crate::{result::TCResult, status::TCStatus, string::TCString, task::TCTask, uuid::TCUuid}; +use taskchampion::{Replica, StorageConfig, Uuid}; /// A replica represents an instance of a user's task data, providing an easy interface /// for querying and modifying that data. @@ -89,7 +89,7 @@ pub extern "C" fn tc_replica_new_on_disk<'a>( /// /// Returns the task, or NULL on error. #[no_mangle] -pub extern "C" fn tc_replica_new_task<'a>( +pub extern "C" fn tc_replica_new_task( rep: *mut TCReplica, status: TCStatus, description: *mut TCString, @@ -105,7 +105,25 @@ pub extern "C" fn tc_replica_new_task<'a>( ) } -// TODO: tc_replica_import_task_with_uuid +/// Create a new task. The task must not already exist. +/// +/// Returns the task, or NULL on error. +#[no_mangle] +pub extern "C" fn tc_replica_import_task_with_uuid( + rep: *mut TCReplica, + uuid: TCUuid, +) -> *mut TCTask { + wrap( + rep, + |rep| { + let uuid: Uuid = uuid.into(); + let task = rep.import_task_with_uuid(uuid)?; + Ok(TCTask::as_ptr(task)) + }, + std::ptr::null_mut(), + ) +} + // TODO: tc_replica_sync /// Undo local operations until the most recent UndoPoint. diff --git a/lib/taskchampion.h b/lib/taskchampion.h index b09a8f4d5..a82ef49a3 100644 --- a/lib/taskchampion.h +++ b/lib/taskchampion.h @@ -93,6 +93,13 @@ struct TCTask *tc_replica_new_task(struct TCReplica *rep, enum TCStatus status, struct TCString *description); +/** + * Create a new task. The task must not already exist. + * + * Returns the task, or NULL on error. + */ +struct TCTask *tc_replica_import_task_with_uuid(struct TCReplica *rep, struct TCUuid uuid); + /** * Undo local operations until the most recent UndoPoint. *