use std::fmt;
use std::sync::Arc;
use crate::script::FlowError;
use crate::script::scope::{NameFault, name_fault};
pub(crate) const MAX_TASK_NAME_BYTES: usize = 128;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub(crate) struct TaskName(Arc<str>);
impl TaskName {
pub(crate) fn new(name: &str) -> Result<Self, FlowError> {
let reason = match name_fault(name, MAX_TASK_NAME_BYTES) {
None => return Ok(Self(Arc::from(name))),
Some(NameFault::Empty) => "the task name is empty",
Some(NameFault::TooLong) => "the task name is longer than MAX_TASK_NAME_BYTES",
Some(NameFault::BadChar) => {
"the task name may hold only ASCII letters, digits, '-', '_', '.' and ':'"
}
};
Err(FlowError::InvalidName { reason })
}
pub(crate) fn as_str(&self) -> &str {
&self.0
}
}
impl fmt::Display for TaskName {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(&self.0)
}
}