use std::borrow::Cow;
#[derive(Debug, Clone, PartialEq)]
pub struct Suture {
pub(crate) id: Option<Cow<'static, str>>,
pub(crate) name: Cow<'static, str>,
pub(crate) description: Option<Cow<'static, str>>,
pub(crate) version: Option<Cow<'static, str>>,
pub(crate) binding: Bindings,
pub(crate) constants: Vec<(Cow<'static, str>, ConstantValue)>,
}
#[derive(Debug, Clone, PartialEq)]
pub enum ConstantValue {
Null,
Bool(bool),
Int(i64),
Float(f64),
String(Cow<'static, str>),
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Bindings {
Request(TrieNode),
Response(TrieNode),
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct TrieNode {
pub(crate) key: Cow<'static, str>,
pub(crate) binding: BindingTaskType,
pub(crate) targets: Vec<Cow<'static, str>>,
pub(crate) children: Vec<TrieNode>,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum BindingTaskType {
Direct,
Iterate {
start: Option<i64>,
end: Option<i64>,
step: Option<i64>,
},
IteratePattern {
pattern: Cow<'static, str>,
start: Option<i64>,
end: Option<i64>,
step: Option<i64>,
},
}
impl Suture {
pub fn id(&self) -> Option<&str> {
self.id.as_deref()
}
pub fn name(&self) -> &str {
&self.name
}
pub fn description(&self) -> Option<&str> {
self.description.as_deref()
}
pub fn version(&self) -> Option<&str> {
self.version.as_deref()
}
pub fn binding(&self) -> &Bindings {
&self.binding
}
pub fn is_request(&self) -> bool {
matches!(self.binding, Bindings::Request { .. })
}
pub fn is_response(&self) -> bool {
matches!(self.binding, Bindings::Response(_))
}
}
impl Suture {
pub fn constants(&self) -> &[(Cow<'static, str>, ConstantValue)] {
&self.constants
}
}
impl TrieNode {
pub fn key(&self) -> &str {
&self.key
}
pub fn binding(&self) -> &BindingTaskType {
&self.binding
}
pub fn targets(&self) -> &[Cow<'static, str>] {
&self.targets
}
pub fn children(&self) -> &[TrieNode] {
&self.children
}
}
impl Bindings {
pub fn root(&self) -> &TrieNode {
match self {
Bindings::Request(root) | Bindings::Response(root) => root,
}
}
}
impl Suture {
#[doc(hidden)]
pub fn __comptime(
id: Option<Cow<'static, str>>,
name: Cow<'static, str>,
description: Option<Cow<'static, str>>,
version: Option<Cow<'static, str>>,
binding: Bindings,
constants: Vec<(Cow<'static, str>, ConstantValue)>,
) -> Self {
Self {
id,
name,
description,
version,
binding,
constants,
}
}
}
impl TrieNode {
#[doc(hidden)]
pub fn __comptime(
key: Cow<'static, str>,
binding: BindingTaskType,
targets: Vec<Cow<'static, str>>,
children: Vec<TrieNode>,
) -> Self {
Self {
key,
binding,
targets,
children,
}
}
}
impl std::fmt::Display for Suture {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match (&self.id, &self.version) {
(Some(id), Some(v)) => write!(f, "{}@{} ({})", self.name, v, id),
(Some(id), None) => write!(f, "{} ({})", self.name, id),
(None, Some(v)) => write!(f, "{}@{}", self.name, v),
(None, None) => write!(f, "{}", self.name),
}
}
}