test bindings in an integration-tests crate

This commit is contained in:
Dustin J. Mitchell
2022-01-25 01:27:24 +00:00
parent 56a805151d
commit c006cbe8e5
29 changed files with 4389 additions and 7123 deletions

View File

@@ -7,10 +7,12 @@ fn main() {
Builder::new()
.with_crate(crate_dir)
.with_language(Language::C)
.with_config(Config {
language: Language::C,
cpp_compat: true,
sys_includes: vec!["stdbool.h".into(), "stdint.h".into()],
usize_is_size_t: true,
no_includes: true,
enumeration: EnumConfig {
// this appears to still default to true for C
enum_class: false,

View File

@@ -1,14 +1,14 @@
use crate::{status::TCStatus, string::TCString, task::TCTask};
use libc::c_char;
use std::ffi::CString;
use taskchampion::{Replica, StorageConfig};
/// A replica represents an instance of a user's task data, providing an easy interface
/// for querying and modifying that data.
///
/// TCReplicas are not threadsafe.
pub struct TCReplica {
// TODO: make this a RefCell so that it can be take()n when holding a mut ref
inner: Replica,
error: Option<CString>,
error: Option<TCString<'static>>,
}
/// Utility function to safely convert *mut TCReplica into &mut TCReplica
@@ -17,6 +17,10 @@ fn rep_ref(rep: *mut TCReplica) -> &'static mut TCReplica {
unsafe { &mut *rep }
}
fn err_to_tcstring(e: impl std::string::ToString) -> TCString<'static> {
TCString::from(e.to_string())
}
/// Utility function to allow using `?` notation to return an error value.
fn wrap<'a, T, F>(rep: *mut TCReplica, f: F, err_value: T) -> T
where
@@ -26,41 +30,48 @@ where
match f(&mut rep.inner) {
Ok(v) => v,
Err(e) => {
let error = e.to_string();
let error = match CString::new(error.as_bytes()) {
Ok(e) => e,
Err(_) => CString::new("(invalid error message)".as_bytes()).unwrap(),
};
rep.error = Some(error);
rep.error = Some(err_to_tcstring(e));
err_value
}
}
}
/// Create a new TCReplica.
///
/// If path is NULL, then an in-memory replica is created. Otherwise, path is the path to the
/// on-disk storage for this replica. The path argument is no longer referenced after return.
///
/// Returns NULL on error; see tc_replica_error.
///
/// TCReplicas are not threadsafe.
/// Create a new TCReplica with an in-memory database. The contents of the database will be
/// lost when it is freed.
#[no_mangle]
pub extern "C" fn tc_replica_new<'a>(path: *mut TCString) -> *mut TCReplica {
let storage_res = if path.is_null() {
StorageConfig::InMemory.into_storage()
} else {
let path = TCString::from_arg(path);
StorageConfig::OnDisk {
taskdb_dir: path.to_path_buf(),
}
pub extern "C" fn tc_replica_new_in_memory() -> *mut TCReplica {
let storage = StorageConfig::InMemory
.into_storage()
};
.expect("in-memory always succeeds");
Box::into_raw(Box::new(TCReplica {
inner: Replica::new(storage),
error: None,
}))
}
/// Create a new TCReplica with an on-disk database. On error, a string is written to the
/// `error_out` parameter (if it is not NULL) and NULL is returned.
#[no_mangle]
pub extern "C" fn tc_replica_new_on_disk<'a>(
path: *mut TCString,
error_out: *mut *mut TCString,
) -> *mut TCReplica {
let path = TCString::from_arg(path);
let storage_res = StorageConfig::OnDisk {
taskdb_dir: path.to_path_buf(),
}
.into_storage();
let storage = match storage_res {
Ok(storage) => storage,
// TODO: report errors somehow
Err(_) => return std::ptr::null_mut(),
Err(e) => {
if !error_out.is_null() {
unsafe {
*error_out = err_to_tcstring(e).return_val();
}
}
return std::ptr::null_mut();
}
};
Box::into_raw(Box::new(TCReplica {
@@ -111,15 +122,15 @@ pub extern "C" fn tc_replica_undo<'a>(rep: *mut TCReplica) -> i32 {
}
/// Get the latest error for a replica, or NULL if the last operation succeeded.
///
/// The returned string is valid until the next replica operation.
/// Subsequent calls to this function will return NULL. The caller must free the
/// returned string.
#[no_mangle]
pub extern "C" fn tc_replica_error<'a>(rep: *mut TCReplica) -> *const c_char {
let rep: &'a TCReplica = rep_ref(rep);
if let Some(ref e) = rep.error {
e.as_ptr()
pub extern "C" fn tc_replica_error<'a>(rep: *mut TCReplica) -> *mut TCString<'static> {
let rep: &'a mut TCReplica = rep_ref(rep);
if let Some(tcstring) = rep.error.take() {
tcstring.return_val()
} else {
std::ptr::null()
std::ptr::null_mut()
}
}

View File

@@ -1,131 +1,185 @@
#include <cstdarg>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <ostream>
#include <new>
#include <stdbool.h>
#include <stdint.h>
/// The status of a task, as defined by the task data model.
enum TCStatus {
/**
* The status of a task, as defined by the task data model.
*/
typedef enum TCStatus {
TC_STATUS_PENDING,
TC_STATUS_COMPLETED,
TC_STATUS_DELETED,
/// Unknown signifies a status in the task DB that was not
/// recognized.
/**
* Unknown signifies a status in the task DB that was not
* recognized.
*/
TC_STATUS_UNKNOWN,
};
} TCStatus;
/// A replica represents an instance of a user's task data, providing an easy interface
/// for querying and modifying that data.
struct TCReplica;
/**
* A replica represents an instance of a user's task data, providing an easy interface
* for querying and modifying that data.
*
* TCReplicas are not threadsafe.
*/
typedef struct TCReplica TCReplica;
/// TCString supports passing strings into and out of the TaskChampion API.
///
/// Unless specified otherwise, functions in this API take ownership of a TCString when it appears
/// as a function argument, and transfer ownership to the caller when the TCString appears as a
/// return value or output argument.
struct TCString;
/**
* TCString supports passing strings into and out of the TaskChampion API.
*
* Unless specified otherwise, functions in this API take ownership of a TCString when it appears
* as a function argument, and transfer ownership to the caller when the TCString appears as a
* return value or output argument.
*/
typedef struct TCString TCString;
/// A task, as publicly exposed by this library.
///
/// A task carries no reference to the replica that created it, and can
/// be used until it is freed or converted to a TaskMut.
struct TCTask;
/**
* A task, as publicly exposed by this library.
*
* A task carries no reference to the replica that created it, and can
* be used until it is freed or converted to a TaskMut.
*/
typedef struct TCTask TCTask;
/// TCUuid is used as a task identifier. Uuids do not contain any pointers and need not be freed.
/// Uuids are typically treated as opaque, but the bytes are available in big-endian format.
///
struct TCUuid {
/**
* TCUuid is used as a task identifier. Uuids do not contain any pointers and need not be freed.
* Uuids are typically treated as opaque, but the bytes are available in big-endian format.
*
*/
typedef struct TCUuid {
uint8_t bytes[16];
};
} TCUuid;
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
extern const size_t TC_UUID_STRING_BYTES;
/// Create a new TCReplica.
///
/// If path is NULL, then an in-memory replica is created. Otherwise, path is the path to the
/// on-disk storage for this replica. The path argument is no longer referenced after return.
///
/// Returns NULL on error; see tc_replica_error.
///
/// TCReplicas are not threadsafe.
TCReplica *tc_replica_new(TCString *path);
/**
* Create a new TCReplica with an in-memory database. The contents of the database will be
* lost when it is freed.
*/
struct TCReplica *tc_replica_new_in_memory(void);
/// Create a new task. The task must not already exist.
///
/// Returns the task, or NULL on error.
TCTask *tc_replica_new_task(TCReplica *rep, TCStatus status, TCString *description);
/**
* Create a new TCReplica with an on-disk database. On error, a string is written to the
* `error_out` parameter (if it is not NULL) and NULL is returned.
*/
struct TCReplica *tc_replica_new_on_disk(struct TCString *path, struct TCString **error_out);
/// Undo local operations until the most recent UndoPoint.
///
/// Returns -1 on error, 0 if there are no local operations to undo, and 1 if operations were
/// undone.
int32_t tc_replica_undo(TCReplica *rep);
/**
* Create a new task. The task must not already exist.
*
* Returns the task, or NULL on error.
*/
struct TCTask *tc_replica_new_task(struct TCReplica *rep,
enum TCStatus status,
struct TCString *description);
/// Get the latest error for a replica, or NULL if the last operation succeeded.
///
/// The returned string is valid until the next replica operation.
const char *tc_replica_error(TCReplica *rep);
/**
* Undo local operations until the most recent UndoPoint.
*
* Returns -1 on error, 0 if there are no local operations to undo, and 1 if operations were
* undone.
*/
int32_t tc_replica_undo(struct TCReplica *rep);
/// Free a TCReplica.
void tc_replica_free(TCReplica *rep);
/**
* Get the latest error for a replica, or NULL if the last operation succeeded.
* Subsequent calls to this function will return NULL. The caller must free the
* returned string.
*/
struct TCString *tc_replica_error(struct TCReplica *rep);
/// Create a new TCString referencing the given C string. The C string must remain valid until
/// after the TCString is freed. It's typically easiest to ensure this by using a static string.
TCString *tc_string_new(const char *cstr);
/**
* Free a TCReplica.
*/
void tc_replica_free(struct TCReplica *rep);
/// Create a new TCString by cloning the content of the given C string.
TCString *tc_string_clone(const char *cstr);
/**
* Create a new TCString referencing the given C string. The C string must remain valid until
* after the TCString is freed. It's typically easiest to ensure this by using a static string.
*/
struct TCString *tc_string_new(const char *cstr);
/// Create a new TCString containing the given string with the given length. This allows creation
/// of strings containing embedded NUL characters. If the given string is not valid UTF-8, this
/// function will return NULL.
TCString *tc_string_clone_with_len(const char *buf, size_t len);
/**
* Create a new TCString by cloning the content of the given C string.
*/
struct TCString *tc_string_clone(const char *cstr);
/// Get the content of the string as a regular C string. The given string must not be NULL. The
/// returned value is NULL if the string contains NUL bytes. The returned string is valid until
/// the TCString is freed or passed to another TC API function.
///
/// This function does _not_ take ownership of the TCString.
const char *tc_string_content(TCString *tcstring);
/**
* Create a new TCString containing the given string with the given length. This allows creation
* of strings containing embedded NUL characters. If the given string is not valid UTF-8, this
* function will return NULL.
*/
struct TCString *tc_string_clone_with_len(const char *buf, size_t len);
/// Get the content of the string as a pointer and length. The given string must not be NULL.
/// This function can return any string, even one including NUL bytes. The returned string is
/// valid until the TCString is freed or passed to another TC API function.
///
/// This function does _not_ take ownership of the TCString.
const char *tc_string_content_with_len(TCString *tcstring, size_t *len_out);
/**
* Get the content of the string as a regular C string. The given string must not be NULL. The
* returned value is NULL if the string contains NUL bytes. The returned string is valid until
* the TCString is freed or passed to another TC API function.
*
* This function does _not_ take ownership of the TCString.
*/
const char *tc_string_content(struct TCString *tcstring);
/// Free a TCString.
void tc_string_free(TCString *string);
/**
* Get the content of the string as a pointer and length. The given string must not be NULL.
* This function can return any string, even one including NUL bytes. The returned string is
* valid until the TCString is freed or passed to another TC API function.
*
* This function does _not_ take ownership of the TCString.
*/
const char *tc_string_content_with_len(struct TCString *tcstring, size_t *len_out);
/// Get a task's UUID.
TCUuid tc_task_get_uuid(const TCTask *task);
/**
* Free a TCString.
*/
void tc_string_free(struct TCString *string);
/// Get a task's status.
TCStatus tc_task_get_status(const TCTask *task);
/**
* Get a task's UUID.
*/
struct TCUuid tc_task_get_uuid(const struct TCTask *task);
/// Get a task's description, or NULL if the task cannot be represented as a C string (e.g., if it
/// contains embedded NUL characters).
TCString *tc_task_get_description(const TCTask *task);
/**
* Get a task's status.
*/
enum TCStatus tc_task_get_status(const struct TCTask *task);
/// Free a task.
void tc_task_free(TCTask *task);
/**
* Get a task's description, or NULL if the task cannot be represented as a C string (e.g., if it
* contains embedded NUL characters).
*/
struct TCString *tc_task_get_description(const struct TCTask *task);
/// Create a new, randomly-generated UUID.
TCUuid tc_uuid_new_v4();
/**
* Free a task.
*/
void tc_task_free(struct TCTask *task);
/// Create a new UUID with the nil value.
TCUuid tc_uuid_nil();
/**
* Create a new, randomly-generated UUID.
*/
struct TCUuid tc_uuid_new_v4(void);
/// Write the string representation of a TCUuid into the given buffer, which must be
/// at least TC_UUID_STRING_BYTES long. No NUL terminator is added.
void tc_uuid_to_str(TCUuid uuid, char *out);
/**
* Create a new UUID with the nil value.
*/
struct TCUuid tc_uuid_nil(void);
/// Parse the given value as a UUID. The value must be exactly TC_UUID_STRING_BYTES long. Returns
/// false on failure.
bool tc_uuid_from_str(const char *val, TCUuid *out);
/**
* Write the string representation of a TCUuid into the given buffer, which must be
* at least TC_UUID_STRING_BYTES long. No NUL terminator is added.
*/
void tc_uuid_to_str(struct TCUuid uuid, char *out);
/**
* Parse the given value as a UUID. The value must be exactly TC_UUID_STRING_BYTES long. Returns
* false on failure.
*/
bool tc_uuid_from_str(const char *val, struct TCUuid *out);
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus