diff --git a/src/lib.rs b/src/lib.rs index a25b799..c1a179a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,19 +12,23 @@ // See the License for the specific language governing permissions and // limitations under the License. +use std::fs::File; +use std::io::BufReader; +use std::path::{Path, PathBuf}; +use std::result; + +use openapiv3::OpenAPI; + +pub use merger::merge_all_openapi_specs; + use crate::rust::lib_gen::{Module, ModuleDef, ModuleName}; use crate::rust::model_gen::RefCache; -use openapiv3::OpenAPI; -use std::path::Path; -use std::result; pub(crate) mod merger; pub(crate) mod printer; mod rust; mod toml; -pub use merger::merge_all_openapi_specs; - #[derive(Debug, Clone)] pub enum Error { Unexpected { message: String }, @@ -57,7 +61,13 @@ impl Error { pub type Result = result::Result; -pub fn gen(openapi_specs: Vec, target: &Path, name: &str, version: &str) -> Result<()> { +pub fn gen( + openapi_specs: Vec, + target: &Path, + name: &str, + version: &str, + overwrite_cargo: bool, +) -> Result<()> { let open_api = merge_all_openapi_specs(openapi_specs)?; let src = target.join("src"); @@ -138,8 +148,23 @@ pub fn gen(openapi_specs: Vec, target: &Path, name: &str, version: &str let lib = rust::lib_gen::lib_gen("crate", &module_defs); std::fs::write(src.join("lib.rs"), lib).unwrap(); - let cargo = toml::cargo::gen(name, version); - std::fs::write(target.join("Cargo.toml"), cargo).unwrap(); + if overwrite_cargo { + let cargo = toml::cargo::gen(name, version); + std::fs::write(target.join("Cargo.toml"), cargo).unwrap(); + } Ok(()) } + +pub fn parse_openapi_specs( + spec: &[PathBuf], +) -> std::result::Result, Box> { + spec.iter() + .map(|spec_path| { + let file = File::open(spec_path)?; + let reader = BufReader::new(file); + let openapi: OpenAPI = serde_yaml::from_reader(reader)?; + Ok(openapi) + }) + .collect() +} diff --git a/src/main.rs b/src/main.rs index 874599f..ca6b5e4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,13 +12,13 @@ // See the License for the specific language governing permissions and // limitations under the License. -use clap::{Args, Parser}; -use golem_openapi_client_generator::gen; -use openapiv3::OpenAPI; use std::fs::File; -use std::io::BufReader; use std::path::PathBuf; +use clap::{Args, Parser}; + +use golem_openapi_client_generator::{gen, parse_openapi_specs}; + #[derive(Parser, Debug)] #[command(author, version, about, long_about = None, rename_all = "kebab-case")] enum Cli { @@ -56,17 +56,18 @@ fn main() { match command { Cli::Generate(args) => { - let openapi_specs = parse_openapi_specs(args.spec_yaml).unwrap(); + let openapi_specs = parse_openapi_specs(&args.spec_yaml).unwrap(); gen( openapi_specs, &args.output_directory, &args.name, &args.client_version, + true, ) .unwrap(); } Cli::Merge(args) => { - let openapi_specs = parse_openapi_specs(args.spec_yaml).unwrap(); + let openapi_specs = parse_openapi_specs(&args.spec_yaml).unwrap(); let openapi = golem_openapi_client_generator::merge_all_openapi_specs(openapi_specs).unwrap(); let file = File::create(&args.output_yaml).unwrap(); @@ -74,14 +75,3 @@ fn main() { } } } - -fn parse_openapi_specs(spec: Vec) -> Result, Box> { - spec.iter() - .map(|spec_path| { - let file = File::open(spec_path)?; - let reader = BufReader::new(file); - let openapi: OpenAPI = serde_yaml::from_reader(reader)?; - Ok(openapi) - }) - .collect() -}