mod byte_level;
use crate::json_structs::{DecoderConfig, DecoderKind};
pub use self::byte_level::ByteLevelDecoder;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("unsupported decoder type: {0}")]
Unsupported(String),
}
#[derive(Debug)]
pub enum Decoder {
ByteLevel(ByteLevelDecoder),
Sequence(Vec<Decoder>),
}
impl Decoder {
pub fn from_config(config: DecoderConfig) -> Result<Self, Error> {
match config {
DecoderConfig::ByteLevel => Ok(Self::ByteLevel(ByteLevelDecoder)),
DecoderConfig::Fuse => Ok(Self::Sequence(vec![])), DecoderConfig::Sequence { decoders } => {
let steps = decoders
.into_iter()
.map(Self::from_config)
.collect::<Result<Vec<_>, _>>()?;
Ok(Self::Sequence(steps))
}
DecoderConfig::Other(v) => {
let typ = v.get("type").and_then(|t| t.as_str()).unwrap_or("unknown");
Err(Error::Unsupported(typ.to_string()))
}
other => {
let kind = DecoderKind::from(&other);
Err(Error::Unsupported(kind.to_string()))
}
}
}
pub fn decode_chain(&self, tokens: Vec<String>) -> Result<Vec<String>, Error> {
match self {
Self::ByteLevel(bl) => Ok(bl.decode_chain(tokens)),
Self::Sequence(steps) => {
let mut current = tokens;
for step in steps {
current = step.decode_chain(current)?;
}
Ok(current)
}
}
}
pub fn decode(&self, tokens: Vec<String>) -> Result<String, Error> {
let result = self.decode_chain(tokens)?;
Ok(result.join(""))
}
}