pub mod code;
pub mod diff;
pub mod generic;
pub mod html;
pub mod json;
pub mod log;
pub mod ml_text;
pub mod search;
pub mod signals;
use async_trait::async_trait;
use crate::types::{CompressInput, CompressOptions, CompressOutput, CompressorKind, ContentKind};
#[async_trait]
pub trait Compressor: Send + Sync {
fn kind(&self) -> CompressorKind;
async fn compress(
&self,
input: &CompressInput<'_>,
opts: &CompressOptions,
) -> Option<CompressOutput>;
}
static JSON_COMPRESSOR: json::JsonCompressor = json::JsonCompressor;
static CODE_COMPRESSOR: code::CodeCompressor = code::CodeCompressor;
static LOG_COMPRESSOR: log::LogCompressor = log::LogCompressor;
static SEARCH_COMPRESSOR: search::SearchCompressor = search::SearchCompressor;
static DIFF_COMPRESSOR: diff::DiffCompressor = diff::DiffCompressor;
static HTML_COMPRESSOR: html::HtmlCompressor = html::HtmlCompressor;
static ML_TEXT_COMPRESSOR: ml_text::MlTextCompressor = ml_text::MlTextCompressor;
static GENERIC_COMPRESSOR: generic::GenericCompressor = generic::GenericCompressor;
pub fn compressor_for(kind: ContentKind) -> &'static dyn Compressor {
match kind {
ContentKind::Json => &JSON_COMPRESSOR,
ContentKind::Code => &CODE_COMPRESSOR,
ContentKind::Log => &LOG_COMPRESSOR,
ContentKind::Search => &SEARCH_COMPRESSOR,
ContentKind::Diff => &DIFF_COMPRESSOR,
ContentKind::Html => &HTML_COMPRESSOR,
ContentKind::PlainText => &ML_TEXT_COMPRESSOR,
}
}
pub fn generic_compressor() -> &'static dyn Compressor {
&GENERIC_COMPRESSOR
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn registry_maps_all_content_kinds_to_expected_compressors() {
let cases = [
(ContentKind::Json, CompressorKind::SmartCrusher),
(ContentKind::Code, CompressorKind::Code),
(ContentKind::Log, CompressorKind::Log),
(ContentKind::Search, CompressorKind::Search),
(ContentKind::Diff, CompressorKind::Diff),
(ContentKind::Html, CompressorKind::Html),
(ContentKind::PlainText, CompressorKind::MlText),
];
for (kind, expected) in cases {
assert_eq!(compressor_for(kind).kind(), expected);
}
assert_eq!(generic_compressor().kind(), CompressorKind::Generic);
}
}