Skip to main content

llama_cpp_bindings/mtmd/
mtmd_default_marker.rs

1use std::ffi::CStr;
2use std::os::raw::c_char;
3
4use crate::mtmd::mtmd_default_marker_error::MtmdDefaultMarkerError;
5
6unsafe fn marker_bytes_to_str(
7    c_str: *const c_char,
8) -> Result<&'static str, MtmdDefaultMarkerError> {
9    Ok(unsafe { CStr::from_ptr(c_str) }.to_str()?)
10}
11
12/// # Errors
13///
14/// Returns [`MtmdDefaultMarkerError::NotUtf8`] if llama.cpp's `mtmd_default_marker`
15/// returns bytes that are not valid UTF-8. The marker is a fixed ASCII constant in
16/// the vendored library; surfacing the error keeps the failure explicit rather than
17/// papering over it with a substituted literal.
18pub fn mtmd_default_marker() -> Result<&'static str, MtmdDefaultMarkerError> {
19    unsafe { marker_bytes_to_str(llama_cpp_bindings_sys::mtmd_default_marker()) }
20}
21
22#[cfg(test)]
23mod tests {
24    use std::os::raw::c_char;
25
26    use super::marker_bytes_to_str;
27    use super::mtmd_default_marker;
28    use crate::mtmd::mtmd_default_marker_error::MtmdDefaultMarkerError;
29
30    #[test]
31    fn returns_non_empty_marker() {
32        let marker = mtmd_default_marker().expect("vendored marker must be valid UTF-8");
33        assert!(!marker.is_empty());
34    }
35
36    #[test]
37    fn non_utf8_marker_bytes_return_not_utf8_error() {
38        let invalid: [u8; 3] = [0xFF, 0xFE, 0x00];
39        let result = unsafe { marker_bytes_to_str(invalid.as_ptr().cast::<c_char>()) };
40
41        assert!(matches!(result, Err(MtmdDefaultMarkerError::NotUtf8(_))));
42    }
43}