@@ -104,18 +104,22 @@ struct Property {
104
104
/// Create all necessary structures and directories for the crate.
105
105
/// Requires `output` folder to exist.
106
106
///
107
+ /// # Arguments
108
+ ///
109
+ /// - `output_name: &str` - The name of the output library given by the CLI. Default `output/`.
110
+ ///
107
111
/// # Panics
108
112
///
109
113
/// This function panics when the `output/` directory does not exist.
110
- fn create_lib_dir ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
114
+ fn create_lib_dir ( output_name : & str ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
111
115
println ! ( "Starting repackaging into crate..." ) ;
112
116
let source_files = [ "paths.rs" , "types.rs" ] ;
113
117
114
- if !fs:: metadata ( "output" ) . is_ok ( ) {
118
+ if !fs:: metadata ( output_name ) . is_ok ( ) {
115
119
panic ! ( "Fatal: Output directory does not exist!" ) ;
116
120
}
117
121
118
- std:: env:: set_current_dir ( "output" ) ?;
122
+ std:: env:: set_current_dir ( output_name ) ?;
119
123
120
124
for src_file in & source_files {
121
125
if !fs:: metadata ( src_file) . is_ok ( ) {
@@ -126,43 +130,15 @@ fn create_lib_dir() -> Result<(), Box<dyn std::error::Error>> {
126
130
let src_dir = "src" ;
127
131
fs:: create_dir_all ( & src_dir) ?;
128
132
129
- for src_file in & source_files {
133
+ for src_file in source_files {
130
134
let dest_path = format ! ( "{}/{}" , src_dir, src_file) ;
131
135
fs:: rename ( src_file, & dest_path) ?;
132
136
}
133
137
134
- let lib_rs = r#"// Your library code goes here.
135
- #[macro_use]
136
- extern crate serde_derive;
137
-
138
- extern crate serde;
139
- extern crate serde_json;
140
- extern crate url;
141
- extern crate reqwest;
142
-
143
- pub mod paths;
144
- pub mod types;"# ;
145
138
let mut lib_file = fs:: File :: create ( format ! ( "{}/lib.rs" , src_dir) ) ?;
146
- write ! ( lib_file, "{}" , lib_rs ) ?;
139
+ write ! ( lib_file, "{}" , include_str! ( "templates/lib.rs.template" ) ) ?;
147
140
148
- let cargo_toml = r#"[package]
149
- name = "your_library"
150
- version = "0.1.0"
151
- authors = ["Your Name"]
152
- edition = "2021"
153
-
154
- [lib]
155
- path = "src/lib.rs"
156
-
157
- [dependencies]
158
- serde = { version = "1.0.195", features = ["derive"] }
159
- serde_yaml = "0.9.30"
160
- reqwest = { version = "0.*", features = ["blocking", "json"] }
161
-
162
- [[bin]]
163
- name = "your_bin_name"
164
- path = "src/main.rs"
165
- "# ;
141
+ let cargo_toml = format ! ( include_str!( "templates/Cargo.toml.template" ) , output_name) ;
166
142
167
143
fs:: write ( "Cargo.toml" , cargo_toml) ?;
168
144
@@ -271,16 +247,16 @@ fn if_some<F: FnOnce(&T), T>(this: Option<T>, func: F) {
271
247
}
272
248
273
249
/// Generates the Rust bindings from a file.
274
- pub fn gen ( input_path : impl AsRef < std:: path:: Path > ) {
250
+ pub fn gen ( input_path : impl AsRef < std:: path:: Path > , output_name : String ) {
275
251
// Parse the schema.
276
252
let input = std:: fs:: read_to_string ( input_path) . unwrap ( ) ;
277
253
let yaml: Schema = serde_yaml:: from_str ( & input) . unwrap ( ) ;
278
254
279
255
// Generate output folder.
280
- _ = std:: fs:: create_dir ( "output/" ) ;
256
+ _ = std:: fs:: create_dir ( & output_name ) ;
281
257
282
258
// Create and open the output file for structs.
283
- let mut types_file = File :: create ( "output /types.rs") . unwrap ( ) ;
259
+ let mut types_file = File :: create ( output_name . clone ( ) + " /types.rs") . unwrap ( ) ;
284
260
285
261
// For every struct.
286
262
for ( name, comp) in & yaml. components . schemas {
@@ -350,7 +326,7 @@ pub fn gen(input_path: impl AsRef<std::path::Path>) {
350
326
}
351
327
352
328
// Create and open the output file for paths.
353
- let mut paths_file = File :: create ( "output /paths.rs") . unwrap ( ) ;
329
+ let mut paths_file = File :: create ( output_name . clone ( ) + " /paths.rs") . unwrap ( ) ;
354
330
355
331
// For every path.
356
332
for ( name, path) in & yaml. paths {
@@ -381,7 +357,7 @@ pub fn gen(input_path: impl AsRef<std::path::Path>) {
381
357
} ) ;
382
358
}
383
359
384
- _ = match create_lib_dir ( ) {
360
+ _ = match create_lib_dir ( & output_name ) {
385
361
Ok ( ( ) ) => ( ) ,
386
362
Err ( e) => {
387
363
panic ! ( "{}" , e) ;
0 commit comments