hunspell_sys/
lib.rs

1//! Bindings for the [hunspell] C API.
2//!
3//! [hunspell]: https://hunspell.github.io/
4
5#![allow(non_upper_case_globals)]
6#![allow(non_camel_case_types)]
7#![allow(non_snake_case)]
8
9include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
10
11#[cfg(test)]
12mod tests {
13    use super::*;
14    use std::convert::TryInto;
15    use std::ffi::{CStr, CString};
16    use tempfile::tempdir;
17
18    #[test]
19    fn simple_sanity_test() {
20        let dir = tempdir().unwrap();
21
22        let aff = dir.path().join("foo.aff");
23
24        std::fs::write(
25            &aff,
26            "SET UTF-8
27
28SFX S Y 1
29SFX S   0     s          [^sxzhy]
30        ",
31        )
32        .unwrap();
33
34        let dic = dir.path().join("foo.dic");
35
36        std::fs::write(
37            &dic,
38            "2
39cat/S
40program/S
41        ",
42        )
43        .unwrap();
44
45        let aff = CString::new(aff.to_str().unwrap()).unwrap();
46        let dic = CString::new(dic.to_str().unwrap()).unwrap();
47
48        unsafe {
49            let h = Hunspell_create(aff.as_ptr(), dic.as_ptr());
50
51            assert!(!h.is_null());
52
53            let cats = CString::new("cats").unwrap();
54
55            let mut result = Vec::new();
56            let mut list = std::ptr::null_mut();
57
58            let n = Hunspell_stem(h, &mut list, cats.as_ptr());
59
60            assert_ne!(n, 0);
61
62            for i in 0..n {
63                let item = CStr::from_ptr(*list.offset(i.try_into().unwrap()));
64                result.push(item.to_str().unwrap());
65            }
66
67            assert_eq!(result, vec!["cat"]);
68
69            Hunspell_free_list(h, &mut list, n);
70
71            Hunspell_destroy(h);
72        }
73    }
74}