use std::io::{Read, Write};
use tabnas_alchemy::translate::Options;
use tabnas_transduce::{AbortFlag, Fail, Limits, Metrics, Segment};
use crate::stack::{on_stack, run_on_stack, Written};
use crate::translate::{self, Compiled, Document, Format, Request};
use crate::{default_limits, Abort, Source};
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TranslateOptions {
pub from: String,
pub to: String,
pub path: Option<String>,
pub key: Option<String>,
pub with: Option<Source>,
pub whole: bool,
pub limits: Limits,
}
impl TranslateOptions {
pub fn new(from: &str, to: &str) -> TranslateOptions {
TranslateOptions {
from: from.to_string(),
to: to.to_string(),
path: None,
key: None,
with: None,
whole: false,
limits: default_limits(),
}
}
pub fn validate(&self) -> Result<(), Fail> {
on_stack(|| self.resolve().map(drop))
}
fn resolve(&self) -> Result<Resolved, Fail> {
let from = named("--from", &self.from)?;
let to = named("--to", &self.to)?;
let path = match &self.path {
Some(text) => Some(translate::parse_path(text, &self.limits)?),
None => None,
};
Ok(Resolved { from, to, path })
}
}
struct Resolved {
from: &'static Format,
to: &'static Format,
path: Option<Vec<Segment>>,
}
fn named(option: &str, id: &str) -> Result<&'static Format, Fail> {
translate::format(id)
.ok_or_else(|| Fail::input(format!("{option} takes {}, not {id:?}", translate::names())))
}
pub struct Translation {
settings: Settings,
compiled: Compiled,
}
struct Settings {
from: &'static Format,
to: &'static Format,
path: Option<Vec<Segment>>,
options: Options,
with: Option<Source>,
whole: bool,
limits: Limits,
}
impl Settings {
fn request(&self) -> Request<'_> {
Request {
from: self.from,
to: self.to,
path: self.path.clone(),
options: self.options.clone(),
program: self
.with
.as_ref()
.map(|with| (with.name.as_str(), with.text.as_str())),
whole: self.whole,
limits: self.limits.clone(),
}
}
}
impl std::fmt::Debug for Translation {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Translation")
.field("from", &self.settings.from.id())
.field("to", &self.settings.to.id())
.field("path", &self.settings.path)
.field("key", &self.settings.options.key)
.field("with", &self.settings.with.as_ref().map(|s| &s.name))
.field("whole", &self.settings.whole)
.finish()
}
}
impl Translation {
pub fn new(options: TranslateOptions) -> Result<Translation, Fail> {
on_stack(move || {
let Resolved { from, to, path } = options.resolve()?;
let TranslateOptions {
key,
with,
whole,
limits,
..
} = options;
let settings = Settings {
from,
to,
path,
options: Options {
key: key.unwrap_or_else(|| Options::default().key),
},
with,
whole,
limits,
};
let compiled = translate::compile(&settings.request())?;
Ok(Translation { settings, compiled })
})
}
pub fn run<R, W>(&self, input: R, output: W) -> Result<(), Fail>
where
R: Read + Send + 'static,
W: Write + Send + 'static,
{
self.run_with_abort(input, output, &Abort::new())
}
pub fn run_with_abort<R, W>(&self, input: R, output: W, abort: &Abort) -> Result<(), Fail>
where
R: Read + Send + 'static,
W: Write + Send + 'static,
{
run_on_stack(abort, |flag| {
self.run_document(Document::Reader(Box::new(input)), Box::new(output), flag)
})
}
pub fn run_str(&self, input: &str) -> Result<String, Fail> {
let written = Written::default();
let output = written.clone();
run_on_stack(&Abort::new(), |flag| {
self.run_document(Document::Text(input), Box::new(output), flag)
})?;
written.text()
}
fn run_document(
&self,
document: Document<'_>,
output: Box<dyn Write + Send>,
abort: &AbortFlag,
) -> Result<(), Fail> {
translate::run_document_with_abort(
&self.settings.request(),
&self.compiled,
document,
output,
Metrics::new(),
abort,
)
}
}
pub fn translate<R, W>(input: R, output: W, options: TranslateOptions) -> Result<(), Fail>
where
R: Read + Send + 'static,
W: Write + Send + 'static,
{
Translation::new(options)?.run(input, output)
}
pub fn translate_str(input: &str, options: TranslateOptions) -> Result<String, Fail> {
Translation::new(options)?.run_str(input)
}