Crates.io | openapi3-parser |
lib.rs | openapi3-parser |
version | 0.0.1 |
source | src |
created_at | 2024-09-08 06:14:05.19475 |
updated_at | 2024-09-09 01:50:03.030066 |
description | A Rust library to parse and work with OpenAPI 3.0 specifications in JSON and YAML format |
homepage | https://github.com/ayonsaha2011/openapi3-parser |
repository | https://github.com/ayonsaha2011/openapi3-parser |
max_upload_size | |
id | 1367791 |
size | 3,631,237 |
OpenAPI 3.0 Parser Library is a Rust crate designed to parse OpenAPI (Swagger) 3.0 specifications in both JSON and YAML formats. This library allows developers to easily handle OpenAPI spec files and extract the information programmatically.
Add this to your Cargo.toml
:
[dependencies]
openapi3-parser = "0.1.0"
use openapi3_parser::OpenApiSpec;
use std::fs::File;
use std::io::Read;
use anyhow::Result;
fn main() -> Result<()> {
// Load OpenAPI spec file
let mut file = File::open("openapi_spec.yaml")?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
// Parse the OpenAPI spec
let openapi_spec: OpenApiSpec = serde_yaml::from_str(&contents)?;
// Print the title of the API
if let Some(info) = openapi_spec.info {
if let Some(title) = info.title {
println!("API Title: {}", title);
}
}
Ok(())
}
You can parse a file in either YAML or JSON format:
use openapi3_parser::OpenApiSpec;
use serde_yaml;
use serde_json;
use std::fs::File;
use std::io::Read;
use anyhow::Result;
fn load_openapi_spec(path: &str) -> Result<OpenApiSpec> {
let mut file = File::open(path)?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
if path.ends_with(".yaml") || path.ends_with(".yml") {
Ok(serde_yaml::from_str(&contents)?)
} else if path.ends_with(".json") {
Ok(serde_json::from_str(&contents)?)
} else {
Err(anyhow::anyhow!("Unsupported file format"))
}
}
fn main() -> Result<()> {
let spec = load_openapi_spec("path/to/openapi_spec.yaml")?;
println!("{:?}", spec);
Ok(())
}
Full documentation is available on docs.rs.
This project is licensed under the MIT License - see the LICENSE file for details.
If you encounter any issues or need help, please open an issue on GitHub.
For additional support, you can contact me at ayonsaha2011@gmail.com.
If you find this library useful and would like to support its ongoing development, consider making a donation. Your support is greatly appreciated!