sass_rs/
lib.rs

1extern crate sass_sys;
2extern crate libc;
3
4mod bindings;
5mod options;
6// mod dispatcher;
7
8pub use options::{Options, OutputStyle};
9pub use bindings::Context;
10
11use std::path::Path;
12
13
14/// Takes a file path and compiles it with the options given
15pub fn compile_file<P: AsRef<Path>>(path: P, options: Options) -> Result<String, String> {
16    let mut context = Context::new_file(path)?;
17    context.set_options(options);
18    context.compile()
19}
20
21/// Takes a string and compiles it with the options given
22pub fn compile_string(content: &str, options: Options) -> Result<String, String> {
23    let mut context = Context::new_data(content);
24    context.set_options(options);
25    context.compile()
26}
27
28#[cfg(test)]
29mod tests {
30    use super::{Options, OutputStyle, compile_string};
31
32    #[test]
33    fn can_compile_some_valid_scss_input() {
34        let input = "body { .hey { color: red; } }";
35        assert_eq!(compile_string(input, Options::default()),
36            Ok("body .hey {\n  color: red; }\n".to_string()));
37    }
38
39    #[test]
40    fn errors_with_invalid_scss_input() {
41        let input = "body { .hey { color: ; } }";
42        let res = compile_string(input, Options::default());
43        assert!(res.is_err());
44    }
45
46    #[test]
47    fn can_use_alternative_options() {
48        let input = "body { .hey { color: red; } }";
49        let mut opts = Options::default();
50        opts.output_style = OutputStyle::Compressed;
51        assert_eq!(compile_string(input, opts),
52            Ok("body .hey{color:red}\n".to_string()));
53    }
54}