add tc_task_add_tag and check errors

This commit is contained in:
Dustin J. Mitchell 2022-01-31 19:44:00 +00:00
parent ef0bb2ced4
commit ce45c1004c
3 changed files with 61 additions and 3 deletions

View file

@ -2,7 +2,8 @@ use crate::{
replica::TCReplica, result::TCResult, status::TCStatus, string::TCString, uuid::TCUuid,
};
use std::ops::Deref;
use taskchampion::{Task, TaskMut};
use std::str::FromStr;
use taskchampion::{Tag, Task, TaskMut};
/// A task, as publicly exposed by this library.
///
@ -282,7 +283,6 @@ pub extern "C" fn tc_task_set_description<'a>(
)
}
// TODO: tc_task_set_description
// TODO: tc_task_set_entry
// TODO: tc_task_set_wait
// TODO: tc_task_set_modified
@ -319,7 +319,28 @@ pub extern "C" fn tc_task_stop<'a>(task: *mut TCTask) -> TCResult {
// TODO: tc_task_done
// TODO: tc_task_delete
// TODO: tc_task_add_tag
/// Add a tag to a mutable task.
///
/// Returns TC_RESULT_TRUE on success and TC_RESULT_ERROR on failure.
#[no_mangle]
pub extern "C" fn tc_task_add_tag<'a>(task: *mut TCTask, tag: *mut TCString) -> TCResult {
// SAFETY:
// - tcstring is not NULL (promised by caller)
// - caller is exclusive owner of tcstring (implicitly promised by caller)
let tcstring = unsafe { TCString::from_arg(tag) };
wrap_mut(
task,
|task| {
let tagstr = tcstring.as_str()?;
let tag = Tag::from_str(tagstr)?;
task.add_tag(&tag)?;
Ok(TCResult::True)
},
TCResult::Error,
)
}
// TODO: tc_task_remove_tag
// TODO: tc_task_add_annotation
// TODO: tc_task_remove_annotation

View file

@ -283,6 +283,13 @@ enum TCResult tc_task_start(struct TCTask *task);
*/
enum TCResult tc_task_stop(struct TCTask *task);
/**
* Add a tag to a mutable task.
*
* Returns TC_RESULT_TRUE on success and TC_RESULT_ERROR on failure.
*/
enum TCResult tc_task_add_tag(struct TCTask *task, struct TCString *tag);
/**
* Free a task. The given task must not be NULL. The task must not be used after this function
* returns, and must not be freed more than once.