// Package ast defines AST structures for all Mermaid diagram types package ast // SequenceDiagram represents a sequence diagram // Based on sequenceDiagram.jison and sequenceDb.ts type SequenceDiagram struct { Participants []*SequenceParticipant `json:"participants"` Messages []*SequenceMessage `json:"messages"` Loops []*SequenceLoop `json:"loops"` Alts []*SequenceAlt `json:"alts"` Opts []*SequenceOpt `json:"opts"` Pars []*SequencePar `json:"pars"` Notes []*SequenceNote `json:"notes"` Boxes []*SequenceBox `json:"boxes"` Rects []*SequenceRect `json:"rects,omitempty"` Criticals []*SequenceCritical `json:"criticals,omitempty"` Breaks []*SequenceBreak `json:"breaks,omitempty"` Activations []*SequenceActivation `json:"activations"` Title *string `json:"title,omitempty"` Config map[string]any `json:"config,omitempty"` } type SequenceParticipant struct { ID string `json:"id"` Name string `json:"name"` Type ParticipantType `json:"type"` Alias *string `json:"alias,omitempty"` Description *string `json:"description,omitempty"` Config map[string]any `json:"config,omitempty"` } type ParticipantType string const ( ParticipantTypeParticipant ParticipantType = "participant" ParticipantTypeActor ParticipantType = "actor" ) type SequenceMessage struct { From string `json:"from"` To string `json:"to"` Message string `json:"message"` Type SequenceMessageType `json:"type"` Activate bool `json:"activate,omitempty"` Deactivate bool `json:"deactivate,omitempty"` Note *string `json:"note,omitempty"` } type SequenceMessageType string const ( MessageTypeSolid SequenceMessageType = "->" MessageTypeDotted SequenceMessageType = "-->" MessageTypeSolidCross SequenceMessageType = "-x" MessageTypeDottedCross SequenceMessageType = "--x" MessageTypeSolidOpen SequenceMessageType = "-)" MessageTypeDottedOpen SequenceMessageType = "--)" MessageTypeBidirectional SequenceMessageType = "<->" ) type SequenceLoop struct { Label string `json:"label"` Messages []*SequenceMessage `json:"messages"` StartLine int `json:"startLine"` EndLine int `json:"endLine"` } type SequenceAlt struct { Label string `json:"label"` IfMessages []*SequenceMessage `json:"ifMessages"` ElseMessages []*SequenceMessage `json:"elseMessages,omitempty"` StartLine int `json:"startLine"` EndLine int `json:"endLine"` } type SequenceOpt struct { Label string `json:"label"` Messages []*SequenceMessage `json:"messages"` StartLine int `json:"startLine"` EndLine int `json:"endLine"` } type SequencePar struct { Sections []SequenceParSection `json:"sections"` StartLine int `json:"startLine"` EndLine int `json:"endLine"` } type SequenceParSection struct { Label *string `json:"label,omitempty"` Messages []*SequenceMessage `json:"messages"` } type SequenceNote struct { Actor string `json:"actor"` Placement NotePlace `json:"placement"` Message string `json:"message"` } type NotePlace string const ( NotePlaceLeft NotePlace = "left of" NotePlaceRight NotePlace = "right of" NotePlaceOver NotePlace = "over" ) type SequenceBox struct { Name string `json:"name"` Color *string `json:"color,omitempty"` Participants []string `json:"participants"` } type SequenceActivation struct { Actor string `json:"actor"` Type ActivationType `json:"type"` } type ActivationType string const ( ActivationTypeActivate ActivationType = "activate" ActivationTypeDeactivate ActivationType = "deactivate" ) type SequenceRect struct { Color *string `json:"color,omitempty"` Messages []*SequenceMessage `json:"messages"` } type SequenceCritical struct { Label string `json:"label"` Options []*SequenceOption `json:"options"` Messages []*SequenceMessage `json:"messages"` } type SequenceOption struct { Label string `json:"label"` Messages []*SequenceMessage `json:"messages"` } type SequenceBreak struct { Label string `json:"label"` Messages []*SequenceMessage `json:"messages"` } // Type returns the diagram type func (s *SequenceDiagram) Type() DiagramType { return DiagramTypeSequence } // Validate checks if the sequence diagram is valid func (s *SequenceDiagram) Validate() error { participantMap := make(map[string]bool) for _, p := range s.Participants { participantMap[p.ID] = true } // Validate messages reference valid participants for _, msg := range s.Messages { if !participantMap[msg.From] { return NewValidationError("message references non-existent participant: " + msg.From) } if !participantMap[msg.To] { return NewValidationError("message references non-existent participant: " + msg.To) } } return nil } // NewSequenceDiagram creates a new sequence diagram func NewSequenceDiagram() *SequenceDiagram { return &SequenceDiagram{ Participants: make([]*SequenceParticipant, 0), Messages: make([]*SequenceMessage, 0), Loops: make([]*SequenceLoop, 0), Alts: make([]*SequenceAlt, 0), Opts: make([]*SequenceOpt, 0), Pars: make([]*SequencePar, 0), Notes: make([]*SequenceNote, 0), Boxes: make([]*SequenceBox, 0), Rects: make([]*SequenceRect, 0), Criticals: make([]*SequenceCritical, 0), Breaks: make([]*SequenceBreak, 0), Activations: make([]*SequenceActivation, 0), Config: make(map[string]any), } }