use crate::ConstString;
use super::{error::Error, is_allowed_port_name, port_direction::PortDirection};
#[derive(Clone, Debug)]
pub struct PortDefinition {
direction: PortDirection,
type_name: &'static str,
name: &'static str,
default_value: ConstString,
description: &'static str,
}
impl PortDefinition {
pub fn new(
direction: PortDirection,
type_name: &'static str,
name: &'static str,
default_value: &str,
description: &'static str,
) -> Result<Self, Error> {
if is_allowed_port_name(name) {
Ok(Self {
direction,
type_name,
name,
default_value: default_value.into(),
description,
})
} else {
Err(Error::NameNotAllowed { port: name.into() })
}
}
#[must_use]
pub const fn name(&self) -> &'static str {
self.name
}
#[must_use]
pub const fn direction(&self) -> &PortDirection {
&self.direction
}
#[must_use]
pub fn default_value(&self) -> Option<&ConstString> {
if self.default_value.is_empty() {
None
} else {
Some(&self.default_value)
}
}
#[must_use]
pub(crate) const fn type_name(&self) -> &'static str {
self.type_name
}
#[must_use]
pub(crate) const fn description(&self) -> &'static str {
self.description
}
}