simplify TCResult to just two values

This commit is contained in:
Dustin J. Mitchell
2022-02-01 00:46:04 +00:00
parent b675cef99c
commit 22a6857c1b
6 changed files with 77 additions and 61 deletions

View File

@@ -6,6 +6,9 @@ use taskchampion::{Replica, StorageConfig, Uuid};
/// for querying and modifying that data.
///
/// TCReplicas are not threadsafe.
///
/// When a `tc_replica_..` function that returns a TCResult returns TC_RESULT_ERROR, then
/// `tc_replica_error` will return the error message.
pub struct TCReplica {
/// The wrapped Replica
inner: Replica,
@@ -213,18 +216,21 @@ pub extern "C" fn tc_replica_import_task_with_uuid(
/// Undo local operations until the most recent UndoPoint.
///
/// Returns TC_RESULT_TRUE if an undo occurred, TC_RESULT_FALSE if there are no operations
/// to be undone, or TC_RESULT_ERROR on error.
/// If undone_out is not NULL, then on success it is set to 1 if operations were undone, or 0 if
/// there are no operations that can be done.
#[no_mangle]
pub extern "C" fn tc_replica_undo<'a>(rep: *mut TCReplica) -> TCResult {
pub extern "C" fn tc_replica_undo<'a>(rep: *mut TCReplica, undone_out: *mut i32) -> TCResult {
wrap(
rep,
|rep| {
Ok(if rep.undo()? {
TCResult::True
} else {
TCResult::False
})
let undone = if rep.undo()? { 1 } else { 0 };
if !undone_out.is_null() {
// SAFETY:
// - undone_out is not NULL (just checked)
// - undone_out is properly aligned (implicitly promised by caller)
unsafe { *undone_out = undone };
}
Ok(TCResult::Ok)
},
TCResult::Error,
)

View File

@@ -1,11 +1,9 @@
// TODO: make true = 1, false = 0, error = -1
/// A result combines a boolean success value with
/// an error response. It is equivalent to `Result<bool, ()>`.
/// A result from a TC operation. Typically if this value is TC_RESULT_ERROR,
/// the associated object's `tc_.._error` method will return an error message.
/// cbindgen:prefix-with-name
/// cbindgen:rename-all=ScreamingSnakeCase
#[repr(C)]
#[repr(i32)]
pub enum TCResult {
Error = -1,
False = 0,
True = 1,
Ok = 0,
}

View File

@@ -16,6 +16,11 @@ use taskchampion::{Tag, Task, TaskMut};
/// be used until it is freed or converted to a TaskMut.
///
/// All `tc_task_..` functions taking a task as an argument require that it not be NULL.
///
/// When a `tc_task_..` function that returns a TCResult returns TC_RESULT_ERROR, then
/// `tc_task_error` will return the error message.
///
/// TCTasks are not threadsafe.
pub struct TCTask {
/// The wrapped Task or TaskMut
inner: Inner,
@@ -249,23 +254,19 @@ pub extern "C" fn tc_task_is_active<'a>(task: *mut TCTask) -> bool {
// TODO: tc_task_get_modified
/// Set a mutable task's status.
///
/// Returns TC_RESULT_TRUE on success and TC_RESULT_ERROR on failure.
#[no_mangle]
pub extern "C" fn tc_task_set_status<'a>(task: *mut TCTask, status: TCStatus) -> TCResult {
wrap_mut(
task,
|task| {
task.set_status(status.into())?;
Ok(TCResult::True)
Ok(TCResult::Ok)
},
TCResult::Error,
)
}
/// Set a mutable task's description.
///
/// Returns TC_RESULT_TRUE on success and TC_RESULT_ERROR on failure.
#[no_mangle]
pub extern "C" fn tc_task_set_description<'a>(
task: *mut TCTask,
@@ -279,7 +280,7 @@ pub extern "C" fn tc_task_set_description<'a>(
task,
|task| {
task.set_description(description.as_str()?.to_string())?;
Ok(TCResult::True)
Ok(TCResult::Ok)
},
TCResult::Error,
)
@@ -290,30 +291,26 @@ pub extern "C" fn tc_task_set_description<'a>(
// TODO: tc_task_set_modified
/// Start a task.
///
/// Returns TC_RESULT_TRUE on success and TC_RESULT_ERROR on failure.
#[no_mangle]
pub extern "C" fn tc_task_start<'a>(task: *mut TCTask) -> TCResult {
wrap_mut(
task,
|task| {
task.start()?;
Ok(TCResult::True)
Ok(TCResult::Ok)
},
TCResult::Error,
)
}
/// Stop a task.
///
/// Returns TC_RESULT_TRUE on success and TC_RESULT_ERROR on failure.
#[no_mangle]
pub extern "C" fn tc_task_stop<'a>(task: *mut TCTask) -> TCResult {
wrap_mut(
task,
|task| {
task.stop()?;
Ok(TCResult::True)
Ok(TCResult::Ok)
},
TCResult::Error,
)
@@ -323,8 +320,6 @@ pub extern "C" fn tc_task_stop<'a>(task: *mut TCTask) -> TCResult {
// TODO: tc_task_delete
/// 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:
@@ -337,7 +332,7 @@ pub extern "C" fn tc_task_add_tag<'a>(task: *mut TCTask, tag: *mut TCString) ->
let tagstr = tcstring.as_str()?;
let tag = Tag::from_str(tagstr)?;
task.add_tag(&tag)?;
Ok(TCResult::True)
Ok(TCResult::Ok)
},
TCResult::Error,
)