Skip to content

Commit

Permalink
Add some more RPC documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
TSnake41 committed Aug 31, 2023
1 parent 7337bf7 commit ef48621
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions xapi-rs/src/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ macro_rules! rpc_method {
};
}

/// A RPC method that has a name.
pub trait XcpRpcMethodNamed {
fn get_method_name() -> &'static str;
}

/// Trait of a RPC method that can be converted back and forth to XML-RPC method call or JSON-RPC request.
pub trait XcpRpcMethod: Sized {
fn to_xmlrpc(&self) -> anyhow::Result<dxr::MethodCall>;
fn to_jsonrpc(&self) -> anyhow::Result<jsonrpc_base::Request>;
Expand Down Expand Up @@ -76,26 +78,30 @@ where
}
}

/// Write a [XcpRpcMethod] into a `writer` using JSON-RPC.
pub fn write_method_jsonrpc<W: Write, M: XcpRpcMethod>(
writer: &mut W,
method: &M,
) -> anyhow::Result<()> {
RpcRequest::new(method, RpcKind::JsonRpc)?.write(writer)
}

/// Write a [XcpRpcMethod] into a `writer` using XML-RPC.
pub fn write_method_xmlrpc<W: Write, M: XcpRpcMethod>(
writer: &mut W,
method: &M,
) -> anyhow::Result<()> {
RpcRequest::new(method, RpcKind::XmlRpc)?.write(writer)
}

/// Parse a [XcpRpcMethod] from raw JSON-RPC.
pub fn parse_method_jsonrpc<M: XcpRpcMethod>(data: &[u8]) -> anyhow::Result<M> {
RpcRequest::parse(data, RpcKind::JsonRpc)?
.try_into_method()
.ok_or(anyhow::anyhow!("Readed method doesn't match"))
}

/// Parse [XcpRpcMethod] from raw XML-RPC.
pub fn parse_method_xmlrpc<M: XcpRpcMethod>(data: &[u8]) -> anyhow::Result<M> {
RpcRequest::parse(data, RpcKind::XmlRpc)?
.try_into_method()
Expand Down

0 comments on commit ef48621

Please sign in to comment.