@@ -2,16 +2,19 @@ use std::ffi::OsStr;
2
2
3
3
use crate :: {
4
4
channel:: Channel ,
5
- importer_registry :: ImporterRegistry ,
6
- logger_registry :: LoggerRegistry ,
5
+ host :: ImporterRegistry ,
6
+ host :: { Host , LoggerRegistry } ,
7
7
protocol:: {
8
- inbound_message:: { compile_request:: Input , CompileRequest } ,
8
+ inbound_message:: {
9
+ compile_request:: { Input , StringInput } ,
10
+ CompileRequest ,
11
+ } ,
9
12
outbound_message:: {
10
13
compile_response:: { self , CompileSuccess } ,
11
14
CompileResponse ,
12
15
} ,
13
16
} ,
14
- Exception , Options , Result ,
17
+ Exception , Options , Result , StringOptions ,
15
18
} ;
16
19
17
20
#[ derive( Debug ) ]
@@ -29,13 +32,17 @@ impl Embedded {
29
32
pub fn compile (
30
33
& mut self ,
31
34
path : impl Into < String > ,
32
- mut options : Options ,
35
+ options : Options ,
33
36
) -> Result < CompileResult > {
34
- let logger_registry = LoggerRegistry :: new ( options. logger . take ( ) ) ;
35
- let importer_registry = ImporterRegistry :: new (
36
- options. importers . take ( ) ,
37
- options. load_paths . take ( ) ,
38
- ) ;
37
+ let mut logger_registry = LoggerRegistry :: default ( ) ;
38
+ let mut importer_registry = ImporterRegistry :: default ( ) ;
39
+ let importers = importer_registry
40
+ . register_all (
41
+ options. importers . unwrap_or_default ( ) ,
42
+ options. load_paths . unwrap_or_default ( ) ,
43
+ )
44
+ . collect ( ) ;
45
+ options. logger . map ( |l| logger_registry. register ( l) ) ;
39
46
40
47
let request = CompileRequest {
41
48
style : options. style as i32 ,
@@ -46,20 +53,66 @@ impl Embedded {
46
53
quiet_deps : options. quiet_deps ,
47
54
source_map_include_sources : options. source_map_include_sources ,
48
55
charset : options. charset ,
49
- importers : importer_registry . importers ( ) ,
56
+ importers,
50
57
input : Some ( Input :: Path ( path. into ( ) ) ) ,
58
+ // id: set in compile_request
59
+ // global_functions: not implemented
60
+ ..Default :: default ( )
61
+ } ;
62
+
63
+ let host = Host :: new ( importer_registry, logger_registry) ;
64
+ let conn = self . channel . connect ( host) ;
65
+ let response = conn. compile_request ( request) ?;
66
+ Ok ( CompileResult :: try_from ( response) ?)
67
+ }
68
+
69
+ pub fn compile_string (
70
+ & mut self ,
71
+ source : impl Into < String > ,
72
+ options : StringOptions ,
73
+ ) -> Result < CompileResult > {
74
+ let mut logger_registry = LoggerRegistry :: default ( ) ;
75
+ let mut importer_registry = ImporterRegistry :: default ( ) ;
76
+ let importers = importer_registry
77
+ . register_all (
78
+ options. common . importers . unwrap_or_default ( ) ,
79
+ options. common . load_paths . unwrap_or_default ( ) ,
80
+ )
81
+ . collect ( ) ;
82
+ options. common . logger . map ( |l| logger_registry. register ( l) ) ;
83
+
84
+ let request = CompileRequest {
85
+ style : options. common . style as i32 ,
86
+ source_map : options. common . source_map ,
87
+ alert_color : options. common . alert_color ,
88
+ alert_ascii : options. common . alert_ascii ,
89
+ verbose : options. common . verbose ,
90
+ quiet_deps : options. common . quiet_deps ,
91
+ source_map_include_sources : options. common . source_map_include_sources ,
92
+ charset : options. common . charset ,
93
+ importers,
94
+ input : Some ( Input :: String ( StringInput {
95
+ source : source. into ( ) ,
96
+ url : options. url . map ( |url| url. to_string ( ) ) . unwrap_or_default ( ) ,
97
+ syntax : options. syntax as i32 ,
98
+ importer : options. importer . map ( |i| importer_registry. register ( i) ) ,
99
+ } ) ) ,
100
+ // id: set in compile_request
101
+ // global_functions: not implemented
51
102
..Default :: default ( )
52
103
} ;
53
104
54
- let conn = self
55
- . channel
56
- . connect ( Some ( logger_registry) , Some ( importer_registry) ) ;
105
+ let host = Host :: new ( importer_registry, logger_registry) ;
106
+ let conn = self . channel . connect ( host) ;
57
107
let response = conn. compile_request ( request) ?;
58
108
Ok ( CompileResult :: try_from ( response) ?)
59
109
}
60
110
61
111
pub fn info ( & mut self ) -> Result < String > {
62
- let conn = self . channel . connect ( None , None ) ;
112
+ let logger_registry = LoggerRegistry :: default ( ) ;
113
+ let importer_registry = ImporterRegistry :: default ( ) ;
114
+ let host = Host :: new ( importer_registry, logger_registry) ;
115
+ let conn = self . channel . connect ( host) ;
63
116
let response = conn. version_request ( ) ?;
64
117
Ok ( format ! (
65
118
"sass-embedded\t #{}" ,
0 commit comments