|
12 | 12 | // See the License for the specific language governing permissions and |
13 | 13 | // limitations under the License. |
14 | 14 |
|
| 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 | + |
15 | 24 | use crate::rust::lib_gen::{Module, ModuleDef, ModuleName}; |
16 | 25 | use crate::rust::model_gen::RefCache; |
17 | | -use openapiv3::OpenAPI; |
18 | | -use std::path::Path; |
19 | | -use std::result; |
20 | 26 |
|
21 | 27 | pub(crate) mod merger; |
22 | 28 | pub(crate) mod printer; |
23 | 29 | mod rust; |
24 | 30 | mod toml; |
25 | 31 |
|
26 | | -pub use merger::merge_all_openapi_specs; |
27 | | - |
28 | 32 | #[derive(Debug, Clone)] |
29 | 33 | pub enum Error { |
30 | 34 | Unexpected { message: String }, |
@@ -57,7 +61,13 @@ impl Error { |
57 | 61 |
|
58 | 62 | pub type Result<T> = result::Result<T, Error>; |
59 | 63 |
|
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<()> { |
61 | 71 | let open_api = merge_all_openapi_specs(openapi_specs)?; |
62 | 72 |
|
63 | 73 | let src = target.join("src"); |
@@ -138,8 +148,23 @@ pub fn gen(openapi_specs: Vec<OpenAPI>, target: &Path, name: &str, version: &str |
138 | 148 | let lib = rust::lib_gen::lib_gen("crate", &module_defs); |
139 | 149 | std::fs::write(src.join("lib.rs"), lib).unwrap(); |
140 | 150 |
|
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 | + } |
143 | 155 |
|
144 | 156 | Ok(()) |
145 | 157 | } |
| 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 | +} |
0 commit comments