rstango 0.3.0

Implementation of the Tango distributed control system
Documentation
//! Implementation of command instantiations.

use std::sync::Arc;

use crate::error::{Error, Result};
use crate::value::{CommandDataKind, Value};
use crate::dev::CommandInfo;

use super::dev::{some_doc, Access, Dev, ModelRef};
use super::def::{CommandDef, ExecFn};
use super::Model;


/// A command of a single device, which is what the server runs when a client asks for it,
/// around the definition it was defined with.
/// 
/// The definition is shared with the class when the class defines it, and owned by the
/// device alone when the device added it for itself, see [`Dev::add_command`].
pub struct Command<M: Model> {
    /// Comman def, behind a pointer because it's a huge structure..
    def: Arc<CommandDef<M>>,
}

impl<M: Model> Command<M> {

    /// A new command of a device, from its definition.
    pub(super) fn new(def: Arc<CommandDef<M>>) -> Self {
        Self { def }
    }

    /// The name of that command, as its definition gave it.
    #[inline]
    pub fn name(&self) -> &str {
        &self.def.name
    }

    /// The access executing that command needs to the model.
    pub(super) fn access(&self) -> Access {
        match &self.def.exec {
            Some(ExecFn::ExclusiveSync(_) | ExecFn::ExclusiveAsync(_)) => Access::Exclusive,
            _ => Access::Shared,
        }
    }

    /// Whether executing that command may block, its execution function being synchronous,
    /// see [`crate::pool::run`].
    pub(super) fn may_block(&self) -> bool {
        matches!(&self.def.exec, Some(ExecFn::SharedSync(_) | ExecFn::ExclusiveSync(_)))
    }

    /// What a client is told of that command: what its definition advertises, a side of it
    /// no execution function derived a kind for being void.
    pub fn info(&self) -> CommandInfo {
        CommandInfo {
            display_level: self.def.display_level,
            in_kind: self.def.in_kind.unwrap_or(CommandDataKind::Void),
            out_kind: self.def.out_kind.unwrap_or(CommandDataKind::Void),
            in_doc: some_doc(&self.def.in_doc),
            out_doc: some_doc(&self.def.out_doc),
        }
    }

    /// Execute that command on the model, borrowed with the access [`Self::access`] asked
    /// for, and give its result.
    pub(super) async fn exec(&self, dev: &Dev<M>, model: &mut ModelRef<'_, M>, input: Value) -> Result<Value> {
        match &self.def.exec {
            Some(ExecFn::SharedSync(func)) => func(model.shared(), dev, input),
            Some(ExecFn::ExclusiveSync(func)) => func(model.exclusive()?, dev, input),
            Some(ExecFn::SharedAsync(func)) => func(model.shared(), dev, input).await,
            Some(ExecFn::ExclusiveAsync(func)) => func(model.exclusive()?, dev, input).await,
            None => Err(Error::not_implemented()
                .with_description(format!("The command {} of the device {} has no execution function", self.name(), dev.name()))),
        }
    }

}