Skip to content

Commit

Permalink
rename ToXml to WriteXml and move method
Browse files Browse the repository at this point in the history
  • Loading branch information
benmaddison committed Dec 11, 2023
1 parent 8e1cc73 commit 940fe3b
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 28 deletions.
8 changes: 4 additions & 4 deletions netconf/src/message/hello.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -108,7 +108,7 @@ impl ClientHello {
}
}

impl ToXml for ClientHello {
impl WriteXml for ClientHello {
type Error = Error;

fn write_xml<W: Write>(&self, writer: &mut W) -> Result<(), Self::Error> {
Expand Down Expand Up @@ -159,7 +159,7 @@ impl Capabilities {
}
}

impl ToXml for Capabilities {
impl WriteXml for Capabilities {
type Error = Error;

fn write_xml<W: Write>(&self, writer: &mut W) -> Result<(), Self::Error> {
Expand Down Expand Up @@ -212,7 +212,7 @@ impl Capability {
}
}

impl ToXml for Capability {
impl WriteXml for Capability {
type Error = Error;

fn write_xml<W: Write>(&self, writer: &mut W) -> Result<(), Self::Error> {
Expand Down
14 changes: 7 additions & 7 deletions netconf/src/message/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,24 @@ pub trait ReadXml: Sized {
fn read_xml(reader: &mut NsReader<&[u8]>, start: &BytesStart<'_>) -> Result<Self, Self::Error>;
}

pub trait ToXml {
pub trait WriteXml {
type Error: From<FromUtf8Error> + std::error::Error + Send + Sync + 'static;

fn write_xml<W: Write>(&self, writer: &mut W) -> Result<(), Self::Error>;
}

#[async_trait]
pub trait ClientMsg: WriteXml + Debug
where
Error: From<Self::Error>,
{
fn to_xml(&self) -> Result<String, Self::Error> {
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<Self::Error>,
{
#[tracing::instrument(skip(sender), err, level = "debug")]
async fn send<T: SendHandle>(&self, sender: &mut T) -> Result<(), Error> {
let serialized = self.to_xml()?;
Expand Down
4 changes: 2 additions & 2 deletions netconf/src/message/rpc/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -461,7 +461,7 @@ mod tests {
type ReplyData = Empty;
}

impl ToXml for Dummy {
impl WriteXml for Dummy {
type Error = crate::Error;
fn write_xml<W: Write>(&self, _: &mut W) -> Result<(), Self::Error> {
Ok(())
Expand Down
8 changes: 4 additions & 4 deletions netconf/src/message/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -47,7 +47,7 @@ impl<O: Operation> Request<O> {
}
}

impl<O: Operation> ToXml for Request<O> {
impl<O: Operation> WriteXml for Request<O> {
type Error = crate::Error;

fn write_xml<W: Write>(&self, writer: &mut W) -> Result<(), Self::Error> {
Expand Down Expand Up @@ -270,7 +270,7 @@ mod tests {
foo: &'static str,
}

impl ToXml for Foo {
impl WriteXml for Foo {
type Error = crate::Error;

fn write_xml<W: Write>(&self, writer: &mut W) -> Result<(), Self::Error> {
Expand Down Expand Up @@ -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<W: Write>(&self, writer: &mut W) -> Result<(), Self::Error> {
Expand Down
37 changes: 26 additions & 11 deletions netconf/src/message/rpc/operation/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
}

Expand All @@ -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 {
Expand All @@ -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<W: Write>(&self, writer: &mut W) -> Result<(), Self::Error> {
Expand Down Expand Up @@ -64,7 +64,7 @@ pub mod get_config {
Running,
}

impl ToXml for Source {
impl WriteXml for Source {
type Error = Error;

fn write_xml<W: Write>(&self, writer: &mut W) -> Result<(), Self::Error> {
Expand Down Expand Up @@ -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 = "<get-config><source><running/></source></get-config>]]>]]>";
let req = Request {
message_id: MessageId(101),
operation: GetConfig::default(),
};
let expect = r#"<rpc message-id="101"><get-config><source><running/></source></get-config></rpc>]]>]]>"#;
assert_eq!(req.to_xml().unwrap(), expect);
}

Expand All @@ -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)]
Expand All @@ -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<W: Write>(&self, writer: &mut W) -> Result<(), Self::Error> {
Expand All @@ -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 = "<close-session/>]]>]]>";
let req = Request {
message_id: MessageId(101),
operation: CloseSession,
};
let expect = r#"<rpc message-id="101"><close-session/></rpc>]]>]]>"#;
assert_eq!(req.to_xml().unwrap(), expect);
}
}
Expand Down

0 comments on commit 940fe3b

Please sign in to comment.