rules

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Jun 16, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package rules handles staghorn rule parsing, registry, and rendering.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertToClaude

func ConvertToClaude(rule *Rule) (string, error)

ConvertToClaude converts a staghorn rule to Claude Code rules format. It preserves the frontmatter (required for path-scoping) and adds a staghorn header.

Types

type Frontmatter

type Frontmatter struct {
	Paths []string `yaml:"paths,omitempty"` // Glob patterns for path-scoping
}

Frontmatter contains the YAML frontmatter of a rule.

type ParseErrors

type ParseErrors struct {
	Errors []error
}

ParseErrors collects multiple parse errors when loading rules from a directory. Individual parse failures don't prevent other rules from loading.

func (*ParseErrors) Error

func (e *ParseErrors) Error() string

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

Registry manages rules from multiple sources with precedence handling. Precedence (highest to lowest): project > personal > team Key is the relative path (e.g., "api/rest.md", "security.md")

func LoadRegistry

func LoadRegistry(teamDir, personalDir, projectDir string) (*Registry, error)

LoadRegistry creates a registry by loading rules from all sources. Empty string for any directory means that source is not available.

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates an empty rule registry.

func (*Registry) Add

func (r *Registry) Add(rule *Rule)

Add adds a rule to the registry. Higher precedence sources (project > personal > team) override lower ones.

func (*Registry) All

func (r *Registry) All() []*Rule

All returns all unique rules (highest precedence version of each), sorted by relative path for deterministic ordering.

func (*Registry) BySource

func (r *Registry) BySource(source Source) []*Rule

BySource returns all rules from a specific source.

func (*Registry) Count

func (r *Registry) Count() int

Count returns the number of unique rules.

func (*Registry) Get

func (r *Registry) Get(relPath string) *Rule

Get returns a rule by its relative path.

type Rule

type Rule struct {
	Frontmatter        // Embedded; access paths via rule.Frontmatter.Paths or rule.Paths
	Name        string // Derived from filename (e.g., "security" from "security.md")
	Body        string // Markdown content after frontmatter
	Source      Source // Where this rule came from
	FilePath    string // Absolute path to the rule file
	RelPath     string // Relative path within rules/ (preserves subdirs, e.g., "api/rest.md")
}

Rule represents a staghorn rule file.

func LoadFromDirectory

func LoadFromDirectory(dir string, source Source) ([]*Rule, error)

LoadFromDirectory loads all rules from a directory (recursive). Parse errors for individual files are collected in the returned ParseErrors slice but do not prevent other rules from loading.

func Parse

func Parse(content string, source Source, filePath, relPath string) (*Rule, error)

Parse parses a rule from markdown content.

func ParseFile

func ParseFile(path string, source Source, relPath string) (*Rule, error)

ParseFile parses a rule from a file.

func (*Rule) HasPathScope

func (r *Rule) HasPathScope() bool

HasPathScope returns true if the rule has path-specific scope.

type Source

type Source string

Source indicates where a rule came from.

const (
	SourceTeam     Source = "team"
	SourcePersonal Source = "personal"
	SourceProject  Source = "project"
	SourceStarter  Source = "starter"
)

func (Source) Label

func (s Source) Label() string

Label returns a human-readable label for the source.

Jump to

Keyboard shortcuts

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