Skip to main content

fyaml/
lib.rs

1#![doc = include_str!(concat!(env!("OUT_DIR"), "/README.md"))]
2
3mod config;
4mod diag;
5pub mod error;
6mod ffi_util;
7mod node;
8mod scalar_parse;
9pub mod value;
10
11// Core modules (formerly v2)
12mod document;
13mod editor;
14mod iter;
15mod node_ref;
16mod parser;
17mod value_ref;
18
19// Re-export main API
20pub use document::Document;
21pub use editor::{Editor, RawNodeHandle};
22pub use iter::{MapIter, SeqIter};
23pub use node::{NodeStyle, NodeType};
24pub use node_ref::NodeRef;
25pub use parser::{DocumentIterator, FyParser};
26pub use value_ref::ValueRef;
27
28// Re-export error and value types
29pub use error::{Error, ParseError, Result};
30pub use value::{Number, TaggedValue, Value};
31
32/// Returns the version string of the underlying libfyaml C library.
33pub fn get_c_version() -> Result<String> {
34    log::trace!("get_c_version()");
35    let cstr_ptr = unsafe { fyaml_sys::fy_library_version() };
36    if cstr_ptr.is_null() {
37        log::error!("Null pointer received from fy_library_version");
38        return Err(Error::Ffi("fy_library_version returned null"));
39    }
40    log::trace!("convert to string");
41    let str = unsafe { std::ffi::CStr::from_ptr(cstr_ptr) };
42    log::trace!("done !");
43    Ok(str.to_string_lossy().into_owned())
44}
45
46#[cfg(test)]
47mod tests {
48    use crate::Document;
49
50    fn path(yaml: &str, path: &str) -> String {
51        let doc = Document::parse_str(yaml).unwrap();
52        let root = doc.root().unwrap();
53        if path.is_empty() {
54            root.emit().unwrap()
55        } else {
56            root.at_path(path).unwrap().emit().unwrap()
57        }
58    }
59
60    #[test]
61    fn test_simple_hash() {
62        assert_eq!(
63            path(
64                r#"
65        foo: bar
66        "#,
67                "/foo"
68            ),
69            "bar"
70        );
71    }
72
73    #[test]
74    fn test_no_path() {
75        let result = path(
76            r#"
77        foo: bar
78        "#,
79            "",
80        );
81        assert_eq!(result, "foo: bar");
82    }
83
84    #[test]
85    fn test_trap() {
86        assert_eq!(
87            path(
88                r#"
89        foo: "bar: wiz"
90        "#,
91                "/foo"
92            ),
93            "\"bar: wiz\""
94        );
95    }
96}