@@ -128,10 +128,7 @@ impl AmberCompiler {
128
128
meta : & ParserMetadata ,
129
129
) -> Vec < ( String , Block ) > {
130
130
let imports_sorted = meta. import_cache . topological_sort ( ) ;
131
- let imports_blocks = meta
132
- . import_cache
133
- . files
134
- . iter ( )
131
+ let imports_blocks = meta. import_cache . files . iter ( )
135
132
. map ( |file| {
136
133
file. metadata
137
134
. as_ref ( )
@@ -186,32 +183,29 @@ impl AmberCompiler {
186
183
}
187
184
188
185
pub fn document ( & self , block : Block , meta : ParserMetadata , output : String ) {
189
- let base_path = PathBuf :: from (
190
- meta. get_path ( )
191
- . expect ( "Input file must exist in docs generation" ) ,
192
- ) ;
186
+ let base_path = meta. get_path ( )
187
+ . map ( PathBuf :: from)
188
+ . expect ( "Input file must exist in docs generation" ) ;
193
189
let base_dir = fs:: canonicalize ( base_path) . map ( |val| {
194
190
val. parent ( )
195
191
. expect ( "Parent dir must exist in docs generation" )
196
192
. to_owned ( )
197
193
. clone ( )
198
194
} ) ;
199
- if let Err ( err ) = base_dir {
195
+ let base_dir = base_dir. unwrap_or_else ( |err| {
200
196
Message :: new_err_msg ( "Couldn't get the absolute path to the provided input file" )
201
197
. comment ( err. to_string ( ) )
202
198
. show ( ) ;
203
199
std:: process:: exit ( 1 ) ;
204
- }
205
- let base_dir = base_dir. unwrap ( ) ;
200
+ } ) ;
206
201
let ast_forest = self . get_sorted_ast_forest ( block, & meta) ;
207
202
let mut paths = vec ! [ ] ;
208
203
for ( path, block) in ast_forest {
209
204
let dep_path = {
210
- let dep_path = fs:: canonicalize ( PathBuf :: from ( path. clone ( ) ) ) ;
211
- if dep_path. is_err ( ) {
212
- continue ;
213
- }
214
- let dep_path = dep_path. unwrap ( ) ;
205
+ let dep_path = match fs:: canonicalize ( PathBuf :: from ( path) ) {
206
+ Ok ( path) => path,
207
+ Err ( _) => continue ,
208
+ } ;
215
209
216
210
if !dep_path. starts_with ( & base_dir) {
217
211
continue ;
@@ -220,22 +214,23 @@ impl AmberCompiler {
220
214
dep_path
221
215
} ;
222
216
let document = block. document ( & meta) ;
223
- // Save to file
217
+ // Save to file; replace the base directory if the output
218
+ // path is absolute, otherwise append the output path.
224
219
let dir_path = {
225
- let file_dir = dep_path. strip_prefix ( & base_dir) . unwrap ( ) ;
226
- let parent = file_dir . parent ( ) . unwrap ( ) . display ( ) ;
227
- format ! ( "{}/{output}/{}" , base_dir. to_string_lossy ( ) , parent )
220
+ let file_path = dep_path. strip_prefix ( & base_dir) . unwrap ( ) ;
221
+ let file_dir = file_path . parent ( ) . unwrap ( ) ;
222
+ base_dir. join ( & output ) . join ( file_dir )
228
223
} ;
229
224
if let Err ( err) = fs:: create_dir_all ( dir_path. clone ( ) ) {
230
225
Message :: new_err_msg ( format ! (
231
- "Couldn't create directory `{dir_path }`. Do you have sufficient permissions?"
226
+ "Couldn't create directory `{}`. Do you have sufficient permissions?" , dir_path . display ( )
232
227
) )
233
228
. comment ( err. to_string ( ) )
234
229
. show ( ) ;
235
230
std:: process:: exit ( 1 ) ;
236
231
}
237
232
let filename = dep_path. file_stem ( ) . unwrap ( ) . to_string_lossy ( ) ;
238
- let path = PathBuf :: from ( dir_path) . join ( format ! ( "{filename}.md" ) ) ;
233
+ let path = dir_path. join ( format ! ( "{filename}.md" ) ) ;
239
234
let mut file = File :: create ( path. clone ( ) ) . unwrap ( ) ;
240
235
file. write_all ( document. as_bytes ( ) ) . unwrap ( ) ;
241
236
paths. push ( String :: from ( path. to_string_lossy ( ) ) ) ;
0 commit comments