package rules import ( "encoding/json" "errors" "flag" "fmt" "io" "io/fs" ) // Program is the checking program's work (cmd/mesh-checking-program; novox/hq ADR 0644, ADR 0656): over its arguments, // its input (read only when asked for the verdict) and the heads laid for it (nil when none are), the one JSON line it // answers, or why it refuses. `-seats` answers the seat list of the heads (SeatsOf), `-verdict` the verdict of the // facts in the input (Judged), refusing facts of a version it does not read. func Program(args []string, input func() ([]byte, error), heads fs.FS) ([]byte, error) { set := flag.NewFlagSet("mesh-checking-program", flag.ContinueOnError) set.SetOutput(io.Discard) verdict := set.Bool("verdict", false, "answer the verdict of the facts in the input") seats := set.Bool("seats", false, "answer the seat list of the heads laid for it") if err := set.Parse(args); err != nil { return nil, err } switch { case *seats: if heads == nil { return nil, errors.New("no heads are laid for it") } answer, err := SeatsOf(heads) if err != nil { return nil, err } return json.Marshal(answer) case *verdict: data, err := input() if err != nil { return nil, fmt.Errorf("the input cannot be read: %w", err) } facts, err := DecodeFacts(data) if err != nil { return nil, err } return json.Marshal(Judged(facts)) } return nil, errors.New("nothing asked: give -verdict or -seats") }