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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use std::path::{Path, PathBuf};
#[derive(Clone, Debug)]
pub struct FileContext {
path: PathBuf,
}
impl FileContext {
pub fn new() -> Self {
FileContext {
path: PathBuf::new(),
}
}
pub fn file(&self, file: &Path) -> (Self, PathBuf) {
let t = self.path.join(file);
if let Some(dir) = t.parent() {
(
FileContext {
path: PathBuf::from(dir),
},
t.clone(),
)
} else {
(FileContext::new(), t.clone())
}
}
pub fn find_file(&self, name: &Path) -> Option<(Self, PathBuf)> {
let parent = name.parent();
if let Some(name) = name.file_name().and_then(|n| n.to_str()) {
for name in &[
name,
&format!("{}.scss", name),
&format!("_{}.scss", name),
&format!("{}/index.scss", name),
&format!("{}/_index.scss", name),
] {
let full = parent
.map(|p| p.join(name))
.unwrap_or_else(|| name.into());
let (c, p) = self.file(&full);
if p.is_file() {
return Some((c, p));
}
}
}
None
}
}