Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

add support for json source #14

Merged
merged 1 commit into from
May 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
demo: demo-compile demo-format demo-check

demo-compile:
@cargo run --package cargo-swagg -- ./demo/openapi.json --out-file ./demo/src/lib.rs
@cargo run --package cargo-swagg -- ./demo/openapi.yaml --out-file ./demo/src/lib.rs

demo-format:
Expand Down
20 changes: 14 additions & 6 deletions cargo-swagg/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,21 @@ fn main() -> Result<(), Box<dyn std::error::Error + 'static>> {
)
.get_matches();

let content = std::fs::read_to_string(
&opts
.value_of("source")
.expect("Pass file with openapi3 specification"),
)?;
let path = opts
.value_of("source")
.expect("Pass file with openapi3 specification");

let source_code = swagg::to_string(&content, swagg::Format::Yaml).unwrap();
let path = std::path::Path::new(&path);

let content = std::fs::read_to_string(&path)?;

let format = match path.extension().and_then(|ext| ext.to_str()) {
Some("yaml") | Some("yml") | None => swagg::Format::Yaml,
Some("json") => swagg::Format::Json,
Some(ext) => panic!("Unexpected source extension {}", ext),
};

let source_code = swagg::to_string(&content, format).unwrap();

let code = format!("{}", source_code);
if let Some(file) = opts.value_of("out-file") {
Expand Down
Loading