aboutsummaryrefslogtreecommitdiff
path: root/model/model.go
blob: 2bc6dfb91eb846342d9771a850fa4515e5ed7458 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package model

import (
	"iter"
)

type Ident string

type Command struct {
	Name  Ident
	Args  []any
	Group Group
}

// Value returns first argument of command.
func (c *Command) Value() any {
	if len(c.Args) > 0 {
		return c.Args[0]
	}
	return nil
}

type Group []Command

// Get returns first command with given name.
func (g Group) Get(name Ident) *Command {
	for _, c := range g {
		if c.Name == name {
			return &c
		}
	}
	return nil
}

// Filter commands by predicate and returns iterator over filtered commands.
func (g Group) Filter(predicate func(c *Command) bool) iter.Seq[*Command] {
	return func(yield func(*Command) bool) {
		for _, c := range g {
			if predicate(&c) && !yield(&c) {
				return
			}
		}
	}
}