use nojson::{JsonParseError, RawJsonValue};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CodecDirection {
Encoder,
Decoder,
}
impl CodecDirection {
pub fn as_str(self) -> &'static str {
match self {
Self::Encoder => "Encoder",
Self::Decoder => "Decoder",
}
}
pub fn as_label(self) -> &'static str {
match self {
Self::Encoder => "encoder",
Self::Decoder => "decoder",
}
}
}
impl<'text, 'raw> TryFrom<RawJsonValue<'text, 'raw>> for CodecDirection {
type Error = JsonParseError;
fn try_from(value: RawJsonValue<'text, 'raw>) -> std::result::Result<Self, Self::Error> {
let direction_text: String = value.try_into()?;
match direction_text.as_str() {
"Encoder" => Ok(Self::Encoder),
"Decoder" => Ok(Self::Decoder),
_ => Err(value.invalid(format!("unsupported codec direction: {direction_text}"))),
}
}
}