use std::fmt;
pub trait OutputCodec<V: ?Sized> {
fn encode(&self, value: &V) -> String;
}
#[derive(Clone, Copy, Debug, Default)]
pub struct DisplayCodec;
impl<V: fmt::Display + ?Sized> OutputCodec<V> for DisplayCodec {
fn encode(&self, value: &V) -> String {
value.to_string()
}
}
#[derive(Clone, Copy, Debug, Default)]
pub struct SpaceJoinCodec;
impl OutputCodec<Vec<String>> for SpaceJoinCodec {
#[allow(clippy::ptr_arg)] fn encode(&self, value: &Vec<String>) -> String {
value.join(" ")
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn display_codec_uses_display() {
assert_eq!(DisplayCodec.encode(&2u8), "2");
assert_eq!(DisplayCodec.encode("hi"), "hi");
}
#[test]
fn space_join_codec_joins_words() {
assert_eq!(
SpaceJoinCodec.encode(&vec!["the".to_owned(), "woman".to_owned()]),
"the woman"
);
assert_eq!(SpaceJoinCodec.encode(&vec![]), "");
assert_eq!(SpaceJoinCodec.encode(&vec!["the".to_owned()]), "the");
}
}