3
3
//!
4
4
//! ## Documentation
5
5
//! For a guide style documentation and examples, visit: [https://martian-lang.github.io/martian-rust/](https://martian-lang.github.io/martian-rust/#/)
6
- //!
7
6
8
7
pub use anyhow:: Error ;
9
- use anyhow:: { format_err , Context } ;
8
+ use anyhow:: { ensure , Context , Result } ;
10
9
use backtrace:: Backtrace ;
11
10
use log:: { error, info} ;
12
-
13
11
use std:: collections:: HashMap ;
14
12
use std:: fmt:: Write as FmtWrite ;
15
13
use std:: fs:: File ;
16
14
use std:: io:: Write as IoWrite ;
17
15
use std:: os:: unix:: io:: { FromRawFd , IntoRawFd } ;
18
16
use std:: path:: Path ;
19
-
20
17
use std:: { io, panic} ;
21
18
use time:: format_description:: modifier:: { Day , Hour , Minute , Month , Second , Year } ;
22
19
use time:: format_description:: FormatItem :: Literal ;
23
20
use time:: format_description:: { Component , FormatItem } ;
24
21
use time:: OffsetDateTime ;
22
+ use utils:: current_executable;
25
23
26
24
mod metadata;
27
25
pub use metadata:: * ;
@@ -42,15 +40,15 @@ pub use log::LevelFilter;
42
40
pub use mro:: * ;
43
41
pub mod prelude;
44
42
45
- pub fn initialize ( args : Vec < String > ) -> Result < Metadata , Error > {
43
+ pub fn initialize ( args : Vec < String > ) -> Result < Metadata > {
46
44
let mut md = Metadata :: new ( args) ;
47
45
md. update_jobinfo ( ) ?;
48
46
49
47
Ok ( md)
50
48
}
51
49
52
50
#[ cold]
53
- fn write_errors ( msg : & str , is_assert : bool ) -> Result < ( ) , Error > {
51
+ fn write_errors ( msg : & str , is_assert : bool ) -> Result < ( ) > {
54
52
let mut err_file: File = unsafe { File :: from_raw_fd ( 4 ) } ;
55
53
56
54
// We want to aggressively avoid allocations here if we can, since one
@@ -79,7 +77,7 @@ fn write_errors(msg: &str, is_assert: bool) -> Result<(), Error> {
79
77
// We could use the proc macro, but then we'd need
80
78
// to compile the proc macro crate, which would slow down build times
81
79
// significantly for very little benefit in readability.
82
- pub ( crate ) const DATE_FORMAT : & [ FormatItem ] = & [
80
+ pub ( crate ) const DATE_FORMAT : & [ FormatItem < ' _ > ] = & [
83
81
FormatItem :: Component ( Component :: Year ( Year :: default ( ) ) ) ,
84
82
Literal ( b"-" ) ,
85
83
FormatItem :: Component ( Component :: Month ( Month :: default ( ) ) ) ,
@@ -195,13 +193,13 @@ fn martian_entry_point<S: std::hash::BuildHasher>(
195
193
} ;
196
194
197
195
// Get the stage implementation
198
- let _stage = stage_map. get ( & md. stage_name ) . ok_or_else (
196
+ let stage = stage_map. get ( & md. stage_name ) . with_context (
199
197
#[ cold]
200
- || format_err ! ( "Couldn't find requested Martian stage: {}" , md. stage_name) ,
198
+ || format ! ( "Couldn't find requested Martian stage: {}" , md. stage_name) ,
201
199
) ;
202
200
203
201
// special handler for non-existent stage
204
- let stage = match _stage {
202
+ let stage = match stage {
205
203
Ok ( s) => s,
206
204
Err ( e) => {
207
205
let _ = write_errors ( & format ! ( "{e:?}" ) , false ) ;
@@ -286,48 +284,43 @@ fn report_error(md: &mut Metadata, e: &Error, is_assert: bool) {
286
284
let _ = write_errors ( & format ! ( "{e:#}" ) , is_assert) ;
287
285
}
288
286
287
+ /// Return the environment variable CARGO_PKG_NAME or the current executable name.
289
288
fn get_generator_name ( ) -> String {
290
- std:: env:: var ( "CARGO_BIN_NAME" )
291
- . or_else ( |_| std:: env:: var ( "CARGO_CRATE_NAME" ) )
292
- . or_else ( |_| std:: env:: var ( "CARGO_PKG_NAME" ) )
293
- . unwrap_or_else ( |_| {
294
- option_env ! ( "CARGO_BIN_NAME" )
295
- . or ( option_env ! ( "CARGO_CRATE_NAME" ) )
296
- . unwrap_or ( "martian-rust" )
297
- . into ( )
298
- } )
289
+ std:: env:: var ( "CARGO_PKG_NAME" ) . unwrap_or_else ( |_| current_executable ( ) )
299
290
}
300
291
292
+ /// Write MRO to filename or stdout.
301
293
pub fn martian_make_mro (
302
294
header_comment : & str ,
303
- file_name : Option < impl AsRef < Path > > ,
295
+ filename : Option < impl AsRef < Path > > ,
304
296
rewrite : bool ,
305
297
mro_registry : Vec < StageMro > ,
306
- ) -> Result < ( ) , Error > {
307
- if let Some ( ref f) = file_name {
308
- let file_path = f. as_ref ( ) ;
309
- if file_path. is_dir ( ) {
310
- return Err ( format_err ! (
311
- "Error! Path {} is a directory!" ,
312
- file_path. display( )
313
- ) ) ;
314
- }
315
- if file_path. exists ( ) && !rewrite {
316
- return Err ( format_err ! (
317
- "File {} exists. You need to explicitly mention if it is okay to rewrite." ,
318
- file_path. display( )
319
- ) ) ;
320
- }
298
+ ) -> Result < ( ) > {
299
+ if let Some ( filename) = & filename {
300
+ let filename = filename. as_ref ( ) ;
301
+ ensure ! (
302
+ !filename. is_dir( ) ,
303
+ "Path {} is a directory" ,
304
+ filename. display( )
305
+ ) ;
306
+ ensure ! (
307
+ rewrite || !filename. exists( ) ,
308
+ "File {} exists. Use --rewrite to overwrite it." ,
309
+ filename. display( )
310
+ ) ;
321
311
}
322
312
323
- let final_mro_string = make_mro_string ( header_comment, & mro_registry) ;
324
- match file_name {
325
- Some ( f) => {
326
- let mut output = File :: create ( f) ?;
327
- output. write_all ( final_mro_string. as_bytes ( ) ) ?;
313
+ let mro = make_mro_string ( header_comment, & mro_registry) ;
314
+ match filename {
315
+ Some ( filename) => {
316
+ let filename = filename. as_ref ( ) ;
317
+ File :: create ( filename)
318
+ . with_context ( || filename. display ( ) . to_string ( ) ) ?
319
+ . write_all ( mro. as_bytes ( ) )
320
+ . with_context ( || filename. display ( ) . to_string ( ) ) ?;
328
321
}
329
322
None => {
330
- print ! ( "{final_mro_string }" ) ;
323
+ print ! ( "{mro }" ) ;
331
324
}
332
325
}
333
326
Ok ( ( ) )
@@ -361,8 +354,7 @@ pub fn make_mro_string(header_comment: &str, mro_registry: &[StageMro]) -> Strin
361
354
header_comment
362
355
. lines( )
363
356
. all( |line| line. trim_end( ) . is_empty( ) || line. starts_with( '#' ) ) ,
364
- "All non-empty header lines must start with '#', but got\n {}" ,
365
- header_comment
357
+ "All non-empty header lines must start with '#', but got\n {header_comment}"
366
358
) ;
367
359
format ! (
368
360
"{}
0 commit comments