go-calc/calc/registry.go

59 lines
952 B
Go

package calc
import (
"gitea.zaclys.com/bvaudour/gob/number"
"gitea.zaclys.com/bvaudour/gob/option"
)
type Registry map[string]Stack
func (r Registry) Exists(k string) (ok bool) {
_, ok = r[k]
return
}
func (r Registry) Set(k string, numbers ...number.Number) {
r[k] = numbers
}
func (r Registry) Get(k string) (result option.Result[Stack]) {
if s, ok := r[k]; ok {
result = option.Ok(s.Clone())
} else {
result = option.Err[Stack](ErrNotExist(reg(k)))
}
return
}
func (r Registry) Reset(k string) (err option.Option[error]) {
if r.Exists(k) {
delete(r, k)
} else {
err = option.Some(ErrNotExist(reg(k)))
}
return
}
func (r Registry) ResetAll() (err option.Option[error]) {
if len(r) > 0 {
for k := range r {
delete(r, k)
}
} else {
err = option.Some(ErrIsEmpty(errMemory))
}
return
}
func (r Registry) Clone() Registry {
out := make(Registry)
for k, s := range r {
out[k] = s.Clone()
}
return out
}