refactor: rewrite CLI and internal architecture #41
@@ -25,11 +25,14 @@ func LambdaConvert() *cobra.Command {
|
|||||||
var inputReprFlag, outputReprFlag string
|
var inputReprFlag, outputReprFlag string
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "convert <input> <output>",
|
Use: "convert <input-file> <output-file>",
|
||||||
Short: "Convert between lambda calculus representations",
|
Short: "Convert between lambda calculus representations",
|
||||||
Args: cobra.ExactArgs(2),
|
|
||||||
SilenceUsage: true,
|
SilenceUsage: true,
|
||||||
RunE: func(_ *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
if len(args) != 2 {
|
||||||
|
return cmd.Help()
|
||||||
|
}
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
inputPath, outputPath := args[0], args[1]
|
inputPath, outputPath := args[0], args[1]
|
||||||
|
|
||||||
@@ -71,7 +74,7 @@ func LambdaConvert() *cobra.Command {
|
|||||||
// Marshal output.
|
// Marshal output.
|
||||||
output, err := r.Marshal(result)
|
output, err := r.Marshal(result)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("marshaling output: %w", err)
|
return fmt.Errorf("unmarshaling output: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write output file.
|
// Write output file.
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ type forwardCodec[T, U any] struct {
|
|||||||
func (c forwardCodec[T, U]) Run(r Repr) (Repr, error) {
|
func (c forwardCodec[T, U]) Run(r Repr) (Repr, error) {
|
||||||
t, ok := r.Data().(T)
|
t, ok := r.Data().(T)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("could not parse '%v' as '%s'", t, c.outType)
|
return nil, fmt.Errorf("could not parse '%v' as '%s'", t, c.inType)
|
||||||
}
|
}
|
||||||
|
|
||||||
u, err := c.codec.Encode(t)
|
u, err := c.codec.Encode(t)
|
||||||
@@ -29,7 +29,7 @@ func (c forwardCodec[T, U]) Run(r Repr) (Repr, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return NewRepr(c.inType, u), nil
|
return NewRepr(c.outType, u), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c forwardCodec[T, U]) InType() string { return c.inType }
|
func (c forwardCodec[T, U]) InType() string { return c.inType }
|
||||||
@@ -44,7 +44,7 @@ type backwardCodec[T, U any] struct {
|
|||||||
func (c backwardCodec[T, U]) Run(r Repr) (Repr, error) {
|
func (c backwardCodec[T, U]) Run(r Repr) (Repr, error) {
|
||||||
u, ok := r.Data().(U)
|
u, ok := r.Data().(U)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("could not parse '%v' as '%s'", r, c.inType)
|
return nil, fmt.Errorf("could not parse '%v' as '%s'", r, c.outType)
|
||||||
}
|
}
|
||||||
|
|
||||||
t, err := c.codec.Decode(u)
|
t, err := c.codec.Decode(u)
|
||||||
@@ -52,7 +52,7 @@ func (c backwardCodec[T, U]) Run(r Repr) (Repr, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return NewRepr(c.outType, t), nil
|
return NewRepr(c.inType, t), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c backwardCodec[T, U]) InType() string { return c.outType }
|
func (c backwardCodec[T, U]) InType() string { return c.outType }
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package cli
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
|
||||||
"git.maximhutz.com/max/lambda/pkg/codec"
|
"git.maximhutz.com/max/lambda/pkg/codec"
|
||||||
)
|
)
|
||||||
@@ -29,7 +30,9 @@ func (c convertedMarshaler[T]) Decode(s string) (Repr, error) {
|
|||||||
func (c convertedMarshaler[T]) Encode(r Repr) (string, error) {
|
func (c convertedMarshaler[T]) Encode(r Repr) (string, error) {
|
||||||
t, ok := r.Data().(T)
|
t, ok := r.Data().(T)
|
||||||
if !ok {
|
if !ok {
|
||||||
return "", fmt.Errorf("could not parse '%v' as 'string'", t)
|
dataType := reflect.TypeOf(r.Data())
|
||||||
|
allowedType := reflect.TypeFor[T]()
|
||||||
|
return "", fmt.Errorf("marshaler for '%s' cannot parse '%s'", allowedType, dataType)
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.codec.Encode(t)
|
return c.codec.Encode(t)
|
||||||
|
|||||||
Reference in New Issue
Block a user