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
extern crate sass_sys;
extern crate libc;

mod bindings;
mod options;
// mod dispatcher;

pub use options::{Options, OutputStyle};
pub use bindings::Context;

use std::path::Path;


/// Takes a file path and compiles it with the options given
pub fn compile_file<P: AsRef<Path>>(path: P, options: Options) -> Result<String, String> {
    let mut context = Context::new_file(path)?;
    context.set_options(options);
    context.compile()
}

/// Takes a string and compiles it with the options given
pub fn compile_string(content: &str, options: Options) -> Result<String, String> {
    let mut context = Context::new_data(content);
    context.set_options(options);
    context.compile()
}

#[cfg(test)]
mod tests {
    use super::{Options, OutputStyle, compile_string};

    #[test]
    fn can_compile_some_valid_scss_input() {
        let input = "body { .hey { color: red; } }";
        assert_eq!(compile_string(input, Options::default()),
            Ok("body .hey {\n  color: red; }\n".to_string()));
    }

    #[test]
    fn errors_with_invalid_scss_input() {
        let input = "body { .hey { color: ; } }";
        let res = compile_string(input, Options::default());
        assert!(res.is_err());
    }

    #[test]
    fn can_use_alternative_options() {
        let input = "body { .hey { color: red; } }";
        let mut opts = Options::default();
        opts.output_style = OutputStyle::Compressed;
        assert_eq!(compile_string(input, opts),
            Ok("body .hey{color:red}\n".to_string()));
    }
}