openapi_utils/
server.rs

1use alloc::string::String;
2use http::Uri;
3use openapiv3::*;
4
5/// Extension methods for Server
6pub trait ServerExt {
7    /// Returns the `base_path` of the server ensuring the string does not end on /
8    fn base_path(&self) -> String;
9}
10
11impl ServerExt for Server {
12    /// Returns a string with the base path for the Server
13    /// It guarantees it does not end on /
14    fn base_path(&self) -> String {
15        if let Some(variables) = &self.variables {
16            match variables.get("basePath") {
17                Some(base_path) => {
18                    let mut base_str = base_path.default.clone();
19                    let last_character = base_str.chars().last().unwrap();
20                    if last_character == '/' {
21                        base_str.pop();
22                    }
23                    base_str
24                }
25                None => String::from(""),
26            }
27        } else {
28            let url_parse = &self.url.parse::<Uri>();
29            match url_parse {
30                Ok(url) => String::from(url.path()),
31                Err(_) => String::from(""),
32            }
33        }
34    }
35}