Skip to main content

xidl_parser/rest_hir/model/
document.rs

1use super::{HttpDocumentMetadata, HttpInterface, RestHirDocument};
2use crate::error::{ParseError, ParserResult};
3use crate::hir;
4
5impl RestHirDocument {
6    /// Reads the projected HTTP document from parser properties.
7    pub fn from_props(props: &hir::ParserProperties) -> ParserResult<Self> {
8        let value = props
9            .get("rest_hir")
10            .cloned()
11            .ok_or_else(|| ParseError::Message("missing rest_hir properties".to_string()))?;
12        serde_json::from_value(value).map_err(|err| ParseError::Message(err.to_string()))
13    }
14
15    /// Finds an interface by its enclosing module path and name.
16    pub fn find_interface(&self, module_path: &[String], name: &str) -> Option<&HttpInterface> {
17        self.interfaces
18            .iter()
19            .find(|interface| interface.name == name && interface.module_path == module_path)
20    }
21}
22
23impl HttpDocumentMetadata {
24    /// Finds the version of an interface, including nested business modules.
25    pub fn api_version(&self, module_path: &[String]) -> Option<&str> {
26        self.version.as_deref().or_else(|| {
27            module_path
28                .first()
29                .and_then(|module| self.version_values.get(module))
30                .map(String::as_str)
31        })
32    }
33}