use crate::{Model, Result, json, text, textproto};
pub trait TextCodec {
type Options: Default;
fn serialize(model: &Model, options: &Self::Options) -> Result<String>;
fn deserialize(source: &str) -> Result<Model>;
}
#[derive(Clone, Copy, Debug, Default)]
pub struct Text;
impl TextCodec for Text {
type Options = text::PrintOptions;
fn serialize(model: &Model, options: &Self::Options) -> Result<String> {
Ok(text::to_text_with(model, options))
}
fn deserialize(source: &str) -> Result<Model> {
text::from_text(source)
}
}
#[derive(Clone, Copy, Debug, Default)]
pub struct Json;
impl TextCodec for Json {
type Options = ();
fn serialize(model: &Model, _options: &Self::Options) -> Result<String> {
json::to_json(model)
}
fn deserialize(source: &str) -> Result<Model> {
json::from_json(source)
}
}
#[derive(Clone, Copy, Debug, Default)]
pub struct TextProto;
impl TextCodec for TextProto {
type Options = ();
fn serialize(model: &Model, _options: &Self::Options) -> Result<String> {
textproto::to_textproto(model)
}
fn deserialize(source: &str) -> Result<Model> {
textproto::from_textproto(source)
}
}