Skip to content

Commit 9cc5985

Browse files
committed
Expose OpenAPI parsing and ability to not overwrite Cargo.toml
1 parent 9c5b914 commit 9cc5985

File tree

2 files changed

+40
-25
lines changed

2 files changed

+40
-25
lines changed

src/lib.rs

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,23 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
use std::fs::File;
16+
use std::io::BufReader;
17+
use std::path::{Path, PathBuf};
18+
use std::result;
19+
20+
use openapiv3::OpenAPI;
21+
22+
pub use merger::merge_all_openapi_specs;
23+
1524
use crate::rust::lib_gen::{Module, ModuleDef, ModuleName};
1625
use crate::rust::model_gen::RefCache;
17-
use openapiv3::OpenAPI;
18-
use std::path::Path;
19-
use std::result;
2026

2127
pub(crate) mod merger;
2228
pub(crate) mod printer;
2329
mod rust;
2430
mod toml;
2531

26-
pub use merger::merge_all_openapi_specs;
27-
2832
#[derive(Debug, Clone)]
2933
pub enum Error {
3034
Unexpected { message: String },
@@ -57,7 +61,13 @@ impl Error {
5761

5862
pub type Result<T> = result::Result<T, Error>;
5963

60-
pub fn gen(openapi_specs: Vec<OpenAPI>, target: &Path, name: &str, version: &str) -> Result<()> {
64+
pub fn gen(
65+
openapi_specs: Vec<OpenAPI>,
66+
target: &Path,
67+
name: &str,
68+
version: &str,
69+
overwrite_cargo: bool,
70+
) -> Result<()> {
6171
let open_api = merge_all_openapi_specs(openapi_specs)?;
6272

6373
let src = target.join("src");
@@ -138,8 +148,23 @@ pub fn gen(openapi_specs: Vec<OpenAPI>, target: &Path, name: &str, version: &str
138148
let lib = rust::lib_gen::lib_gen("crate", &module_defs);
139149
std::fs::write(src.join("lib.rs"), lib).unwrap();
140150

141-
let cargo = toml::cargo::gen(name, version);
142-
std::fs::write(target.join("Cargo.toml"), cargo).unwrap();
151+
if overwrite_cargo {
152+
let cargo = toml::cargo::gen(name, version);
153+
std::fs::write(target.join("Cargo.toml"), cargo).unwrap();
154+
}
143155

144156
Ok(())
145157
}
158+
159+
pub fn parse_openapi_specs(
160+
spec: &[PathBuf],
161+
) -> std::result::Result<Vec<OpenAPI>, Box<dyn std::error::Error>> {
162+
spec.iter()
163+
.map(|spec_path| {
164+
let file = File::open(spec_path)?;
165+
let reader = BufReader::new(file);
166+
let openapi: OpenAPI = serde_yaml::from_reader(reader)?;
167+
Ok(openapi)
168+
})
169+
.collect()
170+
}

src/main.rs

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use clap::{Args, Parser};
16-
use golem_openapi_client_generator::gen;
17-
use openapiv3::OpenAPI;
1815
use std::fs::File;
19-
use std::io::BufReader;
2016
use std::path::PathBuf;
2117

18+
use clap::{Args, Parser};
19+
20+
use golem_openapi_client_generator::{gen, parse_openapi_specs};
21+
2222
#[derive(Parser, Debug)]
2323
#[command(author, version, about, long_about = None, rename_all = "kebab-case")]
2424
enum Cli {
@@ -56,32 +56,22 @@ fn main() {
5656

5757
match command {
5858
Cli::Generate(args) => {
59-
let openapi_specs = parse_openapi_specs(args.spec_yaml).unwrap();
59+
let openapi_specs = parse_openapi_specs(&args.spec_yaml).unwrap();
6060
gen(
6161
openapi_specs,
6262
&args.output_directory,
6363
&args.name,
6464
&args.client_version,
65+
true,
6566
)
6667
.unwrap();
6768
}
6869
Cli::Merge(args) => {
69-
let openapi_specs = parse_openapi_specs(args.spec_yaml).unwrap();
70+
let openapi_specs = parse_openapi_specs(&args.spec_yaml).unwrap();
7071
let openapi =
7172
golem_openapi_client_generator::merge_all_openapi_specs(openapi_specs).unwrap();
7273
let file = File::create(&args.output_yaml).unwrap();
7374
serde_yaml::to_writer(file, &openapi).unwrap();
7475
}
7576
}
7677
}
77-
78-
fn parse_openapi_specs(spec: Vec<PathBuf>) -> Result<Vec<OpenAPI>, Box<dyn std::error::Error>> {
79-
spec.iter()
80-
.map(|spec_path| {
81-
let file = File::open(spec_path)?;
82-
let reader = BufReader::new(file);
83-
let openapi: OpenAPI = serde_yaml::from_reader(reader)?;
84-
Ok(openapi)
85-
})
86-
.collect()
87-
}

0 commit comments

Comments
 (0)