@@ -4,40 +4,81 @@ use crate::{
4
4
compiler_path,
5
5
importer_registry:: ImporterRegistry ,
6
6
logger_registry:: LoggerRegistry ,
7
- pb:: { inbound_message:: CompileRequest , outbound_message:: compile_response} ,
7
+ pb:: {
8
+ inbound_message:: CompileRequest ,
9
+ outbound_message:: { compile_response, CompileResponse } ,
10
+ } ,
8
11
Error ,
9
12
} ;
10
13
14
+ pub fn compile_sync (
15
+ path : String ,
16
+ mut options : Options ,
17
+ ) -> Result < CompileResult > {
18
+ let exe = exe_path ( & options) ;
19
+ let mut importers =
20
+ ImporterRegistry :: new ( options. importers . take ( ) , options. load_paths . take ( ) ) ;
21
+ let logger = LoggerRegistry :: new ( options. logger . take ( ) ) ;
22
+
23
+ let request = CompileRequest :: with_path ( path, & mut importers, & options) ;
24
+ let rt = tokio:: runtime:: Runtime :: new ( ) . unwrap ( ) ;
25
+ let response = rt. block_on ( async {
26
+ let embedded = Embedded :: new ( exe) ;
27
+ let res = embedded. compile ( request, & importers, & logger) . await ?;
28
+ Ok :: < CompileResponse , Error > ( res)
29
+ } ) ?;
30
+
31
+ handle_response ( response)
32
+ }
33
+
34
+ pub fn compile_string_sync (
35
+ source : String ,
36
+ mut options : Options ,
37
+ string_options : StringOptions ,
38
+ ) -> Result < CompileResult > {
39
+ let exe = exe_path ( & options) ;
40
+ let mut importers =
41
+ ImporterRegistry :: new ( options. importers . take ( ) , options. load_paths . take ( ) ) ;
42
+ let logger = LoggerRegistry :: new ( options. logger . take ( ) ) ;
43
+
44
+ let request = CompileRequest :: with_string (
45
+ source,
46
+ & mut importers,
47
+ & options,
48
+ string_options,
49
+ ) ;
50
+ let rt = tokio:: runtime:: Runtime :: new ( ) . unwrap ( ) ;
51
+ let response = rt. block_on ( async {
52
+ let embedded = Embedded :: new ( exe) ;
53
+ let res = embedded. compile ( request, & importers, & logger) . await ?;
54
+ Ok :: < CompileResponse , Error > ( res)
55
+ } ) ?;
56
+
57
+ handle_response ( response)
58
+ }
59
+
11
60
pub async fn compile (
12
61
path : String ,
13
62
mut options : Options ,
14
63
) -> Result < CompileResult > {
64
+ let exe = exe_path ( & options) ;
15
65
let mut importers =
16
66
ImporterRegistry :: new ( options. importers . take ( ) , options. load_paths . take ( ) ) ;
17
67
let logger = LoggerRegistry :: new ( options. logger . take ( ) ) ;
18
68
19
69
let request = CompileRequest :: with_path ( path, & mut importers, & options) ;
20
- let embedded = Embedded :: new ( compiler_path :: compiler_path ( ) . unwrap ( ) ) ;
70
+ let embedded = Embedded :: new ( exe ) ;
21
71
let response = embedded. compile ( request, & importers, & logger) . await ?;
22
72
23
- let res = response. result . ok_or_else ( || {
24
- Error :: Compile (
25
- "OutboundMessage.CompileResponse.result is not set" . to_string ( ) ,
26
- )
27
- } ) ?;
28
- match res {
29
- compile_response:: Result :: Success ( success) => Ok ( success. into ( ) ) ,
30
- compile_response:: Result :: Failure ( failure) => {
31
- Err ( Exception :: new ( failure) . into ( ) )
32
- }
33
- }
73
+ handle_response ( response)
34
74
}
35
75
36
76
pub async fn compile_string (
37
77
source : String ,
38
78
mut options : Options ,
39
79
string_options : StringOptions ,
40
80
) -> Result < CompileResult > {
81
+ let exe = exe_path ( & options) ;
41
82
let mut importers =
42
83
ImporterRegistry :: new ( options. importers . take ( ) , options. load_paths . take ( ) ) ;
43
84
let logger = LoggerRegistry :: new ( options. logger . take ( ) ) ;
@@ -48,9 +89,21 @@ pub async fn compile_string(
48
89
& options,
49
90
string_options,
50
91
) ;
51
- let embedded = Embedded :: new ( compiler_path :: compiler_path ( ) . unwrap ( ) ) ;
92
+ let embedded = Embedded :: new ( exe ) ;
52
93
let response = embedded. compile ( request, & importers, & logger) . await ?;
53
94
95
+ handle_response ( response)
96
+ }
97
+
98
+ fn exe_path ( options : & Options ) -> String {
99
+ options
100
+ . exe_path
101
+ . as_ref ( )
102
+ . unwrap_or ( & compiler_path:: compiler_path ( ) . unwrap ( ) )
103
+ . to_string ( )
104
+ }
105
+
106
+ fn handle_response ( response : CompileResponse ) -> Result < CompileResult > {
54
107
let res = response. result . ok_or_else ( || {
55
108
Error :: Compile (
56
109
"OutboundMessage.CompileResponse.result is not set" . to_string ( ) ,
@@ -79,6 +132,17 @@ mod tests {
79
132
)
80
133
. await
81
134
. unwrap ( ) ;
82
- dbg ! ( res) ;
135
+ assert_eq ! ( res. css, ".foo {\n a: b;\n }" ) ;
136
+ }
137
+
138
+ #[ test]
139
+ fn test_compile_string_sync ( ) {
140
+ let res = compile_string_sync (
141
+ ".foo {a: b}" . to_string ( ) ,
142
+ Options :: default ( ) ,
143
+ StringOptions :: WithoutImporter ( WithoutImporter :: default ( ) ) ,
144
+ )
145
+ . unwrap ( ) ;
146
+ assert_eq ! ( res. css, ".foo {\n a: b;\n }" ) ;
83
147
}
84
148
}
0 commit comments