70 lines
2.0 KiB
Go
70 lines
2.0 KiB
Go
// Package scenedescriptions owns reusable scene-description facts.
|
|
package scenedescriptions
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
|
|
)
|
|
|
|
type Field string
|
|
|
|
const (
|
|
ScenesMissing Field = "scenes_missing"
|
|
ScenesEmpty Field = "scenes_empty"
|
|
ID Field = "id"
|
|
Kind Field = "kind"
|
|
Title Field = "title"
|
|
Summary Field = "summary"
|
|
SourceRef Field = "source_ref"
|
|
)
|
|
|
|
// Issue identifies a malformed field; Index is -1 for the list itself.
|
|
type Issue struct {
|
|
Index int
|
|
Field Field
|
|
}
|
|
|
|
// Check reports shape facts without choosing a validator policy or guidance.
|
|
func Check(value dnd.SceneDescriptionList) []Issue {
|
|
var issues []Issue
|
|
if value.Scenes == nil {
|
|
issues = append(issues, Issue{Index: -1, Field: ScenesMissing})
|
|
} else if len(value.Scenes) == 0 {
|
|
issues = append(issues, Issue{Index: -1, Field: ScenesEmpty})
|
|
}
|
|
for index, scene := range value.Scenes {
|
|
if !nonblankTrimmed(scene.ID) {
|
|
issues = append(issues, Issue{Index: index, Field: ID})
|
|
}
|
|
if !ValidKind(scene.Kind) {
|
|
issues = append(issues, Issue{Index: index, Field: Kind})
|
|
}
|
|
if !nonblankTrimmed(scene.Title) {
|
|
issues = append(issues, Issue{Index: index, Field: Title})
|
|
}
|
|
if !nonblankTrimmed(scene.Summary) {
|
|
issues = append(issues, Issue{Index: index, Field: Summary})
|
|
}
|
|
if !nonblankTrimmed(scene.SourceRef.SourceID) || scene.SourceRef.StartUnitID <= 0 || scene.SourceRef.EndUnitID <= 0 {
|
|
issues = append(issues, Issue{Index: index, Field: SourceRef})
|
|
}
|
|
}
|
|
return issues
|
|
}
|
|
|
|
func nonblankTrimmed(value string) bool {
|
|
return strings.TrimSpace(value) != "" && value == strings.TrimSpace(value)
|
|
}
|
|
|
|
// ValidKind is shared with the durable codec, whose other checks intentionally
|
|
// differ from the selectable shape validator.
|
|
func ValidKind(value dnd.SceneKind) bool {
|
|
switch value {
|
|
case dnd.SceneKindCombat, dnd.SceneKindNarrative, dnd.SceneKindRecap, dnd.SceneKindMeta:
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|