<commit_before>//! Allow access to the various flavours of sass contexts:
//! https://github.com/sass/libsass/wiki/API-Sass-Context

use std::ffi;
use std::sync::{Arc, RwLock};

use sass_sys;
use bindings::util;
use bindings::ptr::Unique;
use options::{OutputStyle, Options};
use bindings::SassOptions;


pub struct SassContext {
    // Need Unique to send between threads, libsass is thread safe
    pub raw: Unique<sass_sys::Sass_Context>,
    pub options: Arc<RwLock<SassOptions>>
}

pub struct SassFileContext {
    // Need Unique to send between threads, libsass is thread safe
    context: Unique<sass_sys::Sass_File_Context>,
    pub sass_context: SassContext
}

pub struct SassDataContext {
    // Need Unique to send between threads, libsass is thread safe
    context: Unique<sass_sys::Sass_Data_Context>,
    pub sass_context: SassContext
}

pub enum Context {
    File(SassFileContext),
    Data(SassDataContext),
}


impl Context {
    fn make_sass_context(c_sass_context: *mut sass_sys::Sass_Context) -> SassContext {;
        let options = unsafe { sass_sys::sass_context_get_options(c_sass_context) };
        let sass_options = Arc::new(RwLock::new(SassOptions {
            raw: unsafe { Unique::new(options) }
        }));

        SassContext {
            raw: unsafe { Unique::new(c_sass_context) },
            options: sass_options
        }
    }

    pub fn new_data(data: &str) -> Context {
        let c_str = ffi::CString::new(data).unwrap();
        let data_context = unsafe { sass_sys::sass_make_data_context(c_str.into_raw()) };
        let data_sass_context = unsafe { sass_sys::sass_data_context_get_context(data_context) };
        let sass_context = Context::make_sass_context(data_sass_context);<commit_msg>use strdup because sass_make_data_context frees its input<commit_after>//! Allow access to the various flavours of sass contexts:
//! https://github.com/sass/libsass/wiki/API-Sass-Context

use std::ffi;
use std::sync::{Arc, RwLock};
use libc::strdup;

use sass_sys;
use bindings::util;
use bindings::ptr::Unique;
use options::{OutputStyle, Options};
use bindings::SassOptions;


pub struct SassContext {
    // Need Unique to send between threads, libsass is thread safe
    pub raw: Unique<sass_sys::Sass_Context>,
    pub options: Arc<RwLock<SassOptions>>
}

pub struct SassFileContext {
    // Need Unique to send between threads, libsass is thread safe
    context: Unique<sass_sys::Sass_File_Context>,
    pub sass_context: SassContext
}

pub struct SassDataContext {
    // Need Unique to send between threads, libsass is thread safe
    context: Unique<sass_sys::Sass_Data_Context>,
    pub sass_context: SassContext
}

pub enum Context {
    File(SassFileContext),
    Data(SassDataContext),
}


impl Context {
    fn make_sass_context(c_sass_context: *mut sass_sys::Sass_Context) -> SassContext {;
        let options = unsafe { sass_sys::sass_context_get_options(c_sass_context) };
        let sass_options = Arc::new(RwLock::new(SassOptions {
            raw: unsafe { Unique::new(options) }
        }));

        SassContext {
            raw: unsafe { Unique::new(c_sass_context) },
            options: sass_options
        }
    }

    pub fn new_data(data: &str) -> Context {
        let c_str = ffi::CString::new(data).unwrap();
        let data_context = unsafe { sass_sys::sass_make_data_context(strdup(c_str.as_ptr())) };
        let data_sass_context = unsafe { sass_sys::sass_data_context_get_context(data_context) };
        let sass_context = Context::make_sass_context(data_sass_context);
{'fraction_non_alphanumeric': 0.10820685778527263, 'fraction_numerical': 0.0, 'mean_word_length': 3.8290366350067844, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 17, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1744582', 'n_tokens_mistral': 1199, 'n_tokens_neox': 1167, 'n_words': 265}