Make constraint kind a typed enum, remove dead next_row handling, panic on invalid kinds

This commit is contained in:
Ian Gulliver
2026-02-21 17:01:37 -07:00
parent a847c17596
commit 956bd5dd2e

View File

@@ -55,30 +55,34 @@ func (c *TimelineCell) String() string {
return fmt.Sprintf("%s/%s@%s:r%d", c.BlockID, c.Event, c.track.ID, c.row) return fmt.Sprintf("%s/%s@%s:r%d", c.BlockID, c.Event, c.track.ID, c.row)
} }
type constraintKind string
const (
constraintSameRow constraintKind = "same_row"
)
type constraint struct { type constraint struct {
kind string kind constraintKind
a *TimelineCell a *TimelineCell
b *TimelineCell b *TimelineCell
} }
func (c constraint) satisfied() bool { func (c constraint) satisfied() bool {
switch c.kind { switch c.kind {
case "same_row": case constraintSameRow:
return c.a.row == c.b.row return c.a.row == c.b.row
case "next_row": default:
return c.b.row > c.a.row panic("invalid constraint kind: " + string(c.kind))
} }
return true
} }
func (c constraint) String() string { func (c constraint) String() string {
switch c.kind { switch c.kind {
case "same_row": case constraintSameRow:
return fmt.Sprintf("same_row(%s, %s)", c.a, c.b) return fmt.Sprintf("same_row(%s, %s)", c.a, c.b)
case "next_row": default:
return fmt.Sprintf("next_row(%s -> %s)", c.a, c.b) panic("invalid constraint kind: " + string(c.kind))
} }
return fmt.Sprintf("%s(%s, %s)", c.kind, c.a, c.b)
} }
type exclusiveGroup struct { type exclusiveGroup struct {
@@ -166,7 +170,7 @@ func BuildTimeline(show *Show) (Timeline, error) {
return tl, nil return tl, nil
} }
func (tl *Timeline) addConstraint(kind string, a, b *TimelineCell) { func (tl *Timeline) addConstraint(kind constraintKind, a, b *TimelineCell) {
tl.constraints = append(tl.constraints, constraint{kind: kind, a: a, b: b}) tl.constraints = append(tl.constraints, constraint{kind: kind, a: a, b: b})
} }
@@ -243,7 +247,7 @@ func (tl *Timeline) buildConstraints() {
for _, target := range trigger.Targets { for _, target := range trigger.Targets {
t := tl.findCell(target.Block, target.Hook) t := tl.findCell(target.Block, target.Hook)
if source.track != t.track { if source.track != t.track {
tl.addConstraint("same_row", source, t) tl.addConstraint(constraintSameRow, source, t)
source.Type = CellSignal source.Type = CellSignal
} }
group.members = append(group.members, t) group.members = append(group.members, t)
@@ -281,14 +285,14 @@ func (tl *Timeline) enforceConstraints() bool {
continue continue
} }
switch c.kind { switch c.kind {
case "same_row": case constraintSameRow:
if c.a.row < c.b.row { if c.a.row < c.b.row {
tl.insertGap(c.a.track, c.a.row) tl.insertGap(c.a.track, c.a.row)
} else { } else {
tl.insertGap(c.b.track, c.b.row) tl.insertGap(c.b.track, c.b.row)
} }
case "next_row": default:
tl.insertGap(c.b.track, c.b.row) panic("invalid constraint kind: " + string(c.kind))
} }
return true return true
} }