A fast and simple blog backend.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
mediocre-blog/src/post/format.go

44 lines
859 B

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
}