grass/src/imports.rs

44 lines
1.3 KiB
Rust
Raw Normal View History

2020-03-01 14:53:52 -05:00
use std::ffi::OsStr;
use std::path::Path;
use crate::error::SassResult;
use crate::scope::Scope;
use crate::{Stmt, StyleSheet};
2020-01-11 18:42:42 -05:00
pub(crate) fn import<P: AsRef<Path>>(path: P) -> SassResult<(Vec<Stmt>, Scope)> {
2020-01-18 15:47:51 -05:00
let mut rules: Vec<Stmt> = Vec::new();
let mut scope = Scope::new();
2020-01-18 19:00:49 -05:00
let path_buf = path.as_ref().to_path_buf();
let name = path_buf.file_name().expect("todo! path ended in `..`");
if path_buf.extension() == Some(OsStr::new(".css")) {
2020-01-18 18:12:53 -05:00
// || name.starts_with("http://") || name.starts_with("https://") {
2020-01-18 18:08:32 -05:00
todo!("handle css imports")
}
2020-01-18 19:00:49 -05:00
let mut p1 = path_buf.clone();
p1.push("index.scss");
let mut p2 = path_buf.clone();
p2.push("_index.scss");
2020-01-18 18:08:32 -05:00
let paths = [
2020-01-18 19:00:49 -05:00
path_buf.with_file_name(format!(
"{}.scss",
name.to_str().expect("path should be UTF-8")
)),
path_buf.with_file_name(format!(
"_{}.scss",
name.to_str().expect("path should be UTF-8")
)),
path_buf,
2020-01-18 18:08:32 -05:00
p1,
p2,
];
for name in &paths {
if name.is_file() {
2020-01-18 19:00:49 -05:00
let (rules2, scope2) =
StyleSheet::export_from_path(name.to_str().expect("path should be UTF-8"))?;
2020-01-18 15:47:51 -05:00
rules.extend(rules2);
scope.extend(scope2);
2020-01-18 15:47:51 -05:00
}
}
2020-01-18 19:00:49 -05:00
Ok((rules, scope))
2020-01-11 19:16:59 -05:00
}