Skip to content

Commit

Permalink
Implement Serialize for Value
Browse files Browse the repository at this point in the history
  • Loading branch information
belak committed Mar 9, 2024
1 parent cc5a905 commit 0de2410
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use std::{collections::BTreeMap, convert::TryFrom};

use iso8601::DateTime;

pub mod de;
pub mod ser;
mod de;
mod ser;

pub use ser::Serializer;
pub(crate) use ser::Serializer;

/// Convert a `T` into `serde_xmlrpc::Value` which is an enum that can represent
/// any valid XML-RPC data.
Expand Down
35 changes: 27 additions & 8 deletions src/value/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,27 @@ use serde::Serialize;
use crate::error::EncodingError;
use crate::{Error, Result, Value};

pub struct Serializer;
impl serde::ser::Serialize for Value {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
match self {
Value::Int(i) => serializer.serialize_i32(*i),
Value::Int64(i) => serializer.serialize_i64(*i),
Value::Bool(b) => serializer.serialize_bool(*b),
Value::Double(f) => serializer.serialize_f64(*f),
Value::String(s) => serializer.serialize_str(s),
Value::DateTime(dt) => serializer.serialize_str(&dt.to_string()),
Value::Base64(b) => serializer.serialize_bytes(b),
Value::Array(a) => a.serialize(serializer),
Value::Struct(m) => m.serialize(serializer),
Value::Nil => serializer.serialize_none(),
}
}
}

pub(crate) struct Serializer;

impl Serializer {
pub fn new() -> Self {
Expand Down Expand Up @@ -93,7 +113,7 @@ impl serde::Serializer for Serializer {
where
T: Serialize,
{
value.serialize(Serializer)
value.serialize(self)
}

fn serialize_unit(self) -> Result<Self::Ok> {
Expand All @@ -117,7 +137,7 @@ impl serde::Serializer for Serializer {
where
T: Serialize,
{
value.serialize(Serializer)
value.serialize(self)
}

fn serialize_newtype_variant<T: ?Sized>(
Expand All @@ -130,7 +150,7 @@ impl serde::Serializer for Serializer {
where
T: Serialize,
{
// TODO: replace with Error
// TODO: match implementation with serde_json
unimplemented!();
}

Expand Down Expand Up @@ -180,12 +200,12 @@ impl serde::Serializer for Serializer {
_variant: &'static str,
len: usize,
) -> Result<Self::SerializeStructVariant> {
// TODO: match implementation with serde_json
self.serialize_map(Some(len))
}
}

#[doc(hidden)]
pub struct SerializeVec {
pub(crate) struct SerializeVec {
vec: Vec<Value>,
}

Expand Down Expand Up @@ -254,8 +274,7 @@ impl serde::ser::SerializeTupleVariant for SerializeVec {
}
}

#[doc(hidden)]
pub struct SerializeMap {
pub(crate) struct SerializeMap {
map: BTreeMap<String, Value>,
next_key: Option<String>,
}
Expand Down

0 comments on commit 0de2410

Please sign in to comment.