#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MessageStyle {
SolidArrow,
DashedArrow,
SolidLine,
DashedLine,
}
impl MessageStyle {
pub fn is_dashed(self) -> bool {
matches!(self, Self::DashedArrow | Self::DashedLine)
}
pub fn has_arrow(self) -> bool {
matches!(self, Self::SolidArrow | Self::DashedArrow)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Participant {
pub id: String,
pub label: String,
}
impl Participant {
pub fn new(id: impl Into<String>) -> Self {
let id = id.into();
let label = id.clone();
Self { id, label }
}
pub fn with_label(id: impl Into<String>, label: impl Into<String>) -> Self {
Self {
id: id.into(),
label: label.into(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Message {
pub from: String,
pub to: String,
pub text: String,
pub style: MessageStyle,
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct SequenceDiagram {
pub participants: Vec<Participant>,
pub messages: Vec<Message>,
}
impl SequenceDiagram {
pub fn participant_index(&self, id: &str) -> Option<usize> {
self.participants.iter().position(|p| p.id == id)
}
pub fn ensure_participant(&mut self, id: &str) {
if self.participant_index(id).is_none() {
self.participants.push(Participant::new(id));
}
}
}