Skip to content

Commit b84a070

Browse files
author
ByteOtter
committed
Add template files, fix standard input
1 parent 9cc230b commit b84a070

File tree

4 files changed

+47
-40
lines changed

4 files changed

+47
-40
lines changed

src/bindgen.rs

+15-39
Original file line numberDiff line numberDiff line change
@@ -104,18 +104,22 @@ struct Property {
104104
/// Create all necessary structures and directories for the crate.
105105
/// Requires `output` folder to exist.
106106
///
107+
/// # Arguments
108+
///
109+
/// - `output_name: &str` - The name of the output library given by the CLI. Default `output/`.
110+
///
107111
/// # Panics
108112
///
109113
/// 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>> {
111115
println!("Starting repackaging into crate...");
112116
let source_files = ["paths.rs", "types.rs"];
113117

114-
if !fs::metadata("output").is_ok() {
118+
if !fs::metadata(output_name).is_ok() {
115119
panic!("Fatal: Output directory does not exist!");
116120
}
117121

118-
std::env::set_current_dir("output")?;
122+
std::env::set_current_dir(output_name)?;
119123

120124
for src_file in &source_files {
121125
if !fs::metadata(src_file).is_ok() {
@@ -126,43 +130,15 @@ fn create_lib_dir() -> Result<(), Box<dyn std::error::Error>> {
126130
let src_dir = "src";
127131
fs::create_dir_all(&src_dir)?;
128132

129-
for src_file in &source_files {
133+
for src_file in source_files {
130134
let dest_path = format!("{}/{}", src_dir, src_file);
131135
fs::rename(src_file, &dest_path)?;
132136
}
133137

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;"#;
145138
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"))?;
147140

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);
166142

167143
fs::write("Cargo.toml", cargo_toml)?;
168144

@@ -271,16 +247,16 @@ fn if_some<F: FnOnce(&T), T>(this: Option<T>, func: F) {
271247
}
272248

273249
/// 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) {
275251
// Parse the schema.
276252
let input = std::fs::read_to_string(input_path).unwrap();
277253
let yaml: Schema = serde_yaml::from_str(&input).unwrap();
278254

279255
// Generate output folder.
280-
_ = std::fs::create_dir("output/");
256+
_ = std::fs::create_dir(&output_name);
281257

282258
// 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();
284260

285261
// For every struct.
286262
for (name, comp) in &yaml.components.schemas {
@@ -350,7 +326,7 @@ pub fn gen(input_path: impl AsRef<std::path::Path>) {
350326
}
351327

352328
// 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();
354330

355331
// For every path.
356332
for (name, path) in &yaml.paths {
@@ -381,7 +357,7 @@ pub fn gen(input_path: impl AsRef<std::path::Path>) {
381357
});
382358
}
383359

384-
_ = match create_lib_dir() {
360+
_ = match create_lib_dir(&output_name) {
385361
Ok(()) => (),
386362
Err(e) => {
387363
panic!("{}", e);

src/main.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ struct Args {
99
/// Path to a YAML schema file.
1010
#[arg(short, long)]
1111
input_file: Option<String>,
12+
/// Name of the output package (Default 'output')
13+
#[arg(short, long)]
14+
name: Option<String>,
1215
}
1316

1417
fn main() {
@@ -32,7 +35,7 @@ fn main() {
3235
);
3336

3437
match args.input_file {
35-
Some(file) => bindgen::gen(file),
38+
Some(file) => bindgen::gen(file, args.name.unwrap_or("output".to_owned())),
3639
None => println!("Error: You need to provide a YAML schema to generate from."),
3740
}
3841
}

src/templates/Cargo.toml.template

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[package]
2+
name = "{}"
3+
version = "0.1.0"
4+
authors = ["Your Name"]
5+
edition = "2021"
6+
7+
[lib]
8+
path = "src/lib.rs"
9+
10+
[dependencies]
11+
serde = {{ version = ">1.0.195", features = ["derive"] }}
12+
serde_yaml = ">0.9.30"
13+
reqwest = {{ version = ">0.*", features = ["blocking", "json"] }}
14+
15+
[[bin]]
16+
name = "your_bin_name"
17+
path = "src/main.rs"

src/templates/lib.rs.template

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Your library code goes here.
2+
#[macro_use]
3+
extern crate serde_derive;
4+
5+
extern crate serde;
6+
extern crate serde_json;
7+
extern crate url;
8+
extern crate reqwest;
9+
10+
pub mod paths;
11+
pub mod types;

0 commit comments

Comments
 (0)