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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
use std::{ffi::OsStr, path::Path};
use atty::Stream;
use crate::{
channel::Channel,
host::ImporterRegistry,
host::{Host, LoggerRegistry},
protocol::{
self,
inbound_message::{
compile_request::{Input, StringInput},
CompileRequest,
},
},
CompileResult, Options, Result, StringOptions,
};
#[cfg(feature = "legacy")]
use crate::{
legacy::LEGACY_IMPORTER_PROTOCOL, protocol::inbound_message::compile_request,
};
#[derive(Debug)]
pub struct Embedded {
channel: Channel,
}
impl Embedded {
pub fn new(exe_path: impl AsRef<OsStr>) -> Result<Self> {
Ok(Self {
channel: Channel::new(exe_path)?,
})
}
pub fn compile(
&mut self,
path: impl AsRef<Path>,
options: Options,
) -> Result<CompileResult> {
let mut logger_registry = LoggerRegistry::default();
let mut importer_registry = ImporterRegistry::default();
let importers = importer_registry
.register_all(options.importers, options.load_paths)
.collect();
if let Some(l) = options.logger {
logger_registry.register(l);
}
let request = CompileRequest {
style: protocol::OutputStyle::from(options.style) as i32,
source_map: options.source_map,
alert_color: options
.alert_color
.unwrap_or_else(|| atty::is(Stream::Stdout)),
alert_ascii: options.alert_ascii,
verbose: options.verbose,
quiet_deps: options.quiet_deps,
source_map_include_sources: options.source_map_include_sources,
charset: options.charset,
importers,
input: Some(Input::Path(path.as_ref().to_str().unwrap().to_string())),
..Default::default()
};
let host = Host::new(importer_registry, logger_registry);
let conn = self.channel.connect(host)?;
let response = conn.compile_request(request)?;
CompileResult::try_from(response)
}
pub fn compile_string(
&mut self,
source: impl Into<String>,
options: StringOptions,
) -> Result<CompileResult> {
let mut logger_registry = LoggerRegistry::default();
let mut importer_registry = ImporterRegistry::default();
let importers = importer_registry
.register_all(options.common.importers, options.common.load_paths)
.collect();
if let Some(l) = options.common.logger {
logger_registry.register(l);
}
#[cfg(feature = "legacy")]
let importer = if let Some(input_importer) = options.input_importer {
Some(importer_registry.register(input_importer))
} else if matches!(&options.url, Some(u) if u.to_string() == LEGACY_IMPORTER_PROTOCOL)
{
Some(compile_request::Importer {
importer: Some(compile_request::importer::Importer::Path(
std::env::current_dir()
.unwrap()
.to_str()
.unwrap()
.to_string(),
)),
})
} else {
None
};
#[cfg(feature = "legacy")]
let url = options
.url
.map(|url| url.to_string())
.filter(|url| url != LEGACY_IMPORTER_PROTOCOL)
.unwrap_or_default();
#[cfg(not(feature = "legacy"))]
let importer = options
.input_importer
.map(|i| importer_registry.register(i));
#[cfg(not(feature = "legacy"))]
let url = options.url.map(|url| url.to_string()).unwrap_or_default();
let request = CompileRequest {
style: protocol::OutputStyle::from(options.common.style) as i32,
source_map: options.common.source_map,
alert_color: options
.common
.alert_color
.unwrap_or_else(|| atty::is(Stream::Stdout)),
alert_ascii: options.common.alert_ascii,
verbose: options.common.verbose,
quiet_deps: options.common.quiet_deps,
source_map_include_sources: options.common.source_map_include_sources,
charset: options.common.charset,
importers,
input: Some(Input::String(StringInput {
source: source.into(),
url,
syntax: protocol::Syntax::from(options.syntax) as i32,
importer,
})),
..Default::default()
};
let host = Host::new(importer_registry, logger_registry);
let conn = self.channel.connect(host)?;
let response = conn.compile_request(request)?;
CompileResult::try_from(response)
}
pub fn info(&mut self) -> Result<String> {
let logger_registry = LoggerRegistry::default();
let importer_registry = ImporterRegistry::default();
let host = Host::new(importer_registry, logger_registry);
let conn = self.channel.connect(host)?;
let response = conn.version_request()?;
Ok(format!(
"sass-embedded\t#{}",
response.implementation_version
))
}
}