mirror of
https://github.com/GothenburgBitFactory/taskwarrior.git
synced 2025-07-07 20:06:36 +02:00
support generation of paths from TCString on Windows
This commit is contained in:
parent
5072ed7458
commit
aadd4a762f
3 changed files with 62 additions and 40 deletions
|
@ -94,6 +94,34 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Utility function to allow using `?` notation to return an error value in the constructor.
|
||||||
|
fn wrap_constructor<T, F>(f: F, error_out: *mut TCString, err_value: T) -> T
|
||||||
|
where
|
||||||
|
F: FnOnce() -> anyhow::Result<T>,
|
||||||
|
{
|
||||||
|
if !error_out.is_null() {
|
||||||
|
// SAFETY:
|
||||||
|
// - error_out is not NULL (just checked)
|
||||||
|
// - properly aligned and valid (promised by caller)
|
||||||
|
unsafe { *error_out = TCString::default() };
|
||||||
|
}
|
||||||
|
|
||||||
|
match f() {
|
||||||
|
Ok(v) => v,
|
||||||
|
Err(e) => {
|
||||||
|
if !error_out.is_null() {
|
||||||
|
// SAFETY:
|
||||||
|
// - error_out is not NULL (just checked)
|
||||||
|
// - properly aligned and valid (promised by caller)
|
||||||
|
unsafe {
|
||||||
|
TCString::val_to_arg_out(err_to_ruststring(e), error_out);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
err_value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Create a new TCReplica with an in-memory database. The contents of the database will be
|
/// Create a new TCReplica with an in-memory database. The contents of the database will be
|
||||||
/// lost when it is freed with tc_replica_free.
|
/// lost when it is freed with tc_replica_free.
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
|
@ -114,40 +142,24 @@ pub unsafe extern "C" fn tc_replica_new_on_disk(
|
||||||
path: TCString,
|
path: TCString,
|
||||||
error_out: *mut TCString,
|
error_out: *mut TCString,
|
||||||
) -> *mut TCReplica {
|
) -> *mut TCReplica {
|
||||||
if !error_out.is_null() {
|
wrap_constructor(
|
||||||
// SAFETY:
|
|| {
|
||||||
// - error_out is not NULL (just checked)
|
|
||||||
// - properly aligned and valid (promised by caller)
|
|
||||||
unsafe { *error_out = TCString::default() };
|
|
||||||
}
|
|
||||||
|
|
||||||
// SAFETY:
|
// SAFETY:
|
||||||
// - path is valid (promised by caller)
|
// - path is valid (promised by caller)
|
||||||
// - caller will not use path after this call (convention)
|
// - caller will not use path after this call (convention)
|
||||||
let path = unsafe { TCString::val_from_arg(path) };
|
let mut path = unsafe { TCString::val_from_arg(path) };
|
||||||
let storage_res = StorageConfig::OnDisk {
|
let storage = StorageConfig::OnDisk {
|
||||||
taskdb_dir: path.to_path_buf(),
|
taskdb_dir: path.to_path_buf()?,
|
||||||
}
|
}
|
||||||
.into_storage();
|
.into_storage()?;
|
||||||
|
|
||||||
let storage = match storage_res {
|
|
||||||
Ok(storage) => storage,
|
|
||||||
Err(e) => {
|
|
||||||
if !error_out.is_null() {
|
|
||||||
unsafe {
|
|
||||||
// SAFETY:
|
|
||||||
// - error_out is not NULL (just checked)
|
|
||||||
// - properly aligned and valid (promised by caller)
|
|
||||||
TCString::val_to_arg_out(err_to_ruststring(e), error_out);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return std::ptr::null_mut();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// SAFETY:
|
// SAFETY:
|
||||||
// - caller promises to free this value
|
// - caller promises to free this value
|
||||||
unsafe { TCReplica::from(Replica::new(storage)).return_ptr() }
|
Ok(unsafe { TCReplica::from(Replica::new(storage)).return_ptr() })
|
||||||
|
},
|
||||||
|
error_out,
|
||||||
|
std::ptr::null_mut(),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get a list of all tasks in the replica.
|
/// Get a list of all tasks in the replica.
|
||||||
|
|
|
@ -70,9 +70,9 @@ pub unsafe extern "C" fn tc_server_new_local(
|
||||||
// SAFETY:
|
// SAFETY:
|
||||||
// - server_dir is valid (promised by caller)
|
// - server_dir is valid (promised by caller)
|
||||||
// - caller will not use server_dir after this call (convention)
|
// - caller will not use server_dir after this call (convention)
|
||||||
let server_dir = unsafe { TCString::val_from_arg(server_dir) };
|
let mut server_dir = unsafe { TCString::val_from_arg(server_dir) };
|
||||||
let server_config = ServerConfig::Local {
|
let server_config = ServerConfig::Local {
|
||||||
server_dir: server_dir.to_path_buf(),
|
server_dir: server_dir.to_path_buf()?,
|
||||||
};
|
};
|
||||||
let server = server_config.into_server()?;
|
let server = server_config.into_server()?;
|
||||||
// SAFETY: caller promises to free this server.
|
// SAFETY: caller promises to free this server.
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
use crate::traits::*;
|
use crate::traits::*;
|
||||||
use crate::util::{string_into_raw_parts, vec_into_raw_parts};
|
use crate::util::{string_into_raw_parts, vec_into_raw_parts};
|
||||||
use std::ffi::{CStr, CString, OsStr};
|
use std::ffi::{CStr, CString, OsStr, OsString};
|
||||||
use std::os::raw::c_char;
|
use std::os::raw::c_char;
|
||||||
use std::os::unix::ffi::OsStrExt;
|
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
/// TCString supports passing strings into and out of the TaskChampion API.
|
/// TCString supports passing strings into and out of the TaskChampion API.
|
||||||
|
@ -293,10 +292,21 @@ impl<'a> RustString<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn to_path_buf(&self) -> PathBuf {
|
pub(crate) fn to_path_buf(&mut self) -> Result<PathBuf, std::str::Utf8Error> {
|
||||||
// TODO: this is UNIX-specific.
|
#[cfg(unix)]
|
||||||
let path: &OsStr = OsStr::from_bytes(self.as_bytes());
|
let path: OsString = {
|
||||||
path.to_os_string().into()
|
// on UNIX, we can use the bytes directly, without requiring that they
|
||||||
|
// be valid UTF-8.
|
||||||
|
use std::os::unix::ffi::OsStrExt;
|
||||||
|
OsStr::from_bytes(self.as_bytes()).to_os_string()
|
||||||
|
};
|
||||||
|
#[cfg(windows)]
|
||||||
|
let path: OsString = {
|
||||||
|
// on Windows, we assume the filename is valid Unicode, so it can be
|
||||||
|
// represented as UTF-8.
|
||||||
|
OsString::from(self.as_str()?.to_string())
|
||||||
|
};
|
||||||
|
Ok(path.into())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue