From 9912146d21acbb1ec18680257d1c8d01e746c2c5 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Mon, 28 Aug 2023 23:46:39 +0000 Subject: [PATCH] Decoder: WIP Signed-off-by: Alejandro Mery --- decoder.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 decoder.go diff --git a/decoder.go b/decoder.go new file mode 100644 index 0000000..4cdff7a --- /dev/null +++ b/decoder.go @@ -0,0 +1,49 @@ +package ini + +import ( + "bytes" + "io" + "strings" + + "asciigoat.org/core" + "asciigoat.org/ini/parser" +) + +// Decoder ... +type Decoder struct { + io.Closer + + p *parser.Parser +} + +// Decode ... +func (dec *Decoder) Decode() error { + defer dec.Close() + + return dec.p.Run() +} + +// NewDecoder creates a Decoder over the provided [io.Reader] +func NewDecoder(r io.Reader) *Decoder { + rc := core.NewReadCloser(r) + switch { + case rc == nil: + return nil + default: + dec := &Decoder{ + p: parser.NewParser(rc), + Closer: rc, + } + return dec + } +} + +// NewDecoderBytes creates a Decoder over a provided bytes array +func NewDecoderBytes(b []byte) *Decoder { + return NewDecoder(bytes.NewReader(b)) +} + +// NewDecoderString creates a Decoder over a provided string of data +func NewDecoderString(s string) *Decoder { + return NewDecoder(strings.NewReader(s)) +}