package modules import ( "context" "fmt" "strings" "uberbringer/internal/pkg/receipt" ) // noteStyles are the box styles a note may ask for. var noteStyles = []string{"single", "double", "double-h", "double-v"} // Note returns the note module: a box with an optional title, the text and // an optional checklist, stamped with the time in its bottom border. func Note() Module { return note{} } type note struct{} // notePayload is the body of a note. type notePayload struct { Title string `json:"title"` Text string `json:"text"` Items []string `json:"items"` Done []int `json:"done"` Style string `json:"style"` Footer string `json:"footer"` Stamp *bool `json:"stamp"` } func (note) Name() string { return "note" } func (note) Description() string { return "Boxed note with an optional checklist" } // Build lays the note out as a box: the text, then, when there are items, // an empty line and a checklist. The footer is the payload's, or the build // time unless stamp is false. func (note) Build(_ context.Context, body []byte, env Env) (receipt.Job, error) { var p notePayload if err := DecodeStrict(body, &p); err != nil { return receipt.Job{}, err } if problems := p.problems(); len(problems) > 0 { return receipt.Job{}, &PayloadError{Problems: problems} } children := []receipt.Element{{Type: receipt.TypeText, Text: p.Text}} if len(p.Items) > 0 { children = append(children, receipt.Element{Type: receipt.TypeFeed}, receipt.Element{Type: receipt.TypeList, Bullet: "checkbox", Items: p.Items, Done: p.Done}, ) } footer := p.Footer if footer == "" && (p.Stamp == nil || *p.Stamp) { footer = env.Stamp() } box := receipt.Element{ Type: receipt.TypeBox, Title: p.Title, Style: strings.ToLower(p.Style), Footer: footer, Children: children, } return receipt.Job{Elements: []receipt.Element{box}}, nil } // problems checks what the box and its list would refuse, in the payload's // own terms. func (p notePayload) problems() []string { var problems []string if strings.TrimSpace(p.Text) == "" { problems = append(problems, "text: required") } if p.Style != "" && !containsFold(noteStyles, p.Style) { problems = append(problems, "style: must be "+strings.Join(noteStyles[:len(noteStyles)-1], ", ")+" or "+noteStyles[len(noteStyles)-1]) } if n := len(p.Items); n > receipt.MaxItems { problems = append(problems, fmt.Sprintf("items: %d items, limit is %d", n, receipt.MaxItems)) } for i, item := range p.Items { if item == "" { problems = append(problems, fmt.Sprintf("items[%d]: must not be empty", i)) } } seen := make(map[int]bool, len(p.Done)) for i, index := range p.Done { switch { case index < 0 || index >= len(p.Items): problems = append(problems, fmt.Sprintf("done[%d]: index %d is out of range", i, index)) case seen[index]: problems = append(problems, fmt.Sprintf("done[%d]: index %d is repeated", i, index)) } seen[index] = true } return problems } func containsFold(list []string, s string) bool { for _, item := range list { if strings.EqualFold(item, s) { return true } } return false }