The gate's developer needs OwedEnded and its outcomes; fields are the ones §164 and §168 name (id, outcome, words, the seat that said the act, time). Builds: design 47 G §164, §168 Claude-Session: https://claude.ai/code/session_01JG73KEq4zoDfM9WtYpcQgZ
77 lines
2.8 KiB
Go
77 lines
2.8 KiB
Go
package forgemsg
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"slices"
|
|
"time"
|
|
)
|
|
|
|
// OwedEndedEvent is the name of an owed act's outcome on the forge bridge's stream `forge` (design 47 G §164): the
|
|
// bridge says it for each act the gate or the checker said as the event `owed`. Its two parts are no repository's
|
|
// forge event (`forge.<owner>.<name>.<kind>`).
|
|
const OwedEndedEvent = "owed.ended"
|
|
|
|
// The outcomes of an owed act (design 47 G §164, §168): performed, refused (the forge's own words), could not be
|
|
// performed (the forge did not answer within the call's bound, with the error), undecodable (an act the bridge cannot
|
|
// read, with why) and, from the tool `bridge_owed_drop`, dropped.
|
|
const (
|
|
OwedPerformed = "performed"
|
|
OwedRefused = "refused"
|
|
OwedCouldNotBePerformed = "could not be performed"
|
|
OwedUndecodable = "undecodable"
|
|
OwedDropped = "dropped"
|
|
)
|
|
|
|
// OwedOutcomes are the outcomes an owed.ended says, one of them.
|
|
var OwedOutcomes = []string{OwedPerformed, OwedRefused, OwedCouldNotBePerformed, OwedUndecodable, OwedDropped}
|
|
|
|
// OwedEnded is an owed act's outcome (design 47 G §164): the act's id and one outcome, with the words of any outcome
|
|
// but performed. Of is the seat that said the act (mesh-delivery for the gate's, mesh-delivery-checker for the
|
|
// checker's), so each hears its own ids (§164). An act the bridge cannot decode may name no id.
|
|
type OwedEnded struct {
|
|
V int `json:"v"`
|
|
Of string `json:"of"`
|
|
ID string `json:"id,omitempty"`
|
|
Outcome string `json:"outcome"`
|
|
Words string `json:"words,omitempty"`
|
|
At time.Time `json:"at"`
|
|
}
|
|
|
|
func (e OwedEnded) check() error {
|
|
switch {
|
|
case e.Of == "":
|
|
return fmt.Errorf("an owed act's outcome names no seat that said it")
|
|
case !slices.Contains(OwedOutcomes, e.Outcome):
|
|
return fmt.Errorf("the outcome %q of an owed act is none of %v", e.Outcome, OwedOutcomes)
|
|
case e.ID == "" && e.Outcome != OwedUndecodable:
|
|
return fmt.Errorf("the outcome %s of an owed act names no id", e.Outcome)
|
|
case e.Outcome != OwedPerformed && e.Words == "":
|
|
return fmt.Errorf("the outcome %s of the owed act %s says no words", e.Outcome, e.ID)
|
|
case e.At.IsZero():
|
|
return fmt.Errorf("the outcome of the owed act %s says no time", e.ID)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// EncodeOwedEnded is e as JSON at Version; it refuses what check refuses.
|
|
func EncodeOwedEnded(e OwedEnded) ([]byte, error) {
|
|
if err := e.check(); err != nil {
|
|
return nil, err
|
|
}
|
|
e.V = Version
|
|
return json.Marshal(e)
|
|
}
|
|
|
|
// DecodeOwedEnded reads an owed act's outcome; it refuses an unknown version and what EncodeOwedEnded refuses.
|
|
func DecodeOwedEnded(data []byte) (OwedEnded, error) {
|
|
var e OwedEnded
|
|
if err := decode(data, &e); err != nil {
|
|
return OwedEnded{}, err
|
|
}
|
|
if err := e.check(); err != nil {
|
|
return OwedEnded{}, err
|
|
}
|
|
return e, nil
|
|
}
|