Skip to content

Commit 4272e9a

Browse files
authored
refactor: more ergonomics api (#8)
* refactor: use path instead of string, 'static + Importer instead of Box<dyn Importer> * chore: clippy
1 parent 40fe97b commit 4272e9a

File tree

11 files changed

+311
-397
lines changed

11 files changed

+311
-397
lines changed

examples/big_scss/main.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,11 @@ fn main() {
1515
.parent()
1616
.unwrap()
1717
.join("examples/big_scss/big.scss");
18-
let path = &path.to_string_lossy().into_owned();
1918
let now = time::Instant::now();
2019
let mut sass = Sass::new(exe_path());
21-
let _ = sass.compile(path, Options::default()).unwrap();
22-
let _ = sass.compile(path, Options::default()).unwrap();
23-
let _ = sass.compile(path, Options::default()).unwrap();
24-
let _ = sass.compile(path, Options::default()).unwrap();
20+
let _ = sass.compile(&path, Options::default()).unwrap();
21+
let _ = sass.compile(&path, Options::default()).unwrap();
22+
let _ = sass.compile(&path, Options::default()).unwrap();
23+
let _ = sass.compile(&path, Options::default()).unwrap();
2524
dbg!(now.elapsed());
2625
}

examples/bootstrap5/main.rs

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,62 +22,46 @@ fn main() {
2222
let bootstrap_reboot = path.join("bootstrap-reboot.scss");
2323
let bootstrap_utilities = path.join("bootstrap-utilities.scss");
2424
let mut sass = Sass::new(exe_path());
25+
let _ = sass.compile(&bootstrap, Options::default()).unwrap();
26+
let _ = sass.compile(&bootstrap_grid, Options::default()).unwrap();
27+
let _ = sass.compile(&bootstrap_reboot, Options::default()).unwrap();
2528
let _ = sass
26-
.compile(bootstrap.to_string_lossy(), Options::default())
27-
.unwrap();
28-
let _ = sass
29-
.compile(bootstrap_grid.to_string_lossy(), Options::default())
30-
.unwrap();
31-
let _ = sass
32-
.compile(bootstrap_reboot.to_string_lossy(), Options::default())
33-
.unwrap();
34-
let _ = sass
35-
.compile(bootstrap_utilities.to_string_lossy(), Options::default())
29+
.compile(&bootstrap_utilities, Options::default())
3630
.unwrap();
3731

3832
let now = time::Instant::now();
33+
let _ = sass.compile(&bootstrap, Options::default()).unwrap();
34+
let _ = sass.compile(&bootstrap_grid, Options::default()).unwrap();
35+
let _ = sass.compile(&bootstrap_reboot, Options::default()).unwrap();
3936
let _ = sass
40-
.compile(bootstrap.to_string_lossy(), Options::default())
41-
.unwrap();
42-
let _ = sass
43-
.compile(bootstrap_grid.to_string_lossy(), Options::default())
44-
.unwrap();
45-
let _ = sass
46-
.compile(bootstrap_reboot.to_string_lossy(), Options::default())
47-
.unwrap();
48-
let _ = sass
49-
.compile(bootstrap_utilities.to_string_lossy(), Options::default())
37+
.compile(&bootstrap_utilities, Options::default())
5038
.unwrap();
5139
println!("modern: {:?}", now.elapsed());
5240

5341
#[cfg(feature = "legacy")]
5442
{
5543
let now = time::Instant::now();
5644
let _ = sass
57-
.render(
58-
LegacyOptionsBuilder::default()
59-
.file(bootstrap.to_string_lossy())
60-
.build(),
61-
)
45+
.render(LegacyOptionsBuilder::default().file(&bootstrap).build())
6246
.unwrap();
6347
let _ = sass
6448
.render(
6549
LegacyOptionsBuilder::default()
66-
.file(bootstrap_grid.to_string_lossy())
50+
.file(&bootstrap_grid)
6751
.build(),
6852
)
6953
.unwrap();
7054
let _ = sass
7155
.render(
7256
LegacyOptionsBuilder::default()
73-
.file(bootstrap_reboot.to_string_lossy())
57+
.file(&bootstrap_reboot)
7458
.build(),
7559
)
7660
.unwrap();
7761
let _ = sass
7862
.render(
7963
LegacyOptionsBuilder::default()
80-
.file(bootstrap_utilities.to_string_lossy())
64+
.file(&bootstrap_utilities)
8165
.build(),
8266
)
8367
.unwrap();

src/api.rs

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use std::fmt::Debug;
1+
use std::{
2+
fmt::Debug,
3+
path::{Path, PathBuf},
4+
};
25

36
use crate::{
47
protocol::{
@@ -23,7 +26,7 @@ pub struct Options {
2326
/// https://sass-lang.com/documentation/js-api/interfaces/Options#importers
2427
pub importers: Vec<SassImporter>,
2528
/// https://sass-lang.com/documentation/js-api/interfaces/Options#loadPaths
26-
pub load_paths: Vec<String>,
29+
pub load_paths: Vec<PathBuf>,
2730
/// https://sass-lang.com/documentation/js-api/interfaces/Options#logger
2831
pub logger: Option<SassLogger>,
2932
/// https://sass-lang.com/documentation/js-api/interfaces/Options#quietDeps
@@ -82,13 +85,14 @@ impl OptionsBuilder {
8285
self
8386
}
8487

85-
pub fn load_paths(mut self, arg: impl IntoIterator<Item = String>) -> Self {
86-
self.options.load_paths = arg.into_iter().collect();
88+
pub fn load_paths(mut self, arg: &[impl AsRef<Path>]) -> Self {
89+
self.options.load_paths =
90+
arg.iter().map(|p| p.as_ref().to_owned()).collect();
8791
self
8892
}
8993

90-
pub fn load_path(mut self, arg: impl Into<String>) -> Self {
91-
self.options.load_paths.push(arg.into());
94+
pub fn load_path(mut self, arg: impl AsRef<Path>) -> Self {
95+
self.options.load_paths.push(arg.as_ref().to_owned());
9296
self
9397
}
9498

@@ -134,28 +138,24 @@ impl OptionsBuilder {
134138

135139
pub fn sass_importers(
136140
mut self,
137-
arg: impl IntoIterator<Item = SassImporter>,
141+
arg: impl IntoIterator<Item = impl Into<SassImporter>>,
138142
) -> Self {
139-
self.options.importers = arg.into_iter().collect();
143+
self.options.importers = arg.into_iter().map(|i| i.into()).collect();
140144
self
141145
}
142146

143-
pub fn importer(mut self, arg: impl Into<Box<dyn Importer>>) -> Self {
147+
pub fn importer<I: 'static + Importer>(mut self, arg: I) -> Self {
144148
self
145149
.options
146150
.importers
147-
.push(SassImporter::Importer(arg.into()));
151+
.push(SassImporter::Importer(Box::new(arg) as Box<dyn Importer>));
148152
self
149153
}
150154

151-
pub fn file_importer(
152-
mut self,
153-
arg: impl Into<Box<dyn FileImporter>>,
154-
) -> Self {
155-
self
156-
.options
157-
.importers
158-
.push(SassImporter::FileImporter(arg.into()));
155+
pub fn file_importer<I: 'static + FileImporter>(mut self, arg: I) -> Self {
156+
self.options.importers.push(SassImporter::FileImporter(
157+
Box::new(arg) as Box<dyn FileImporter>
158+
));
159159
self
160160
}
161161
}
@@ -201,16 +201,16 @@ impl StringOptionsBuilder {
201201
self
202202
}
203203

204-
pub fn input_importer(mut self, arg: impl Into<Box<dyn Importer>>) -> Self {
205-
self.input_importer = Some(SassImporter::Importer(arg.into()));
204+
pub fn input_importer<I: 'static + Importer>(mut self, arg: I) -> Self {
205+
self.input_importer = Some(SassImporter::Importer(Box::new(arg)));
206206
self
207207
}
208208

209-
pub fn input_file_importer(
209+
pub fn input_file_importer<I: 'static + FileImporter>(
210210
mut self,
211-
arg: impl Into<Box<dyn FileImporter>>,
211+
arg: I,
212212
) -> Self {
213-
self.input_importer = Some(SassImporter::FileImporter(arg.into()));
213+
self.input_importer = Some(SassImporter::FileImporter(Box::new(arg)));
214214
self
215215
}
216216

@@ -234,13 +234,14 @@ impl StringOptionsBuilder {
234234
self
235235
}
236236

237-
pub fn load_paths(mut self, arg: impl IntoIterator<Item = String>) -> Self {
238-
self.options.load_paths = arg.into_iter().collect();
237+
pub fn load_paths(mut self, arg: &[impl AsRef<Path>]) -> Self {
238+
self.options.load_paths =
239+
arg.iter().map(|p| p.as_ref().to_owned()).collect();
239240
self
240241
}
241242

242-
pub fn load_path(mut self, arg: impl Into<String>) -> Self {
243-
self.options.load_paths.push(arg.into());
243+
pub fn load_path(mut self, arg: impl AsRef<Path>) -> Self {
244+
self.options.load_paths.push(arg.as_ref().to_owned());
244245
self
245246
}
246247

@@ -286,28 +287,25 @@ impl StringOptionsBuilder {
286287

287288
pub fn sass_importers(
288289
mut self,
289-
arg: impl IntoIterator<Item = SassImporter>,
290+
arg: impl IntoIterator<Item = impl Into<SassImporter>>,
290291
) -> Self {
291-
self.options.importers = arg.into_iter().collect();
292+
self.options.importers = arg.into_iter().map(|i| i.into()).collect();
292293
self
293294
}
294295

295-
pub fn importer(mut self, arg: impl Into<Box<dyn Importer>>) -> Self {
296+
pub fn importer<I: 'static + Importer>(mut self, arg: I) -> Self {
296297
self
297298
.options
298299
.importers
299-
.push(SassImporter::Importer(arg.into()));
300+
.push(SassImporter::Importer(Box::new(arg)));
300301
self
301302
}
302303

303-
pub fn file_importer(
304-
mut self,
305-
arg: impl Into<Box<dyn FileImporter>>,
306-
) -> Self {
304+
pub fn file_importer<I: 'static + FileImporter>(mut self, arg: I) -> Self {
307305
self
308306
.options
309307
.importers
310-
.push(SassImporter::FileImporter(arg.into()));
308+
.push(SassImporter::FileImporter(Box::new(arg)));
311309
self
312310
}
313311
}
@@ -368,7 +366,7 @@ pub struct ImporterResult {
368366
/// https://sass-lang.com/documentation/js-api/interfaces/ImporterResult#contents
369367
pub contents: String,
370368
/// https://sass-lang.com/documentation/js-api/interfaces/ImporterResult#sourceMapUrl
371-
pub source_map_url: Option<String>,
369+
pub source_map_url: Option<Url>,
372370
/// https://sass-lang.com/documentation/js-api/interfaces/ImporterResult#syntax
373371
pub syntax: Syntax,
374372
}
@@ -379,7 +377,7 @@ pub struct CompileResult {
379377
/// https://sass-lang.com/documentation/js-api/interfaces/CompileResult#css
380378
pub css: String,
381379
/// https://sass-lang.com/documentation/js-api/interfaces/CompileResult#loadedUrls
382-
pub loaded_urls: Vec<String>,
380+
pub loaded_urls: Vec<Url>,
383381
/// https://sass-lang.com/documentation/js-api/interfaces/CompileResult#sourceMap
384382
pub source_map: Option<String>,
385383
}
@@ -402,7 +400,11 @@ impl From<CompileSuccess> for CompileResult {
402400
fn from(s: CompileSuccess) -> Self {
403401
Self {
404402
css: s.css,
405-
loaded_urls: s.loaded_urls,
403+
loaded_urls: s
404+
.loaded_urls
405+
.iter()
406+
.map(|url| Url::parse(url).unwrap())
407+
.collect(),
406408
source_map: if s.source_map.is_empty() {
407409
None
408410
} else {

src/embedded.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::ffi::OsStr;
1+
use std::{ffi::OsStr, path::Path};
22

33
use atty::Stream;
44

@@ -31,7 +31,7 @@ impl Embedded {
3131

3232
pub fn compile(
3333
&mut self,
34-
path: impl Into<String>,
34+
path: impl AsRef<Path>,
3535
options: Options,
3636
) -> Result<CompileResult> {
3737
let mut logger_registry = LoggerRegistry::default();
@@ -55,7 +55,7 @@ impl Embedded {
5555
source_map_include_sources: options.source_map_include_sources,
5656
charset: options.charset,
5757
importers,
58-
input: Some(Input::Path(path.into())),
58+
input: Some(Input::Path(path.as_ref().to_str().unwrap().to_string())),
5959
// id: set in compile_request
6060
// global_functions: not implemented
6161
..Default::default()
@@ -90,7 +90,8 @@ impl Embedded {
9090
importer: Some(compile_request::importer::Importer::Path(
9191
std::env::current_dir()
9292
.unwrap()
93-
.to_string_lossy()
93+
.to_str()
94+
.unwrap()
9495
.to_string(),
9596
)),
9697
})

src/host/importer_registry.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::path::PathBuf;
2+
13
use rustc_hash::FxHashMap;
24

35
use crate::{
@@ -29,7 +31,7 @@ impl ImporterRegistry {
2931
pub fn register_all(
3032
&mut self,
3133
importers: Vec<SassImporter>,
32-
load_paths: Vec<String>,
34+
load_paths: Vec<PathBuf>,
3335
) -> impl Iterator<Item = compile_request::Importer> + '_ {
3436
let load_paths: Vec<_> = self.register_load_paths(load_paths).collect();
3537
self.register_importers(importers).chain(load_paths)
@@ -46,10 +48,12 @@ impl ImporterRegistry {
4648

4749
fn register_load_paths(
4850
&self,
49-
load_paths: Vec<String>,
51+
load_paths: Vec<PathBuf>,
5052
) -> impl Iterator<Item = compile_request::Importer> + '_ {
5153
load_paths.into_iter().map(|p| {
52-
let i = compile_request::importer::Importer::Path(p);
54+
let i = compile_request::importer::Importer::Path(
55+
p.to_str().unwrap().to_string(),
56+
);
5357
compile_request::Importer { importer: Some(i) }
5458
})
5559
}
@@ -107,7 +111,10 @@ impl ImporterRegistry {
107111
Some(import_response::Result::Success(ImportSuccess {
108112
contents: result.contents,
109113
syntax: result.syntax as i32,
110-
source_map_url: result.source_map_url.unwrap_or_default(),
114+
source_map_url: result
115+
.source_map_url
116+
.map(|url| url.to_string())
117+
.unwrap_or_default(),
111118
}))
112119
} else {
113120
None

src/legacy.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ use crate::{Embedded, Importer, Options, Result, SassImporter, StringOptions};
1111
impl Embedded {
1212
pub fn render(&mut self, options: LegacyOptions) -> Result<LegacyResult> {
1313
let start = SystemTime::now();
14-
let entry = options.file.clone();
14+
let entry = options
15+
.file
16+
.clone()
17+
.map(|file| file.to_str().unwrap().to_string())
18+
.unwrap_or_else(|| "stdin".to_string());
1519
let mut options = options.adjust_options();
1620
let result = if let Some(data) = options.data.clone() {
1721
let this = LegacyPluginThis::new(&options);
@@ -21,7 +25,7 @@ impl Embedded {
2125
this,
2226
importers,
2327
options.include_paths.clone(),
24-
options.file.clone().unwrap_or_else(|| "stdin".to_string()),
28+
&entry,
2529
);
2630
let importers = vec![SassImporter::Importer(Box::new(Arc::clone(
2731
&wrapper,
@@ -48,7 +52,7 @@ impl Embedded {
4852
this,
4953
importers,
5054
options.include_paths.clone(),
51-
options.file.clone().unwrap_or_else(|| "stdin".to_string()),
55+
&entry,
5256
);
5357
let importers = vec![SassImporter::Importer(Box::new(Arc::clone(
5458
&wrapper,

0 commit comments

Comments
 (0)