Skip to content

Commit

Permalink
do the todo
Browse files Browse the repository at this point in the history
  • Loading branch information
molpopgen committed Jan 2, 2024
1 parent 3e1badf commit 80b22f4
Showing 1 changed file with 33 additions and 12 deletions.
45 changes: 33 additions & 12 deletions demes/src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,18 +382,24 @@ pub unsafe extern "C" fn demes_graph_to_yaml(
///
/// # Safety
///
/// * `graph` must be a non-NULL pointers to a [`Graph`]
/// * `graph` must be a non-NULL pointer to a [`Graph`]
/// * `error` must be a non-NULL pointer to a [`FFIError`]
///
/// # Notes
///
/// * The return value, if not NULL, **must** be freed by the C `free` function.
#[no_mangle]
pub unsafe extern "C" fn demes_graph_toplevel_metadata_yaml(graph: *const Graph) -> *mut c_char {
todo!("should take error");
pub unsafe extern "C" fn demes_graph_toplevel_metadata_yaml(
graph: *const Graph,
error: *mut FFIError,
) -> *mut c_char {
match (*graph).metadata() {
None => std::ptr::null_mut(),
Some(metadata) => match serde_yaml::to_string(&metadata) {
Err(_) => std::ptr::null_mut(),
Err(e) => {
(*error).error = Some(ErrorDetails::NonLibraryError(Box::new(e)));
std::ptr::null_mut()
}
Ok(metadata) => str_to_owned_c_char(&metadata),
},
}
Expand All @@ -417,12 +423,17 @@ pub unsafe extern "C" fn demes_graph_toplevel_metadata_yaml(graph: *const Graph)
/// * The return value, if not NULL, **must** be freed by the C `free` function.
#[cfg(feature = "json")]
#[no_mangle]
pub unsafe extern "C" fn demes_graph_toplevel_metadata_json(graph: *const Graph) -> *mut c_char {
todo!("should take error");
pub unsafe extern "C" fn demes_graph_toplevel_metadata_json(
graph: *const Graph,
error: *mut FFIError,
) -> *mut c_char {
match (*graph).metadata() {
None => std::ptr::null_mut(),
Some(metadata) => match serde_json::to_string(&metadata) {
Err(_) => std::ptr::null_mut(),
Err(e) => {
(*error).error = Some(ErrorDetails::NonLibraryError(Box::new(e)));
std::ptr::null_mut()
}
Ok(metadata) => str_to_owned_c_char(&metadata),
},
}
Expand Down Expand Up @@ -569,10 +580,15 @@ fn test_metadata_yaml() {
start_size: 100
";
let graph = crate::loads(yaml).unwrap();
let metadata_string = unsafe { demes_graph_toplevel_metadata_yaml(&graph as *const Graph) };
let error = demes_error_allocate();
let metadata_string =
unsafe { demes_graph_toplevel_metadata_yaml(&graph as *const Graph, error) };
let cstr = unsafe { CStr::from_ptr(metadata_string) };
assert_eq!(cstr.to_str().unwrap(), "X: 3,\nY: unicorns\n");
unsafe { libc::free(metadata_string as _) };
unsafe {
libc::free(metadata_string as _);
demes_error_deallocate(error);
};
}

#[cfg(feature = "json")]
Expand All @@ -590,10 +606,15 @@ fn test_metadata_json() {
start_size: 100
";
let graph = crate::loads(yaml).unwrap();
let metadata_string = unsafe { demes_graph_toplevel_metadata_json(&graph as *const Graph) };
let error = demes_error_allocate();
let metadata_string =
unsafe { demes_graph_toplevel_metadata_json(&graph as *const Graph, error) };
let cstr = unsafe { CStr::from_ptr(metadata_string) };
assert_eq!(cstr.to_str().unwrap(), "X: 3,\nY: unicorns\n");
unsafe { libc::free(metadata_string as _) };
assert_eq!(cstr.to_str().unwrap(), "{\"X\":\"3,\",\"Y\":\"unicorns\"}");
unsafe {
libc::free(metadata_string as _);
demes_error_deallocate(error);
};
}

#[test]
Expand Down

0 comments on commit 80b22f4

Please sign in to comment.