use std::env;
use std::path::Path;
use std::process::ExitCode;
use mdkit::{Document, Engine, Error, Extractor, Result};
struct Rot13Extractor;
impl Extractor for Rot13Extractor {
fn extensions(&self) -> &[&'static str] {
&["rot", "rot13"]
}
fn name(&self) -> &'static str {
"rot13-example"
}
fn extract(&self, path: &Path) -> Result<Document> {
let bytes = std::fs::read(path)?;
self.extract_bytes(&bytes, "rot")
}
fn extract_bytes(&self, bytes: &[u8], _ext: &str) -> Result<Document> {
let text = std::str::from_utf8(bytes)
.map_err(|e| Error::ParseError(format!("rot13: not valid UTF-8: {e}")))?;
Ok(Document::new(rot13(text)))
}
}
fn rot13(input: &str) -> String {
input
.chars()
.map(|c| match c {
'a'..='m' | 'A'..='M' => (c as u8 + 13) as char,
'n'..='z' | 'N'..='Z' => (c as u8 - 13) as char,
_ => c,
})
.collect()
}
fn main() -> ExitCode {
let Some(path) = env::args().nth(1) else {
eprintln!("usage: custom_extractor <path-to-.rot-file>");
return ExitCode::FAILURE;
};
let mut engine = Engine::new();
engine.register(Box::new(Rot13Extractor));
match engine.extract(Path::new(&path)) {
Ok(doc) => {
print!("{}", doc.markdown);
ExitCode::SUCCESS
}
Err(e) => {
eprintln!("error: {e}");
ExitCode::FAILURE
}
}
}