diff options
author | Brian Picciano <mediocregopher@gmail.com> | 2023-01-18 20:15:12 +0100 |
---|---|---|
committer | Brian Picciano <mediocregopher@gmail.com> | 2023-01-18 20:15:12 +0100 |
commit | e7b5b55f6718b25a437a891a06a26c21384b6818 (patch) | |
tree | 6c95d222f00806e2d3b335bfd0d04c4be34c740f /src/post/format.go | |
parent | 4878495914fb9701bedc242eb5087394138c8ee3 (diff) |
Add format column to post tables
Diffstat (limited to 'src/post/format.go')
-rw-r--r-- | src/post/format.go | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/post/format.go b/src/post/format.go new file mode 100644 index 0000000..21ef79f --- /dev/null +++ b/src/post/format.go @@ -0,0 +1,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 +} |