use crate::{
loader::gnu_units,
output::{QueryError, QueryReply},
parsing::text_query,
Context,
};
#[cfg(feature = "bundle-files")]
pub static DEFAULT_FILE: Option<&'static str> = Some(include_str!("../definitions.units"));
#[cfg(not(feature = "bundle-files"))]
pub static DEFAULT_FILE: Option<&'static str> = None;
#[cfg(feature = "bundle-files")]
pub static DATES_FILE: Option<&'static str> = Some(include_str!("../datepatterns.txt"));
#[cfg(not(feature = "bundle-files"))]
pub static DATES_FILE: Option<&'static str> = None;
#[cfg(feature = "bundle-files")]
pub static CURRENCY_FILE: Option<&'static str> = Some(include_str!("../currency.units"));
#[cfg(not(feature = "bundle-files"))]
pub static CURRENCY_FILE: Option<&'static str> = None;
pub fn eval(ctx: &mut Context, line: &str) -> Result<QueryReply, QueryError> {
ctx.update_time();
let mut iter = text_query::TokenIterator::new(line.trim()).peekable();
let expr = text_query::parse_query(&mut iter);
let res = ctx.eval_query(&expr)?;
if ctx.save_previous_result {
if let QueryReply::Number(ref number_parts) = res {
if let Some(ref raw) = number_parts.raw_value {
ctx.previous_result = Some(raw.clone());
}
}
}
Ok(res)
}
pub fn one_line(ctx: &mut Context, line: &str) -> Result<String, String> {
eval(ctx, line)
.as_ref()
.map(ToString::to_string)
.map_err(ToString::to_string)
}
pub fn simple_context() -> Result<Context, String> {
let message = "bundle-files feature not enabled, cannot create simple context.";
let units = DEFAULT_FILE.ok_or(message.to_owned())?;
let mut iter = gnu_units::TokenIterator::new(&*units).peekable();
let units = gnu_units::parse(&mut iter);
let dates = DATES_FILE.ok_or(message.to_owned())?;
let dates = crate::parsing::datetime::parse_datefile(dates);
let mut ctx = Context::new();
ctx.load(units)?;
ctx.load_dates(dates);
Ok(ctx)
}
pub fn version() -> &'static str {
env!("CARGO_PKG_VERSION")
}