diff --git a/netconf/src/message/hello.rs b/netconf/src/message/hello.rs index 2cfc391..5f7cb66 100644 --- a/netconf/src/message/hello.rs +++ b/netconf/src/message/hello.rs @@ -13,7 +13,7 @@ use quick_xml::{ use crate::Error; -use super::{xmlns, ClientMsg, ReadXml, ServerMsg, ToXml}; +use super::{xmlns, ClientMsg, ReadXml, ServerMsg, WriteXml}; #[derive(Debug, Clone, PartialEq, Eq)] pub(crate) struct ServerHello { @@ -108,7 +108,7 @@ impl ClientHello { } } -impl ToXml for ClientHello { +impl WriteXml for ClientHello { type Error = Error; fn write_xml(&self, writer: &mut W) -> Result<(), Self::Error> { @@ -159,7 +159,7 @@ impl Capabilities { } } -impl ToXml for Capabilities { +impl WriteXml for Capabilities { type Error = Error; fn write_xml(&self, writer: &mut W) -> Result<(), Self::Error> { @@ -212,7 +212,7 @@ impl Capability { } } -impl ToXml for Capability { +impl WriteXml for Capability { type Error = Error; fn write_xml(&self, writer: &mut W) -> Result<(), Self::Error> { diff --git a/netconf/src/message/mod.rs b/netconf/src/message/mod.rs index d390a04..84532c5 100644 --- a/netconf/src/message/mod.rs +++ b/netconf/src/message/mod.rs @@ -27,24 +27,24 @@ pub trait ReadXml: Sized { fn read_xml(reader: &mut NsReader<&[u8]>, start: &BytesStart<'_>) -> Result; } -pub trait ToXml { +pub trait WriteXml { type Error: From + std::error::Error + Send + Sync + 'static; fn write_xml(&self, writer: &mut W) -> Result<(), Self::Error>; +} +#[async_trait] +pub trait ClientMsg: WriteXml + Debug +where + Error: From, +{ fn to_xml(&self) -> Result { let mut buf = Vec::new(); self.write_xml(&mut buf)?; buf.extend_from_slice(MARKER); Ok(String::from_utf8(buf)?) } -} -#[async_trait] -pub trait ClientMsg: ToXml + Debug -where - Error: From, -{ #[tracing::instrument(skip(sender), err, level = "debug")] async fn send(&self, sender: &mut T) -> Result<(), Error> { let serialized = self.to_xml()?; diff --git a/netconf/src/message/rpc/error.rs b/netconf/src/message/rpc/error.rs index 8606bd1..58add84 100644 --- a/netconf/src/message/rpc/error.rs +++ b/netconf/src/message/rpc/error.rs @@ -451,7 +451,7 @@ mod tests { use super::*; use crate::message::{ rpc::{Empty, MessageId, Operation, PartialReply, Reply, ReplyInner}, - ServerMsg, ToXml, + ServerMsg, WriteXml, }; #[derive(Debug, PartialEq)] @@ -461,7 +461,7 @@ mod tests { type ReplyData = Empty; } - impl ToXml for Dummy { + impl WriteXml for Dummy { type Error = crate::Error; fn write_xml(&self, _: &mut W) -> Result<(), Self::Error> { Ok(()) diff --git a/netconf/src/message/rpc/mod.rs b/netconf/src/message/rpc/mod.rs index 3085e18..8ae12dc 100644 --- a/netconf/src/message/rpc/mod.rs +++ b/netconf/src/message/rpc/mod.rs @@ -6,7 +6,7 @@ use quick_xml::{ NsReader, Writer, }; -use super::{xmlns, ClientMsg, ReadXml, ServerMsg, ToXml, MARKER}; +use super::{xmlns, ClientMsg, ReadXml, ServerMsg, WriteXml, MARKER}; pub mod error; pub use self::error::{Error, Errors}; @@ -47,7 +47,7 @@ impl Request { } } -impl ToXml for Request { +impl WriteXml for Request { type Error = crate::Error; fn write_xml(&self, writer: &mut W) -> Result<(), Self::Error> { @@ -270,7 +270,7 @@ mod tests { foo: &'static str, } - impl ToXml for Foo { + impl WriteXml for Foo { type Error = crate::Error; fn write_xml(&self, writer: &mut W) -> Result<(), Self::Error> { @@ -342,7 +342,7 @@ mod tests { #[derive(Debug, Clone, PartialEq, Eq)] struct Bar; - impl ToXml for Bar { + impl WriteXml for Bar { type Error = crate::Error; fn write_xml(&self, writer: &mut W) -> Result<(), Self::Error> { diff --git a/netconf/src/message/rpc/operation/mod.rs b/netconf/src/message/rpc/operation/mod.rs index 7ccb423..5edc9da 100644 --- a/netconf/src/message/rpc/operation/mod.rs +++ b/netconf/src/message/rpc/operation/mod.rs @@ -1,8 +1,8 @@ use std::fmt::Debug; -use crate::message::{ReadXml, ToXml}; +use crate::message::{ReadXml, WriteXml}; -pub trait Operation: Debug + ToXml + Send + Sync { +pub trait Operation: Debug + WriteXml + Send + Sync { type ReplyData: Debug + ReadXml; } @@ -13,7 +13,7 @@ pub mod get_config { use crate::Error; - use super::{Operation, ReadXml, ToXml}; + use super::{Operation, ReadXml, WriteXml}; #[derive(Debug, Default, Clone)] pub struct GetConfig { @@ -32,7 +32,7 @@ pub mod get_config { type ReplyData = Reply; } - impl ToXml for GetConfig { + impl WriteXml for GetConfig { type Error = Error; fn write_xml(&self, writer: &mut W) -> Result<(), Self::Error> { @@ -64,7 +64,7 @@ pub mod get_config { Running, } - impl ToXml for Source { + impl WriteXml for Source { type Error = Error; fn write_xml(&self, writer: &mut W) -> Result<(), Self::Error> { @@ -104,13 +104,20 @@ pub mod get_config { #[cfg(test)] mod tests { use super::*; + use crate::message::{ + rpc::{MessageId, Request}, + ClientMsg, + }; use quick_xml::events::Event; #[test] fn default_request_to_xml() { - let req = GetConfig::default(); - let expect = "]]>]]>"; + let req = Request { + message_id: MessageId(101), + operation: GetConfig::default(), + }; + let expect = r#"]]>]]>"#; assert_eq!(req.to_xml().unwrap(), expect); } @@ -137,7 +144,7 @@ pub mod close_session { use quick_xml::Writer; - use super::{super::Empty, Operation, ToXml}; + use super::{super::Empty, Operation, WriteXml}; use crate::Error; #[derive(Debug, Default, Clone, Copy)] @@ -147,7 +154,7 @@ pub mod close_session { type ReplyData = Empty; } - impl ToXml for CloseSession { + impl WriteXml for CloseSession { type Error = Error; fn write_xml(&self, writer: &mut W) -> Result<(), Self::Error> { @@ -162,10 +169,18 @@ pub mod close_session { mod tests { use super::*; + use crate::message::{ + rpc::{MessageId, Request}, + ClientMsg, + }; + #[test] fn request_to_xml() { - let req = CloseSession; - let expect = "]]>]]>"; + let req = Request { + message_id: MessageId(101), + operation: CloseSession, + }; + let expect = r#"]]>]]>"#; assert_eq!(req.to_xml().unwrap(), expect); } }