//! Shared access to OpenRPC documents and their Sausage extensions. use serde_json::{Value, json}; /// An OpenRPC document, including JSON Schema and extension fields. #[derive(Clone, Debug, PartialEq)] pub struct Document(pub Value); impl Document { /// Create an empty main interface. pub fn new(title: &str, version: &str) -> Self { Self( json!({"openrpc":"1.2.6", "info":{"title":title,"version":version}, "methods":[], "components":{"schemas":{}}, "x-sausage-endpoints":{}, "x-sausage-imports":{}}), ) } /// Read the method descriptions. pub fn methods(&self) -> &[Value] { self.0["methods"] .as_array() .map(Vec::as_slice) .unwrap_or_default() } /// Read a named schema. pub fn schema(&self, name: &str) -> Option<&Value> { self.0.get("components")?.get("schemas")?.get(name) } /// Consume the document as a JSON value. pub fn into_value(self) -> Value { self.0 } }