format

package
v0.5.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 25, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package format handles message serialization using JSON frontmatter with Markdown body content. It provides message ID generation, parsing, and timestamp-based sorting utilities.

Index

Constants

View Source
const (
	CurrentSchema  = 1
	CurrentVersion = 1
)

Schema and version constants.

Variables

View Source
var (
	ErrMissingFrontmatterStart = errors.New("missing frontmatter start")
	ErrMissingFrontmatterEnd   = errors.New("missing frontmatter end")
)

Sentinel errors for message parsing.

Functions

func NewMessageID

func NewMessageID(now time.Time) (string, error)

func SortByTimestamp

func SortByTimestamp[T Timestamped](items []T)

SortByTimestamp sorts a slice of Timestamped items by time, then by ID for stability.

Types

type Header struct {
	Schema      int      `json:"schema"`
	ID          string   `json:"id"`
	From        string   `json:"from"`
	To          []string `json:"to"`
	Thread      string   `json:"thread"`
	Subject     string   `json:"subject,omitempty"`
	Created     string   `json:"created"`
	AckRequired bool     `json:"ack_required"`
	Refs        []string `json:"refs,omitempty"`
}

Header is the JSON frontmatter stored at the top of each message file.

func ParseHeader

func ParseHeader(data []byte) (Header, error)

func ReadHeader added in v0.4.0

func ReadHeader(r io.Reader) (Header, error)

func ReadHeaderFile

func ReadHeaderFile(path string) (Header, error)

type Message

type Message struct {
	Header Header
	Body   string
}

Message is the in-memory representation of a message file.

func ParseMessage

func ParseMessage(data []byte) (Message, error)

func ReadMessageFile

func ReadMessageFile(path string) (Message, error)

func (Message) Marshal

func (m Message) Marshal() ([]byte, error)

type Timestamped

type Timestamped interface {
	GetCreated() string
	GetID() string
	GetRawTime() time.Time
}

Timestamped is implemented by types that have a Created timestamp and ID for sorting.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL