pub trait Output {
fn to_plain(&self) -> String;
#[cfg(feature = "json")]
fn to_json(&self) -> serde_json::Value;
}
impl Output for String {
fn to_plain(&self) -> String {
self.clone()
}
#[cfg(feature = "json")]
fn to_json(&self) -> serde_json::Value {
serde_json::json!(self)
}
}
impl Output for &str {
fn to_plain(&self) -> String {
(*self).to_string()
}
#[cfg(feature = "json")]
fn to_json(&self) -> serde_json::Value {
serde_json::json!(self)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_string_to_plain() {
let value = "test value".to_string();
assert_eq!(value.to_plain(), "test value");
}
#[test]
#[cfg(feature = "json")]
fn test_string_to_json() {
let value = "test value".to_string();
let json = value.to_json();
assert_eq!(json, "test value");
}
#[test]
fn test_str_to_plain() {
let value = "test value";
assert_eq!(value.to_plain(), "test value");
}
#[test]
#[cfg(feature = "json")]
fn test_str_to_json() {
let value = "test value";
let json = value.to_json();
assert_eq!(json, "test value");
}
}