1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//! Bindings for the [hunspell] C API.
//!
//! [hunspell]: https://hunspell.github.io/

#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]

include!(concat!(env!("OUT_DIR"), "/bindings.rs"));

#[cfg(test)]
mod tests {
    use super::*;
    use std::convert::TryInto;
    use std::ffi::{CStr, CString};
    use tempfile::tempdir;

    #[test]
    fn simple_sanity_test() {
        let dir = tempdir().unwrap();

        let aff = dir.path().join("foo.aff");

        std::fs::write(
            &aff,
            "SET UTF-8

SFX S Y 1
SFX S   0     s          [^sxzhy]
        ",
        )
        .unwrap();

        let dic = dir.path().join("foo.dic");

        std::fs::write(
            &dic,
            "2
cat/S
program/S
        ",
        )
        .unwrap();

        let aff = CString::new(aff.to_str().unwrap()).unwrap();
        let dic = CString::new(dic.to_str().unwrap()).unwrap();

        unsafe {
            let h = Hunspell_create(aff.as_ptr(), dic.as_ptr());

            assert!(!h.is_null());

            let cats = CString::new("cats").unwrap();

            let mut result = Vec::new();
            let mut list = std::ptr::null_mut();

            let n = Hunspell_stem(h, &mut list, cats.as_ptr());

            assert_ne!(n, 0);

            for i in 0..n {
                let item = CStr::from_ptr(*list.offset(i.try_into().unwrap()));
                result.push(item.to_str().unwrap());
            }

            assert_eq!(result, vec!["cat"]);

            Hunspell_free_list(h, &mut list, n);

            Hunspell_destroy(h);
        }
    }
}