use super::*;
pub type Str = String;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct CommandId(pub u32);
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Sugar {
Plain,
Quote,
Choice,
}
impl Sugar {
pub fn name(self) -> &'static str {
match self {
Sugar::Plain => "plain",
Sugar::Quote => "quote",
Sugar::Choice => "choice",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Binding {
Command(CommandId),
Ignored,
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CommandKind {
Effect,
Query,
Stop,
}
impl CommandKind {
pub fn name(self) -> &'static str {
match self {
CommandKind::Effect => "effect",
CommandKind::Query => "query",
CommandKind::Stop => "stop",
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct StopDecl {
pub name: String,
pub args: Vec<Ty>,
pub ret: Ty,
pub package: Option<String>,
pub params: Vec<Option<String>>,
pub defaults: Vec<Option<Value>>,
pub doc: Option<String>,
pub text_param: Option<usize>,
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Kind {
Save,
Persistent,
}
#[derive(Debug, Clone, PartialEq)]
pub struct VarDecl {
pub name: String,
pub ty: Ty,
pub kind: Kind,
pub init: Value,
pub read_only: bool,
pub doc: Option<String>,
}
#[derive(Debug)]
pub struct VarHandle<'r> {
pub(super) var: Option<&'r mut VarDecl>,
}
impl VarHandle<'_> {
pub fn read_only(mut self) -> Self {
if let Some(v) = self.var.as_deref_mut() {
v.read_only = true;
}
self
}
pub fn doc(mut self, text: &str) -> Self {
if let Some(v) = self.var.as_deref_mut() {
v.doc = Some(text.to_string());
}
self
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum NativeKind {
Effect,
Query,
}
pub(crate) type NativeFn =
Arc<dyn Fn(Env<'_>, &[Value]) -> Result<Value, CommandError> + Send + Sync>;
#[derive(Clone)]
pub(crate) struct NativeDecl {
pub name: String,
pub kind: NativeKind,
pub args: Vec<Ty>,
pub ret: Ty,
pub package: usize,
pub func: NativeFn,
pub params: Vec<Option<String>>,
pub defaults: Vec<Option<Value>>,
pub doc: Option<String>,
}
impl std::fmt::Debug for NativeDecl {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("NativeDecl")
.field("name", &self.name)
.field("kind", &self.kind)
.finish_non_exhaustive()
}
}
#[derive(Clone)]
pub(crate) struct StopCheck(Arc<StopCheckFn>);
type StopCheckFn = dyn Fn(&[Value]) -> Result<(), CommandError> + Send + Sync;
impl StopCheck {
pub(crate) fn new(
f: impl Fn(&[Value]) -> Result<(), CommandError> + Send + Sync + 'static,
) -> Self {
Self(Arc::new(f))
}
pub(crate) fn run(&self, args: &[Value]) -> Result<(), CommandError> {
(self.0)(args)
}
}
impl std::fmt::Debug for StopCheck {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "StopCheck(..)")
}
}
#[derive(Clone)]
pub(crate) struct RetCheck(Arc<RetCheckFn>);
type RetCheckFn = dyn Fn(&[Value], &Value) -> Result<(), CommandError> + Send + Sync;
impl RetCheck {
pub(crate) fn new(
f: impl Fn(&[Value], &Value) -> Result<(), CommandError> + Send + Sync + 'static,
) -> Self {
Self(Arc::new(f))
}
pub(crate) fn run(&self, args: &[Value], ret: &Value) -> Result<(), CommandError> {
(self.0)(args, ret)
}
}
impl std::fmt::Debug for RetCheck {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "RetCheck(..)")
}
}
#[derive(Debug, Clone)]
pub(crate) struct CommandInfo {
pub name: String,
pub via_alias: bool,
pub kind: CommandKind,
pub args: Vec<Ty>,
pub ret: Ty,
pub package: Option<String>,
pub params: Vec<Option<String>>,
pub defaults: Vec<Option<Value>>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct CommandDecl {
pub name: String,
pub kind: CommandKind,
pub args: Vec<Ty>,
pub ret: Ty,
pub params: Vec<Option<String>>,
pub aliases: Vec<String>,
pub doc: Option<String>,
pub package: Option<String>,
pub defaults: Vec<Option<Value>>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PackageDecl {
pub name: String,
pub version: u32,
pub persistent_version: u32,
pub depends: Vec<String>,
}
#[derive(Clone, Copy)]
pub struct PackageItem {
pub(super) name: &'static str,
pub(super) depends: &'static [&'static str],
pub(super) stage: fn(&mut Registry) -> Result<(), RegistryError>,
}
impl PackageItem {
pub fn of<P: Package>() -> Self {
Self {
name: P::NAME,
depends: P::DEPENDS,
stage: |reg| {
reg.packages.push(PackageEntry::of::<P>());
let index = reg.packages.len() - 1;
let mut pr = PackageReg::<P>::new(reg, index);
P::register(&mut pr);
pr.finish()
},
}
}
pub fn name(&self) -> &'static str {
self.name
}
}
impl std::fmt::Debug for PackageItem {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "PackageItem({})", self.name)
}
}
#[macro_export]
macro_rules! packages {
($($p:ty),* $(,)?) => {
[$($crate::PackageItem::of::<$p>()),*]
};
}