use derive_more::From;
use serde_json::Value;
#[derive(Clone, Debug, Default, From)]
pub enum Content {
#[from(String, &str)]
String(String),
#[from]
JSON(Value),
#[default]
Empty,
}
impl Content {
pub fn as_string(self) -> String {
match self {
Self::String(content) => content,
Self::JSON(json) => json.to_string(),
Self::Empty => String::new(),
}
}
pub fn as_json(self) -> Value {
match self {
Self::String(content) => serde_json::from_str(&content).unwrap_or_default(),
Self::JSON(json) => json,
Self::Empty => Value::Null,
}
}
pub fn is_empty(self) -> bool {
match self {
Self::String(content) => content.is_empty(),
Self::JSON(json) => json == Value::Null,
Self::Empty => true,
}
}
}