## Description The `Repr` type name was unclear — it was intended to represent a lambda calculus expression, not a "representation." This PR renames `Repr` to `Expr` throughout the registry package for clarity. - Rename `Repr` interface to `Expr` and `baseRepr` struct to `baseExpr`. - Rename `repr.go` to `expr.go`. - Rename `ID()` method to `Repr()` to indicate the representation type. - Rename `NewRepr` constructor to `NewExpr`. - Update all usages in codec, conversion, engine, process, and registry files. - Add command aliases `conv` and `eng` for `convert` and `engine` subcommands. ## Benefits - The naming better reflects the domain: an `Expr` is an expression, and `Repr()` returns its representation kind. - Command aliases reduce typing for common subcommands. ## Checklist - [x] Code follows conventional commit format. - [x] Branch follows naming convention (`<type>/<description>`). Always use underscores. - [x] Tests pass (if applicable). - [x] Documentation updated (if applicable). Reviewed-on: #44 Co-authored-by: M.V. Hutz <git@maximhutz.me> Co-committed-by: M.V. Hutz <git@maximhutz.me>
44 lines
921 B
Go
44 lines
921 B
Go
package registry
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"git.maximhutz.com/max/lambda/pkg/codec"
|
|
)
|
|
|
|
type Conversion interface {
|
|
InType() string
|
|
OutType() string
|
|
|
|
Run(Expr) (Expr, error)
|
|
}
|
|
|
|
type convertedConversion[T, U any] struct {
|
|
conversion codec.Conversion[T, U]
|
|
inType, outType string
|
|
}
|
|
|
|
func (c convertedConversion[T, U]) Run(expr Expr) (Expr, error) {
|
|
t, ok := expr.Data().(T)
|
|
if !ok {
|
|
return nil, fmt.Errorf("could not parse '%v' as '%s'", t, c.inType)
|
|
}
|
|
|
|
u, err := c.conversion(t)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return NewExpr(c.outType, u), nil
|
|
}
|
|
|
|
func (c convertedConversion[T, U]) InType() string { return c.inType }
|
|
|
|
func (c convertedConversion[T, U]) OutType() string { return c.outType }
|
|
|
|
func RegisterConversion[T, U any](registry *Registry, conversion func(T) (U, error), inType, outType string) error {
|
|
registry.converter.Add(convertedConversion[T, U]{conversion, inType, outType})
|
|
|
|
return nil
|
|
}
|