@@ -19,6 +19,7 @@ use cargo::{
19
19
} ;
20
20
use cargo_platform:: Platform ;
21
21
use colorify:: colorify;
22
+ use failure:: { Error , ResultExt } ;
22
23
use semver:: { Version , VersionReq } ;
23
24
use tera:: Tera ;
24
25
@@ -33,24 +34,34 @@ mod template;
33
34
type Feature < ' a > = & ' a str ;
34
35
type PackageName < ' a > = & ' a str ;
35
36
type RootFeature < ' a > = ( PackageName < ' a > , Feature < ' a > ) ;
37
+ type Result < T > = std:: result:: Result < T , Error > ;
36
38
37
39
const VERSION_ATTRIBUTE_NAME : & str = "cargo2nixVersion" ;
38
40
39
41
fn main ( ) {
40
42
let args: Vec < String > = std:: env:: args ( ) . collect ( ) ;
41
43
let args: Vec < & str > = args. iter ( ) . map ( AsRef :: as_ref) . collect ( ) ;
44
+ if let Err ( err) = try_main ( & args) {
45
+ eprintln ! ( "{}" , err) ;
46
+ std:: process:: exit ( 1 ) ;
47
+ }
48
+ }
42
49
50
+ fn try_main ( args : & [ & str ] ) -> Result < ( ) > {
43
51
match & args[ 1 ..] {
44
52
[ "--stdout" ] | [ "-s" ] => generate_cargo_nix ( io:: stdout ( ) . lock ( ) ) ,
45
53
[ "--file" ] | [ "-f" ] => write_to_file ( "Cargo.nix" ) ,
46
54
[ "--file" , file] | [ "-f" , file] => write_to_file ( file) ,
47
55
[ "--help" ] | [ "-h" ] => print_help ( ) ,
48
- [ "--version" ] | [ "-v" ] => println ! ( "{}" , version( ) ) ,
56
+ [ "--version" ] | [ "-v" ] => {
57
+ println ! ( "{}" , version( ) ) ;
58
+ Ok ( ( ) )
59
+ }
49
60
[ ] => print_help ( ) ,
50
61
_ => {
51
62
println ! ( "Invalid arguments: {:?}" , & args[ 1 ..] ) ;
52
63
println ! ( "\n Try again, with help: \n " ) ;
53
- print_help ( ) ;
64
+ print_help ( )
54
65
}
55
66
}
56
67
}
@@ -93,17 +104,18 @@ fn version_req(path: &Path) -> (VersionReq, Version) {
93
104
)
94
105
}
95
106
96
- fn print_help ( ) {
107
+ fn print_help ( ) -> Result < ( ) > {
97
108
println ! ( "cargo2nix-{}\n " , version( ) ) ;
98
109
println ! ( "$ cargo2nix # Print the help" ) ;
99
110
println ! ( "$ cargo2nix -s,--stdout # Output to stdout" ) ;
100
111
println ! ( "$ cargo2nix -f,--file # Output to Cargo.nix" ) ;
101
112
println ! ( "$ cargo2nix -f,--file <file> # Output to the given file" ) ;
102
113
println ! ( "$ cargo2nix -v,--version # Print version of cargo2nix" ) ;
103
114
println ! ( "$ cargo2nix -h,--help # Print the help" ) ;
115
+ Ok ( ( ) )
104
116
}
105
117
106
- fn write_to_file ( file : impl AsRef < Path > ) {
118
+ fn write_to_file ( file : impl AsRef < Path > ) -> Result < ( ) > {
107
119
let path = file. as_ref ( ) ;
108
120
if path. exists ( ) {
109
121
let ( vers_req, ver) = version_req ( path) ;
@@ -121,7 +133,7 @@ fn write_to_file(file: impl AsRef<Path>) {
121
133
colorify!( red: "Please upgrade your cargo2nix ({}) to proceed.\n " ) ,
122
134
vers_req
123
135
) ;
124
- return ;
136
+ return Ok ( ( ) ) ;
125
137
}
126
138
println ! (
127
139
colorify!( green_bold: "\n Version {} matches the requirement {} [{}]\n " ) ,
@@ -140,22 +152,24 @@ fn write_to_file(file: impl AsRef<Path>) {
140
152
. expect ( "failed to read input" ) ;
141
153
if line. trim ( ) != "yes" {
142
154
println ! ( "aborted!" ) ;
143
- return ;
155
+ return Ok ( ( ) ) ;
144
156
}
145
157
}
146
158
147
159
let mut temp_file = tempfile:: Builder :: new ( )
148
160
. tempfile ( )
149
161
. expect ( "could not create new temporary file" ) ;
150
162
151
- generate_cargo_nix ( & mut temp_file) ;
163
+ generate_cargo_nix ( & mut temp_file) ? ;
152
164
153
165
temp_file
154
166
. persist ( path)
155
- . unwrap_or_else ( |e| panic ! ( "could not write file to {}: {}" , path. display( ) , e) ) ;
167
+ . with_context ( |e| format ! ( "could not write file to {}: {}" , path. display( ) , e) ) ?;
168
+
169
+ Ok ( ( ) )
156
170
}
157
171
158
- fn generate_cargo_nix ( mut out : impl io:: Write ) {
172
+ fn generate_cargo_nix ( mut out : impl io:: Write ) -> Result < ( ) > {
159
173
let config = {
160
174
let mut c = cargo:: Config :: default ( ) . unwrap ( ) ;
161
175
c. configure ( 0 , None , & None , false , true , false , & None , & [ ] )
@@ -208,7 +222,8 @@ fn generate_cargo_nix(mut out: impl io::Write) {
208
222
)
209
223
. expect ( "error adding template" ) ;
210
224
let context = tera:: Context :: from_serialize ( plan) . unwrap ( ) ;
211
- write ! ( out, "{}" , tera. render( "Cargo.nix.tera" , & context) . unwrap( ) ) . expect ( "write error" )
225
+ write ! ( out, "{}" , tera. render( "Cargo.nix.tera" , & context) . unwrap( ) ) ?;
226
+ Ok ( ( ) )
212
227
}
213
228
214
229
fn simplify_optionality < ' a , ' b : ' a > (
@@ -525,7 +540,7 @@ fn display_root_feature((pkg_name, feature): RootFeature) -> String {
525
540
format ! ( "{}/{}" , pkg_name, feature)
526
541
}
527
542
528
- fn prefetch_git ( url : & str , rev : & str ) -> Result < String , Box < dyn std :: error :: Error > > {
543
+ fn prefetch_git ( url : & str , rev : & str ) -> Result < String > {
529
544
use std:: process:: { Command , Output } ;
530
545
531
546
let Output {
@@ -539,16 +554,16 @@ fn prefetch_git(url: &str, rev: &str) -> Result<String, Box<dyn std::error::Erro
539
554
. output ( ) ?;
540
555
541
556
if status. success ( ) {
542
- Ok ( serde_json:: from_slice :: < serde_json:: Value > ( & stdout) ?
557
+ serde_json:: from_slice :: < serde_json:: Value > ( & stdout) ?
543
558
. get ( "sha256" )
544
559
. and_then ( |v| v. as_str ( ) )
545
560
. map ( |s| s. to_string ( ) )
546
- . ok_or ( "unexpected JSON output" ) ? )
561
+ . ok_or_else ( || failure :: format_err! ( "unexpected JSON output" ) )
547
562
} else {
548
- Err ( format ! (
563
+ Err ( failure :: format_err !(
549
564
"process failed with stderr {:?}" ,
550
565
String :: from_utf8( stderr)
551
- ) ) ?
566
+ ) )
552
567
}
553
568
}
554
569
0 commit comments