iowo/src/namespace/command.rs

58 lines
1.6 KiB
Rust

use std::fmt::Debug;
use std::fmt::Display;
use super::typedef::{InternalTypeDef, TypeDef};
use super::GlobalNamespace;
#[derive(Clone, Copy)]
pub struct Command<'a> {
pub(super) id: usize,
pub(super) namespace: &'a GlobalNamespace,
}
impl<'a> Command<'a> {
pub fn get_input_types(&self) -> Option<TypeDef<'a>> {
self.namespace.commands.borrow()[self.id]
.input
.as_ref()
.map(|def| TypeDef::from_internal(self.namespace, def))
}
pub fn get_output_types(&self) -> Option<TypeDef> {
self.namespace.commands.borrow()[self.id]
.output
.as_ref()
.map(|def| TypeDef::from_internal(self.namespace, def))
}
}
impl Display for Command<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let name = &self.namespace.commands.borrow()[self.id].name;
f.write_fmt(format_args!(
"{name} {} -> {}",
self.get_input_types()
.map_or("!".to_owned(), |v| v.to_string()),
self.get_output_types()
.map_or("!".to_owned(), |v| v.to_string())
))
}
}
impl Debug for Command<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let name = &self.namespace.commands.borrow()[self.id].name;
f.write_fmt(format_args!(
"{name:?} {:?} -> {:?}",
self.get_input_types(),
self.get_output_types()
))
}
}
pub(super) struct InternalCommand {
pub(super) name: String,
// gosh this is hacky
pub(super) input: Option<InternalTypeDef>,
pub(super) output: Option<InternalTypeDef>,
}