2020-11-28 02:52:51 +00:00
|
|
|
package ui
|
|
|
|
|
|
|
|
// DocType represents a type of markdown document.
|
|
|
|
type DocType int
|
|
|
|
|
|
|
|
// Available document types.
|
|
|
|
const (
|
2020-12-16 22:49:48 +00:00
|
|
|
NoDocType DocType = iota
|
|
|
|
LocalDoc
|
2020-11-28 02:52:51 +00:00
|
|
|
StashedDoc
|
|
|
|
ConvertedDoc
|
|
|
|
NewsDoc
|
|
|
|
)
|
|
|
|
|
2020-12-09 20:24:24 +00:00
|
|
|
func (d DocType) String() string {
|
|
|
|
return [...]string{
|
2020-12-16 22:49:48 +00:00
|
|
|
"none",
|
2020-12-09 20:24:24 +00:00
|
|
|
"local",
|
|
|
|
"stashed",
|
|
|
|
"converted",
|
|
|
|
"news",
|
|
|
|
}[d]
|
|
|
|
}
|
|
|
|
|
2020-11-28 02:52:51 +00:00
|
|
|
// DocTypeSet is a set (in the mathematic sense) of document types.
|
|
|
|
type DocTypeSet map[DocType]struct{}
|
|
|
|
|
|
|
|
// NewDocTypeSet returns a set of document types.
|
|
|
|
func NewDocTypeSet(t ...DocType) DocTypeSet {
|
|
|
|
d := DocTypeSet(make(map[DocType]struct{}))
|
|
|
|
if len(t) > 0 {
|
|
|
|
d.Add(t...)
|
|
|
|
}
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add adds a document type of the set.
|
|
|
|
func (d *DocTypeSet) Add(t ...DocType) int {
|
|
|
|
for _, v := range t {
|
|
|
|
(*d)[v] = struct{}{}
|
|
|
|
}
|
|
|
|
return len(*d)
|
|
|
|
}
|
|
|
|
|
2020-12-01 03:12:05 +00:00
|
|
|
// Contains returns whether or not the set contains the given DocTypes.
|
2020-11-28 02:52:51 +00:00
|
|
|
func (d DocTypeSet) Contains(m ...DocType) bool {
|
|
|
|
matches := 0
|
|
|
|
for _, t := range m {
|
|
|
|
if _, found := d[t]; found {
|
|
|
|
matches++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return matches == len(m)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Difference return a DocumentType set that does not contain the given types.
|
|
|
|
func (d DocTypeSet) Difference(t ...DocType) DocTypeSet {
|
|
|
|
c := copyDocumentTypes(d)
|
|
|
|
for k := range c {
|
|
|
|
for _, docType := range t {
|
|
|
|
if k == docType {
|
|
|
|
delete(c, k)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// Equals returns whether or not the two sets are equal.
|
|
|
|
func (d DocTypeSet) Equals(other DocTypeSet) bool {
|
2020-11-28 23:30:51 +00:00
|
|
|
return d.Contains(other.AsSlice()...) && len(d) == len(other)
|
2020-11-28 02:52:51 +00:00
|
|
|
}
|
|
|
|
|
2020-11-28 23:30:51 +00:00
|
|
|
// AsSlice returns the set as a slice of document types.
|
|
|
|
func (d DocTypeSet) AsSlice() (agg []DocType) {
|
2020-11-28 02:52:51 +00:00
|
|
|
for k := range d {
|
|
|
|
agg = append(agg, k)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-01 03:38:00 +00:00
|
|
|
// Return a copy of the given DoctTypes map.
|
2020-11-28 02:52:51 +00:00
|
|
|
func copyDocumentTypes(d DocTypeSet) DocTypeSet {
|
|
|
|
c := make(map[DocType]struct{})
|
|
|
|
for k, v := range d {
|
|
|
|
c[k] = v
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|