openapi3-parser

Crates.ioopenapi3-parser
lib.rsopenapi3-parser
version0.0.1
sourcesrc
created_at2024-09-08 06:14:05.19475
updated_at2024-09-09 01:50:03.030066
descriptionA Rust library to parse and work with OpenAPI 3.0 specifications in JSON and YAML format
homepagehttps://github.com/ayonsaha2011/openapi3-parser
repositoryhttps://github.com/ayonsaha2011/openapi3-parser
max_upload_size
id1367791
size3,631,237
Ayon Saha (ayonsaha2011)

documentation

https://docs.rs/openapi3-parser

README

OpenAPI 3.0 Parser Library

Crates.io Documentation

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.

Features

  • Supports parsing OpenAPI 3.0 specifications in both JSON and YAML.
  • Extract and manipulate OpenAPI components such as paths, operations, parameters, responses, etc.
  • Written in Rust for high performance and safety.

Installation

Add this to your Cargo.toml:

[dependencies]
openapi3-parser = "0.1.0"

Usage

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(())
}

Examples

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(())
}

Documentation

Full documentation is available on docs.rs.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Support

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.

Donation

If you find this library useful and would like to support its ongoing development, consider making a donation. Your support is greatly appreciated!

Buy me a coffee

Commit count: 0

cargo fmt