hyper_static_server/
support_sass.rs

1
2
3#[ cfg (feature = "support-sass") ]
4use ::sass_rs as sass;
5
6
7use ::std::{
8		
9		cell,
10		env,
11		ffi,
12		fmt,
13		fs,
14		io,
15		iter,
16		mem,
17		path,
18		rc,
19		
20		collections::BTreeSet,
21		iter::Iterator,
22		path::{Path, PathBuf},
23		
24		str::FromStr as _,
25		fmt::{Write as _},
26		io::{Write as _},
27		
28	};
29
30
31use crate::builder_errors::*;
32
33
34
35
36#[ cfg (feature = "support-sass") ]
37pub fn compile_sass (_source : &Path) -> BuilderResult<String> {
38	
39	let _extension = _source.extension () .or_wrap (0x836ff108) ? .to_str () .or_wrap (0x4068e13f) ?;
40	let _indented_syntax = match _extension {
41		"sass" =>
42			true,
43		"scss" =>
44			false,
45		_ =>
46			return Err (error_with_code (0x720c0c23)),
47	};
48	
49	let _options = sass::Options {
50			output_style : sass::OutputStyle::Expanded,
51			precision : 4,
52			indented_syntax : _indented_syntax,
53			include_paths : vec! [],
54		};
55	
56	let mut _context = sass::Context::new_file (&_source) .map_err (|_message| error_with_message (0xfde48681, &_message)) ?;
57	_context.set_options (_options);
58	let _data = _context.compile () .map_err (|_message| error_with_message (0x00c4c0dd, &_message)) ?;
59	
60	Ok (_data)
61}
62
63
64
65
66#[ cfg (feature = "support-sass-alt") ]
67pub fn compile_sass (_source : &Path) -> BuilderResult<String> {
68	
69	let _parent = _source.parent () .or_wrap (0xf6ce0d79) ?;
70	
71	let _extension = _source.extension () .or_wrap (0xf2cd37bc) ? .to_str () .or_wrap (0xdb216e38) ?;
72	let _input_syntax = match _extension {
73		"sass" =>
74			sass::InputSyntax::SASS,
75		"scss" =>
76			sass::InputSyntax::SCSS,
77		_ =>
78			return Err (error_with_code (0x90668feb)),
79	};
80	
81	pub struct Importer { parent : PathBuf, resolved : rc::Rc<cell::RefCell<Vec<Box<Path>>>> }
82	impl sass::SassImporter for Importer {
83		fn callback (&mut self, _path_0 : &ffi::CStr, _compiler : sass::SassCompiler) -> Result<Option<Vec<sass::SassImportEntry>>, sass::SassImporterError> {
84			
85			let _path_0 = Path::new (_path_0.to_str () .or_panic (0xfd5fc0a2));
86			let _path = self.parent.join (_path_0);
87			
88			for _extension in &["sass", "scss", "css"] {
89				let _path = _path.with_extension (_extension);
90				if ! _path.exists () {
91					continue;
92				}
93				let _path = normalize_path (&_path);
94				// eprintln! ("[dd] [d84fd5f5] {} -> {}", _path_0.display (), _path.display ());
95				
96				// NOTE:  The code bellow will keep a reference to `_path` that should live until after the compilation completes.
97				let mut _resolved = self.resolved.borrow_mut ();
98				_resolved.push (_path.into_boxed_path ());
99				let _path = _resolved.last () .or_panic (0x5d9cba96) .as_ref ();
100				let _entry = sass::SassImport::AbsolutePath (_path);
101				
102				return Ok (Some (vec! (_entry.into_sass_import_entry ())));
103			}
104			
105			panic_with_format (0xe9c920d0, format_args! ("{}", _path.display ()));
106		}
107	}
108	impl fmt::Debug for Importer {
109		fn fmt (&self, _formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
110			_formatter.write_str ("SassImporter()")
111		}
112	}
113	
114	let _resolved = rc::Rc::new (cell::RefCell::new (Vec::new ()));
115	let _importer = Box::new (Importer { parent : _parent.into (), resolved : _resolved.clone () });
116	
117	let _options = sass::SassOptions {
118			output_style : sass::OutputStyle::Expanded,
119			source_comments : true,
120			source_map_embed : false,
121			source_map_contents : false,
122			source_map_file_urls : false,
123			omit_source_map_url : true,
124			indent : ffi::CString::new ("\t") .infallible (0x77771198),
125			linefeed : ffi::CString::new ("\n") .infallible (0xef2eea09),
126			precision : 4,
127			input_syntax : _input_syntax,
128			include_paths : &[],
129			function_list : rc::Rc::new (sass::SassFunctionList::new (Vec::new ())),
130			importer_list : rc::Rc::new (sass::SassImporterList::new (vec! (_importer))),
131			header_list : rc::Rc::new (sass::SassImporterList::new (Vec::new ())),
132		};
133	
134	let _data = _options.compile_file (_source) .or_wrap (0xbbaffa6f) ?;
135	
136	for _dependency in _resolved.borrow () .iter () {
137		self.dependencies_include (_dependency.as_ref ());
138	}
139	
140	Ok (_data)
141}
142