summaryrefslogtreecommitdiff
path: root/src/post/format.go
blob: 21ef79fb3653f2125ed866a2f82008bcf9c90bcd (plain)
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
package post

import "errors"

// ErrFormatStringMalformed indicates that a string could not be converted to a
// Format.
var ErrFormatStringMalformed = errors.New("format string malformed")

// Format describes the format of the body of a Post.
type Format string

// Enumeration of possible formats.
const (
	FormatMarkdown Format = "md"
	FormatGemtext  Format = "gmi"
)

// Formats slice of all possible Formats.
var Formats = []Format{
	FormatMarkdown,
	FormatGemtext,
}

var strsToFormats = func() map[string]Format {

	m := map[string]Format{}

	for _, f := range Formats {
		m[string(f)] = f
	}

	return m
}()

// FormatFromString parses a string into a Format, or returns
// ErrFormatStringMalformed.
func FormatFromString(str string) (Format, error) {

	if f, ok := strsToFormats[str]; ok {
		return f, nil
	}

	return "", ErrFormatStringMalformed
}