package seatdefs // Where a seat of the mesh's own contracts may be defined (novox/hq ADR 0636 §5, ADR 0634 §7): a definition names its // glossary domain; a seat of the mesh's own contracts is taken only from that domain's contracts module // (mesh--contracts), or from mesh-contracts for a domain with none. A definition moving from mesh-contracts to // its domain's contracts module is a handover: the new definer takes the seat over, and a removal from the module that is // not the seat's domain's keeps the last trunk definition until the domain's module defines it. import ( "fmt" "slices" "strings" "git.novox.be/novox/mesh-sdk/go/seatdef" ) // Fallback is the mesh's contracts module for a domain with no contracts module of its own. const Fallback = "mesh-contracts" // domainsWithContracts are the domains whose seats have a contracts module of their own (ADR 0634 §7). Every other // domain's seats are defined in Fallback. var domainsWithContracts = []string{"Delivery", "Provisioning", "Identity and access", "Health and repair", "Connectivity", "Operator and conversation"} // DomainContracts is the module that defines a domain's seats: mesh--contracts, the heading lowercased with its // spaces as dashes, or Fallback for a domain with none. func DomainContracts(domain string) string { if !slices.Contains(domainsWithContracts, domain) { return Fallback } return "mesh-" + strings.ReplaceAll(strings.ToLower(domain), " ", "-") + "-contracts" } // Own says a contracts module is one of the mesh's own: Fallback or a domain's. func Own(module string) bool { return module == Fallback || slices.ContainsFunc(seatdef.Domains, func(d string) bool { return module == DomainContracts(d) && module != Fallback }) } // Refusal is, for a definition a contracts module of the mesh's own holds, why it may not: the words naming the module // and the domain's. Empty when the module may define the seat (a module outside the mesh's own contracts defines what it // likes, ADR 0625 §7). func Refusal(module string, d seatdef.Definition) string { if !Own(module) { return "" } if want := DomainContracts(d.Domain); want != module { return fmt.Sprintf("it is a seat of the domain %q, whose seats %s defines, and %s defines it", d.Domain, want, module) } return "" } // HandsOver says a seat its holder defines is handed over to a definer: the holder is a module of the mesh's own that is // not the definer, and the definer is the seat's domain's contracts module. func HandsOver(holder, definer string, d seatdef.Definition) bool { return holder != definer && Own(holder) && DomainContracts(d.Domain) == definer } // HandoverPending says a seat a holder of the mesh's own no longer defines waits for its domain's module: the holder is // not the domain's (named by the definition it held), so its last definition stays until that module defines it. func HandoverPending(holder, domain string) bool { return Own(holder) && DomainContracts(domain) != holder }