//! Module with trimmed down `OpenRPC` document structs. //! It tries to follow this standard: use bevy_platform::collections::HashMap; use bevy_utils::default; use serde::{Deserialize, Serialize}; use crate::RemoteMethods; use super::json_schema::JsonSchemaBevyType; /// Represents an `OpenRPC` document as defined by the `OpenRPC` specification. #[derive(Debug, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct OpenRpcDocument { /// The version of the `OpenRPC` specification being used. pub openrpc: String, /// Informational metadata about the document. pub info: InfoObject, /// List of RPC methods defined in the document. pub methods: Vec, /// Optional list of server objects that provide the API endpoint details. pub servers: Option>, } /// Contains metadata information about the `OpenRPC` document. #[derive(Serialize, Deserialize, Debug)] #[serde(rename_all = "camelCase")] pub struct InfoObject { /// The title of the API or document. pub title: String, /// The version of the API. pub version: String, /// An optional description providing additional details about the API. #[serde(skip_serializing_if = "Option::is_none")] pub description: Option, /// A collection of custom extension fields. #[serde(flatten)] pub extensions: HashMap, } impl Default for InfoObject { fn default() -> Self { Self { title: "Bevy Remote Protocol".to_owned(), version: env!("CARGO_PKG_VERSION").to_owned(), description: None, extensions: Default::default(), } } } /// Describes a server hosting the API as specified in the `OpenRPC` document. #[derive(Serialize, Deserialize, Debug, Default)] #[serde(rename_all = "camelCase")] pub struct ServerObject { /// The name of the server. pub name: String, /// The URL endpoint of the server. pub url: String, /// An optional description of the server. #[serde(skip_serializing_if = "Option::is_none")] pub description: Option, /// Additional custom extension fields. #[serde(flatten)] pub extensions: HashMap, } /// Represents an RPC method in the `OpenRPC` document. #[derive(Serialize, Deserialize, Debug, Default)] #[serde(rename_all = "camelCase")] pub struct MethodObject { /// The method name (e.g., "/bevy/get") pub name: String, /// An optional short summary of the method. #[serde(skip_serializing_if = "Option::is_none")] pub summary: Option, /// An optional detailed description of the method. #[serde(skip_serializing_if = "Option::is_none")] pub description: Option, /// Parameters for the RPC method #[serde(default)] pub params: Vec, // /// The expected result of the method // #[serde(skip_serializing_if = "Option::is_none")] // pub result: Option, /// Additional custom extension fields. #[serde(flatten)] pub extensions: HashMap, } /// Represents an RPC method parameter in the `OpenRPC` document. #[derive(Serialize, Deserialize, Debug)] #[serde(rename_all = "camelCase")] pub struct Parameter { /// Parameter name pub name: String, /// Parameter description #[serde(skip_serializing_if = "Option::is_none")] pub description: Option, /// JSON schema describing the parameter pub schema: JsonSchemaBevyType, /// Additional custom extension fields. #[serde(flatten)] pub extensions: HashMap, } impl From<&RemoteMethods> for Vec { fn from(value: &RemoteMethods) -> Self { value .methods() .iter() .map(|e| MethodObject { name: e.to_owned(), ..default() }) .collect() } }