Write DCC encoder implementation
gravestench opened this issue · comments
gravestench commented
We should write a DCC encoder as a method of the DCC
struct.
func (d *DCC) Encode(w io.Writer) error { /* ... */ }
This should fully marshal our DCC struct to a byte slice, by writing the bytes sequentially into the given stream.
The use-case, as a user of this library, looks something like this:
import (
"bufio"
"ioutil"
"os"
dcc "github.com/gravestench/dcc/pkg"
)
func main() {
// load a dcc file
fileContents, err := ioutil.ReadFile("/path/to/something.dcc")
if err != nil {
// file io error
}
// parse the dcc file data into a dcc struct
instance, err := dcc.FromBytes(fileContents)
if err != nil {
// dcc parse error
}
// re-encode the struct back to dcc data, should be identical to original file data
f, err := os.Create("/path/to/something_else.dcc")
if err != nil {
// file io error
}
defer f.Close()
stream := bufio.NewWriter(w)
if err := instance.Encode(stream); err != nil {
// dcc encode error
}
}