Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 109 additions & 1 deletion src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use std::ffi::{c_char, CStr, CString};
use std::ptr;

use crate::{
custom_rules, normalize, normalize_sentence, normalize_sentence_with_max_span, tn_normalize,
custom_rules, normalize, normalize_aviation, normalize_sentence, normalize_sentence_aviation,
normalize_sentence_aviation_with_max_span, normalize_sentence_with_max_span, tn_normalize,
tn_normalize_lang, tn_normalize_sentence, tn_normalize_sentence_lang,
tn_normalize_sentence_with_max_span, tn_normalize_sentence_with_max_span_lang,
};
Expand Down Expand Up @@ -90,6 +91,89 @@ pub unsafe extern "C" fn nemo_normalize_sentence_with_max_span(
}
}

/// Aviation-flavoured single-input normalize.
///
/// Layered on top of [`nemo_normalize`]: tries `cardinal::parse_aviation`
/// first so flight-number / call-sign phrases like `"seven eighty eight"`
/// resolve to `"788"`, then falls back to the regular dispatch.
///
/// # Safety
/// - `input` must be a valid null-terminated UTF-8 string
/// - Returns a newly allocated string that must be freed with `nemo_free_string`
#[no_mangle]
pub unsafe extern "C" fn nemo_normalize_aviation(input: *const c_char) -> *mut c_char {
if input.is_null() {
return ptr::null_mut();
}

let c_str = match CStr::from_ptr(input).to_str() {
Ok(s) => s,
Err(_) => return ptr::null_mut(),
};

let result = normalize_aviation(c_str);

match CString::new(result) {
Ok(c_string) => c_string.into_raw(),
Err(_) => ptr::null_mut(),
}
}

/// Aviation-flavoured sentence normalize.
///
/// Sentence-mode equivalent of [`nemo_normalize_aviation`]. Aviation cardinal
/// runs at priority 89 (above date / time, below money / measure), so
/// flight-number-style spans win without disturbing money / measure / decimal.
///
/// # Safety
/// - `input` must be a valid null-terminated UTF-8 string
/// - Returns a newly allocated string that must be freed with `nemo_free_string`
#[no_mangle]
pub unsafe extern "C" fn nemo_normalize_sentence_aviation(input: *const c_char) -> *mut c_char {
if input.is_null() {
return ptr::null_mut();
}

let c_str = match CStr::from_ptr(input).to_str() {
Ok(s) => s,
Err(_) => return ptr::null_mut(),
};

let result = normalize_sentence_aviation(c_str);

match CString::new(result) {
Ok(c_string) => c_string.into_raw(),
Err(_) => ptr::null_mut(),
}
}

/// Aviation sentence normalize with a configurable max span size.
///
/// # Safety
/// - `input` must be a valid null-terminated UTF-8 string
/// - Returns a newly allocated string that must be freed with `nemo_free_string`
#[no_mangle]
pub unsafe extern "C" fn nemo_normalize_sentence_aviation_with_max_span(
input: *const c_char,
max_span_tokens: u32,
) -> *mut c_char {
if input.is_null() {
return ptr::null_mut();
}

let c_str = match CStr::from_ptr(input).to_str() {
Ok(s) => s,
Err(_) => return ptr::null_mut(),
};

let result = normalize_sentence_aviation_with_max_span(c_str, max_span_tokens as usize);

match CString::new(result) {
Ok(c_string) => c_string.into_raw(),
Err(_) => ptr::null_mut(),
}
}

/// Free a string allocated by nemo_normalize or nemo_normalize_sentence.
///
/// # Safety
Expand Down Expand Up @@ -372,4 +456,28 @@ mod tests {
assert!(result.is_null());
}
}

#[test]
fn test_ffi_normalize_aviation() {
unsafe {
let input = CString::new("seven eighty eight").unwrap();
let result = nemo_normalize_aviation(input.as_ptr());
assert!(!result.is_null());
let result_str = CStr::from_ptr(result).to_str().unwrap();
assert_eq!(result_str, "788");
nemo_free_string(result);
}
}

#[test]
fn test_ffi_normalize_sentence_aviation() {
unsafe {
let input = CString::new("United seven eighty eight").unwrap();
let result = nemo_normalize_sentence_aviation(input.as_ptr());
assert!(!result.is_null());
let result_str = CStr::from_ptr(result).to_str().unwrap();
assert_eq!(result_str, "United 788");
nemo_free_string(result);
}
}
}
Loading
Loading