use std::sync::Arc;
use canvist_model::Attrs;
use canvist_model::Value;
use crate::Command;
use crate::CommandMeta;
use crate::EditorError;
use crate::EditorState;
use crate::Field;
use crate::QueryMeta;
use crate::ValueType;
#[derive(Clone, Copy, Debug)]
pub struct Args<'a> {
owner: &'static str,
fields: &'a [Field],
given: &'a Attrs,
}
impl<'a> Args<'a> {
fn check(
owner: &'static str,
fields: &'a [Field],
given: &'a Attrs,
) -> Result<Self, EditorError> {
let invalid = |reason: String| {
EditorError::InvalidArgs {
name: owner.to_owned(),
reason,
}
};
if let Some((unknown, _)) = given
.iter()
.find(|(key, _)| !fields.iter().any(|field| field.name == *key))
{
return Err(invalid(format!("unknown argument `{unknown}`")));
}
for field in fields {
match (given.get(field.name), &field.default) {
(Some(value), _) if !field.ty.accepts(value) => {
return Err(invalid(format!("`{}` must be {}", field.name, field.ty)));
}
(None, None) => return Err(invalid(format!("`{}` is required", field.name))),
_ => {}
}
}
Ok(Self {
owner,
fields,
given,
})
}
pub fn get(&self, name: &str) -> Result<&'a Value, EditorError> {
let field = self.fields.iter().find(|field| field.name == name);
let value = field.and_then(|field| self.given.get(name).or(field.default.as_ref()));
value.ok_or_else(|| {
EditorError::InvalidArgs {
name: self.owner.to_owned(),
reason: format!("`{name}` is not an argument"),
}
})
}
pub fn int(&self, name: &str) -> Result<i64, EditorError> {
self.get(name)?.as_int().ok_or_else(|| self.mistyped(name))
}
pub fn bool(&self, name: &str) -> Result<bool, EditorError> {
self.get(name)?.as_bool().ok_or_else(|| self.mistyped(name))
}
pub fn string(&self, name: &str) -> Result<&'a str, EditorError> {
self.get(name)?.as_str().ok_or_else(|| self.mistyped(name))
}
pub fn optional_string(&self, name: &str) -> Result<Option<&'a str>, EditorError> {
Ok(self.get(name)?.as_str())
}
fn mistyped(&self, name: &str) -> EditorError {
EditorError::InvalidArgs {
name: self.owner.to_owned(),
reason: format!("`{name}` has the wrong type"),
}
}
}
pub type CommandBuilder = Arc<dyn Fn(&Args<'_>) -> Result<Command, EditorError> + Send + Sync>;
#[derive(Clone)]
pub struct CommandSpec {
meta: CommandMeta,
build: CommandBuilder,
}
impl std::fmt::Debug for CommandSpec {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter
.debug_struct("CommandSpec")
.field("meta", &self.meta)
.finish_non_exhaustive()
}
}
impl CommandSpec {
pub fn new(
name: &'static str,
doc: &'static str,
build: impl Fn(&Args<'_>) -> Result<Command, EditorError> + Send + Sync + 'static,
) -> Self {
Self {
meta: CommandMeta {
name,
doc: crate::meta::doc(doc),
args: Vec::new(),
},
build: Arc::new(build),
}
}
#[must_use]
pub fn simple(name: &'static str, doc: &'static str, command: Command) -> Self {
Self::new(name, doc, move |_| Ok(command.clone()))
}
#[must_use]
pub fn arg(mut self, field: Field) -> Self {
self.meta.args.push(field);
self
}
#[must_use]
pub fn meta(&self) -> &CommandMeta {
&self.meta
}
pub fn build(&self, args: &Attrs) -> Result<Command, EditorError> {
let args = Args::check(self.meta.name, &self.meta.args, args)?;
(self.build)(&args)
}
}
pub type QueryFn = Arc<dyn Fn(&EditorState, &Args<'_>) -> Result<Value, EditorError> + Send + Sync>;
#[derive(Clone)]
pub struct QuerySpec {
meta: QueryMeta,
run: QueryFn,
}
impl std::fmt::Debug for QuerySpec {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter
.debug_struct("QuerySpec")
.field("meta", &self.meta)
.finish_non_exhaustive()
}
}
impl QuerySpec {
pub fn new(
name: &'static str,
doc: &'static str,
returns: ValueType,
run: impl Fn(&EditorState, &Args<'_>) -> Result<Value, EditorError> + Send + Sync + 'static,
) -> Self {
Self {
meta: QueryMeta {
name,
doc: crate::meta::doc(doc),
args: Vec::new(),
returns,
},
run: Arc::new(run),
}
}
#[must_use]
pub fn arg(mut self, field: Field) -> Self {
self.meta.args.push(field);
self
}
#[must_use]
pub fn meta(&self) -> &QueryMeta {
&self.meta
}
pub fn run(&self, state: &EditorState, args: &Attrs) -> Result<Value, EditorError> {
let args = Args::check(self.meta.name, &self.meta.args, args)?;
(self.run)(state, &args)
}
}