-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.go
More file actions
40 lines (34 loc) · 867 Bytes
/
parse.go
File metadata and controls
40 lines (34 loc) · 867 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package nzb
import (
"bytes"
"encoding/xml"
"io"
"os"
"github.com/smquartz/errors"
)
// FromFile parses a NZB from a file
func FromFile(path string) (*NZB, error) {
fdata, err := os.Open(path)
if err != nil {
return nil, errors.Wrapf(err, "error opening file %v", 1, path)
}
return FromReader(fdata)
}
// FromString parses a NZB from a string
func FromString(data string) (*NZB, error) {
return FromReader(bytes.NewBufferString(data))
}
// FromBytes parses a NZB from a byte slice
func FromBytes(data []byte) (*NZB, error) {
return FromReader(bytes.NewBuffer(data))
}
// FromReader parses a NZB from an io.Reader
func FromReader(buf io.Reader) (*NZB, error) {
nzb := new(NZB)
decoder := xml.NewDecoder(buf)
err := decoder.Decode(nzb)
if err != nil {
return nil, errors.Wrapf(err, "error unmarshalling XML into *NZB", 1)
}
return nzb, nil
}