xml_data/serializer/
value.rs

1use crate::{
2	Result,
3};
4use std::borrow::Cow;
5
6/// Trait to serialize attributes and inner text
7///
8/// This is implemented my "marker" types to decide how to serialize a type (the same type can be
9/// serialized differently depending on the marker type)
10pub trait Value<T> {
11	/// Serialize value to text
12	fn serialize_value(data: &T) -> Result<Cow<'_, str>>;
13}
14
15/// Implements `Value` for all types implementing `std::fmt::Display`; this is a good default.
16pub struct ValueDefault;
17
18impl<T: std::fmt::Display> Value<T> for ValueDefault {
19	fn serialize_value(data: &T) -> Result<Cow<'_, str>> {
20		Ok(Cow::Owned(data.to_string()))
21	}
22}
23
24/// Implements `Value` for all types implementing `AsRef<str>`.
25pub struct ValueString;
26
27impl<T: AsRef<str>> Value<T> for ValueString {
28	fn serialize_value(data: &T) -> Result<Cow<'_, str>> {
29		Ok(Cow::Borrowed(data.as_ref()))
30	}
31}