forked from Silverfish/proton-bridge
feat: initial parser exposing walker/writer
This commit is contained in:
48
pkg/message/parser/writer.go
Normal file
48
pkg/message/parser/writer.go
Normal file
@ -0,0 +1,48 @@
|
||||
package parser
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
||||
"github.com/emersion/go-message"
|
||||
)
|
||||
|
||||
type Writer struct {
|
||||
root *Part
|
||||
cond []Condition
|
||||
}
|
||||
|
||||
type Condition func(p *Part) bool
|
||||
|
||||
func newWriter(root *Part) *Writer {
|
||||
return &Writer{
|
||||
root: root,
|
||||
}
|
||||
}
|
||||
|
||||
func (w *Writer) WithCondition(cond Condition) *Writer {
|
||||
w.cond = append(w.cond, cond)
|
||||
return w
|
||||
}
|
||||
|
||||
func (w *Writer) Write(ww io.Writer) (err error) {
|
||||
msgWriter, err := message.CreateWriter(ww, w.root.Header)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if err = w.root.write(msgWriter, w); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return msgWriter.Close()
|
||||
}
|
||||
|
||||
func (w *Writer) shouldWrite(p *Part) bool {
|
||||
for _, cond := range w.cond {
|
||||
if !cond(p) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
Reference in New Issue
Block a user