~samwhited/xmpp

xmpp/commands/actions.go -rw-r--r-- 2.4 KiB
91959039Sam Whited xtime: re-enable ejabberd integration tests an hour ago
                                                                                
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright 2021 The Mellium Contributors.
// Use of this source code is governed by the BSD 2-clause
// license that can be found in the LICENSE file.

package commands

import (
	"encoding/xml"

	"mellium.im/xmlstream"
)

//go:generate go run -tags=tools golang.org/x/tools/cmd/stringer -type=Actions -linecomment

// Actions represent the next steps that can be performed in multi-stage
// commands.
type Actions uint8

// A list of possible actions.
const (
	Prev     Actions = 1 << iota // prev
	Next                         // next
	Complete                     // complete

	// Execute is a bitmask that can be used to extract the default action.
	Execute = 0x38
)

// TokenReader satisfies the xmlstream.Marshaler interface.
func (a Actions) TokenReader() xml.TokenReader {
	var attr []xml.Attr
	switch execute := (a & Execute) >> 3; execute {
	case Prev, Next, Complete:
		attr = []xml.Attr{{Name: xml.Name{Local: "execute"}, Value: execute.String()}}
	default:
	}

	var inner []xml.TokenReader
	for i := Actions(1); i <= Complete; i <<= 1 {
		if a&i == 0 {
			continue
		}
		inner = append(inner, xmlstream.Wrap(nil, xml.StartElement{
			Name: xml.Name{Local: i.String()},
		}))
	}

	return xmlstream.Wrap(
		xmlstream.MultiReader(inner...),
		xml.StartElement{
			Name: xml.Name{Local: "actions"},
			Attr: attr,
		},
	)
}

// WriteXML satisfies the xmlstream.WriterTo interface.
// It is like MarshalXML except it writes tokens to w.
func (a Actions) WriteXML(w xmlstream.TokenWriter) (n int, err error) {
	return xmlstream.Copy(w, a.TokenReader())
}

// MarshalXML satisfies xml.Marshaler.
func (a Actions) MarshalXML(e *xml.Encoder, _ xml.StartElement) error {
	_, err := a.WriteXML(e)
	return err
}

// UnmarshalXML satisfies xml.Unmarshaler.
func (a *Actions) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
	var action Actions
	for _, attr := range start.Attr {
		if attr.Name.Local == "execute" {
			switch attr.Value {
			case "prev":
				action |= Prev << 3
			case "next":
				action |= Next << 3
			case "complete":
				action |= Complete << 3
			}
			break
		}
	}
	for {
		tok, err := d.Token()
		if err != nil {
			return err
		}
		start, ok := tok.(xml.StartElement)
		if !ok {
			break
		}
		switch start.Name.Local {
		case "prev":
			action |= Prev
		case "next":
			action |= Next
		case "complete":
			action |= Complete
		}
		err = d.Skip()
		if err != nil {
			return err
		}
	}
	*a = action
	return nil
}