Skip to content

Commit 5689585

Browse files
authored
test: importer spec (#2)
1 parent 6296420 commit 5689585

File tree

9 files changed

+1377
-45
lines changed

9 files changed

+1377
-45
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,9 @@ dashmap = "5"
1212
crossbeam-channel = "0.5"
1313
rustc-hash = "1"
1414

15+
[dev-dependencies]
16+
serde_json = "1"
17+
tempfile = "3"
18+
1519
[build-dependencies]
1620
prost-build = "0.10"

src/dispatcher.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::sync::Arc;
55
use crate::{
66
compiler::Compiler,
77
connection::{Connected, ConnectedGuard, Connection, Unconnected},
8-
host::{Host},
8+
host::Host,
99
protocol::{outbound_message, InboundMessage, OutboundMessage},
1010
};
1111

@@ -51,7 +51,7 @@ impl Dispatcher {
5151
}
5252

5353
pub fn unsubscribe(&self, id: &u32) {
54-
self.observers.remove(&id);
54+
self.observers.remove(id);
5555
}
5656

5757
pub fn send_message(&self, inbound_message: InboundMessage) {
@@ -67,10 +67,8 @@ impl Dispatcher {
6767
for ob in self.observers.iter() {
6868
ob.error(e.clone());
6969
}
70-
} else {
71-
if let Some(ob) = self.observers.get(&e.id) {
72-
ob.error(e);
73-
}
70+
} else if let Some(ob) = self.observers.get(&e.id) {
71+
ob.error(e);
7472
}
7573
}
7674
outbound_message::Message::CompileResponse(e) => {

src/embedded.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,16 @@ impl Embedded {
4444
options.load_paths.unwrap_or_default(),
4545
)
4646
.collect();
47-
options.logger.map(|l| logger_registry.register(l));
47+
if let Some(l) = options.logger {
48+
logger_registry.register(l);
49+
}
4850

4951
let request = CompileRequest {
5052
style: options.style as i32,
5153
source_map: options.source_map,
52-
alert_color: options.alert_color.unwrap_or(atty::is(Stream::Stdout)),
54+
alert_color: options
55+
.alert_color
56+
.unwrap_or_else(|| atty::is(Stream::Stdout)),
5357
alert_ascii: options.alert_ascii,
5458
verbose: options.verbose,
5559
quiet_deps: options.quiet_deps,
@@ -65,7 +69,7 @@ impl Embedded {
6569
let host = Host::new(importer_registry, logger_registry);
6670
let conn = self.channel.connect(host);
6771
let response = conn.compile_request(request)?;
68-
Ok(CompileResult::try_from(response)?)
72+
CompileResult::try_from(response)
6973
}
7074

7175
pub fn compile_string(
@@ -81,15 +85,17 @@ impl Embedded {
8185
options.common.load_paths.unwrap_or_default(),
8286
)
8387
.collect();
84-
options.common.logger.map(|l| logger_registry.register(l));
88+
if let Some(l) = options.common.logger {
89+
logger_registry.register(l);
90+
}
8591

8692
let request = CompileRequest {
8793
style: options.common.style as i32,
8894
source_map: options.common.source_map,
8995
alert_color: options
9096
.common
9197
.alert_color
92-
.unwrap_or(atty::is(Stream::Stdout)),
98+
.unwrap_or_else(|| atty::is(Stream::Stdout)),
9399
alert_ascii: options.common.alert_ascii,
94100
verbose: options.common.verbose,
95101
quiet_deps: options.common.quiet_deps,
@@ -110,7 +116,7 @@ impl Embedded {
110116
let host = Host::new(importer_registry, logger_registry);
111117
let conn = self.channel.connect(host);
112118
let response = conn.compile_request(request)?;
113-
Ok(CompileResult::try_from(response)?)
119+
CompileResult::try_from(response)
114120
}
115121

116122
pub fn info(&mut self) -> Result<String> {

src/error.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ impl From<ProtocolError> for Exception {
5959
}
6060

6161
impl Exception {
62-
pub fn new(message: String) -> Self {
62+
pub fn new(message: impl Into<String>) -> Self {
6363
Self {
64-
message,
64+
message: message.into(),
6565
sass_message: None,
6666
sass_stack: None,
6767
span: None,

src/host/importer_registry.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,8 @@ impl ImporterRegistry {
8787
) {
8888
Ok(url) => CanonicalizeResponse {
8989
id: request.id,
90-
result: if let Some(url) = url {
91-
Some(canonicalize_response::Result::Url(url.to_string()))
92-
} else {
93-
None
94-
},
90+
result: url
91+
.map(|url| canonicalize_response::Result::Url(url.to_string())),
9592
},
9693
Err(e) => CanonicalizeResponse {
9794
id: request.id,

src/lib.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,23 @@ mod varint;
1212
pub use embedded::{CompileResult, Embedded, Embedded as Sass};
1313
pub use error::{Exception, Result};
1414
pub use options::{
15-
FileImporter, Importer, Logger, LoggerDebugOptions, LoggerWarnOptions,
16-
Options, OptionsBuilder, SassLogger, StringOptions, StringOptionsBuilder,
15+
FileImporter, Importer, ImporterOptions, ImporterResult, Logger,
16+
LoggerDebugOptions, LoggerWarnOptions, Options, OptionsBuilder, SassLogger,
17+
StringOptions, StringOptionsBuilder,
1718
};
1819
pub use protocol::{OutputStyle, SourceSpan, Syntax};
1920
pub use url::{self, Url};
2021

21-
#[cfg(test)]
22-
pub fn exe_path() -> std::path::PathBuf {
23-
std::path::PathBuf::from(concat!(env!("CARGO_MANIFEST_DIR")))
24-
.join("sass_embedded")
25-
.join("dart-sass-embedded")
26-
}
27-
2822
#[cfg(test)]
2923
mod tests {
3024
use super::*;
3125

26+
pub fn exe_path() -> std::path::PathBuf {
27+
std::path::PathBuf::from(concat!(env!("CARGO_MANIFEST_DIR")))
28+
.join("sass_embedded")
29+
.join("dart-sass-embedded")
30+
}
31+
3232
#[test]
3333
fn version_smoke() {
3434
let mut sass = Sass::new(exe_path());

src/options.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -76,18 +76,18 @@ impl OptionsBuilder {
7676
self
7777
}
7878

79-
pub fn load_paths(mut self, arg: Vec<String>) -> Self {
80-
self.options.load_paths = Some(arg);
79+
pub fn load_paths(mut self, arg: impl IntoIterator<Item = String>) -> Self {
80+
self.options.load_paths = Some(arg.into_iter().collect());
8181
self
8282
}
8383

84-
pub fn load_path(mut self, arg: &str) -> Self {
84+
pub fn load_path(mut self, arg: impl Into<String>) -> Self {
8585
let mut load_paths = if let Some(load_paths) = self.options.load_paths {
8686
load_paths
8787
} else {
8888
Vec::new()
8989
};
90-
load_paths.push(arg.to_owned());
90+
load_paths.push(arg.into());
9191
self.options.load_paths = Some(load_paths);
9292
self
9393
}
@@ -228,8 +228,8 @@ impl StringOptionsBuilder {
228228
self
229229
}
230230

231-
pub fn url(mut self, arg: Url) -> Self {
232-
self.url = Some(arg);
231+
pub fn url(mut self, arg: impl Into<Url>) -> Self {
232+
self.url = Some(arg.into());
233233
self
234234
}
235235

@@ -243,18 +243,18 @@ impl StringOptionsBuilder {
243243
self
244244
}
245245

246-
pub fn load_paths(mut self, arg: Vec<String>) -> Self {
247-
self.options.load_paths = Some(arg);
246+
pub fn load_paths(mut self, arg: impl IntoIterator<Item = String>) -> Self {
247+
self.options.load_paths = Some(arg.into_iter().collect());
248248
self
249249
}
250250

251-
pub fn load_path(mut self, arg: &str) -> Self {
251+
pub fn load_path(mut self, arg: impl Into<String>) -> Self {
252252
let mut load_paths = if let Some(load_paths) = self.options.load_paths {
253253
load_paths
254254
} else {
255255
Vec::new()
256256
};
257-
load_paths.push(arg.to_owned());
257+
load_paths.push(arg.into());
258258
self.options.load_paths = Some(load_paths);
259259
self
260260
}
@@ -363,14 +363,14 @@ pub enum SassImporter {
363363
/// https://sass-lang.com/documentation/js-api/interfaces/Importer
364364
pub trait Importer: Debug + Send + Sync {
365365
/// https://sass-lang.com/documentation/js-api/interfaces/Importer#canonicalize
366-
fn canonicalize<'u, 'o>(
366+
fn canonicalize(
367367
&self,
368-
url: &'u str,
369-
options: &'o ImporterOptions,
368+
url: &str,
369+
options: &ImporterOptions,
370370
) -> Result<Option<Url>>;
371371

372372
/// https://sass-lang.com/documentation/js-api/interfaces/Importer#load
373-
fn load<'u>(&self, canonical_url: &'u Url) -> Result<Option<ImporterResult>>;
373+
fn load(&self, canonical_url: &Url) -> Result<Option<ImporterResult>>;
374374
}
375375

376376
pub struct ImporterOptions {
@@ -380,10 +380,10 @@ pub struct ImporterOptions {
380380
/// https://sass-lang.com/documentation/js-api/interfaces/FileImporter
381381
pub trait FileImporter: Debug + Send + Sync {
382382
/// https://sass-lang.com/documentation/js-api/interfaces/FileImporter#findFileUrl
383-
fn find_file_url<'u, 'o>(
383+
fn find_file_url(
384384
&self,
385-
url: &'u str,
386-
options: &'o ImporterOptions,
385+
url: &str,
386+
options: &ImporterOptions,
387387
) -> Result<Option<Url>>;
388388
}
389389

0 commit comments

Comments
 (0)