1package headless23import (4 "encoding/json"5 "fmt"6 "io"78 "src.rest/cat/012/internal/nuon"9)1011// Encode writes v, a value of this package's result types, as JSON12// (indented, one value) or NUON, the forms --format json and nuon ask13// for. NUON is the JSON read as nushell reads it, so both carry the14// same schema (docs/reference/json.md).15func Encode(w io.Writer, format string, v any) error {16 data, err := json.MarshalIndent(v, "", " ")17 if err != nil {18 return err19 }20 switch format {21 case "json":22 case "nuon":23 val, err := nuon.Parse(data)24 if err != nil {25 return err26 }27 data = nuon.Append(nil, val)28 default:29 return fmt.Errorf("can't write %s", format)30 }31 _, err = w.Write(append(data, '\n'))32 return err33}