8 "github.com/mjl-/mox/mlog"
13 xwriteTo(c *conn, xw io.Writer) // Writes to xw panic on error.
18func (t bare) pack(c *conn) string {
22func (t bare) xwriteTo(c *conn, xw io.Writer) {
23 xw.Write([]byte(t.pack(c)))
30func (t niltoken) pack(c *conn) string {
34func (t niltoken) xwriteTo(c *conn, xw io.Writer) {
35 xw.Write([]byte(t.pack(c)))
38func nilOrString(s *string) token {
49func (t string0) pack(c *conn) string {
52 for _, ch := range t {
53 if ch == '\x00' || ch == '\r' || ch == '\n' || ch > 0x7f && !c.utf8strings() {
54 return syncliteral(t).pack(c)
56 if ch == '\\' || ch == '"' {
59 r.WriteString(string(ch))
65func (t string0) xwriteTo(c *conn, xw io.Writer) {
66 xw.Write([]byte(t.pack(c)))
71func (t dquote) pack(c *conn) string {
75 if c == '\\' || c == '"' {
78 r.WriteString(string(c))
84func (t dquote) xwriteTo(c *conn, xw io.Writer) {
85 xw.Write([]byte(t.pack(c)))
88type syncliteral string
90func (t syncliteral) pack(c *conn) string {
91 return fmt.Sprintf("{%d}\r\n", len(t)) + string(t)
94func (t syncliteral) xwriteTo(c *conn, xw io.Writer) {
95 fmt.Fprintf(xw, "{%d}\r\n", len(t))
99// data from reader with known size.
100type readerSizeSyncliteral struct {
106func (t readerSizeSyncliteral) pack(c *conn) string {
107 buf, err := io.ReadAll(t.r)
115 return fmt.Sprintf("%s{%d}\r\n", lit, t.size) + string(buf)
118func (t readerSizeSyncliteral) xwriteTo(c *conn, xw io.Writer) {
123 fmt.Fprintf(xw, "%s{%d}\r\n", lit, t.size)
124 defer c.xtracewrite(mlog.LevelTracedata)()
125 if _, err := io.Copy(xw, io.LimitReader(t.r, t.size)); err != nil {
130// data from reader without known size.
131type readerSyncliteral struct {
135func (t readerSyncliteral) pack(c *conn) string {
136 buf, err := io.ReadAll(t.r)
140 return fmt.Sprintf("{%d}\r\n", len(buf)) + string(buf)
143func (t readerSyncliteral) xwriteTo(c *conn, xw io.Writer) {
144 buf, err := io.ReadAll(t.r)
148 fmt.Fprintf(xw, "{%d}\r\n", len(buf))
149 defer c.xtracewrite(mlog.LevelTracedata)()
153// list with tokens space-separated
154type listspace []token
156func (t listspace) pack(c *conn) string {
157 var s strings.Builder
159 for i, e := range t {
163 s.WriteString(e.pack(c))
169func (t listspace) xwriteTo(c *conn, xw io.Writer) {
171 for i, e := range t {
180// concatenate tokens space-separated
181type concatspace []token
183func (t concatspace) pack(c *conn) string {
184 var s strings.Builder
185 for i, e := range t {
189 s.WriteString(e.pack(c))
194func (t concatspace) xwriteTo(c *conn, xw io.Writer) {
195 for i, e := range t {
203// Concatenated tokens, no spaces or list syntax.
206func (t concat) pack(c *conn) string {
207 var s strings.Builder
208 for _, e := range t {
209 s.WriteString(e.pack(c))
214func (t concat) xwriteTo(c *conn, xw io.Writer) {
215 for _, e := range t {
222func (t astring) pack(c *conn) string {
224 return string0(t).pack(c)
227 for _, ch := range t {
228 for _, x := range atomChar {
233 return string0(t).pack(c)
238func (t astring) xwriteTo(c *conn, xw io.Writer) {
239 xw.Write([]byte(t.pack(c)))
242// mailbox with utf7 encoding if connection requires it, or utf8 otherwise.
245func (t mailboxt) pack(c *conn) string {
247 if !c.utf8strings() {
250 return astring(s).pack(c)
253func (t mailboxt) xwriteTo(c *conn, xw io.Writer) {
254 xw.Write([]byte(t.pack(c)))
259func (t number) pack(c *conn) string {
260 return fmt.Sprintf("%d", t)
263func (t number) xwriteTo(c *conn, xw io.Writer) {
264 xw.Write([]byte(t.pack(c)))