rules

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package rules implements declarative, layered content rules.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Applies

func Applies(rule CompiledRule, target string) bool

func DifferingKeys

func DifferingKeys(a, b RuleSpec) []string

DifferingKeys returns normalized semantic fields that differ between specs.

func IsDirConfigPath

func IsDirConfigPath(target string) bool

func IsStdlib

func IsStdlib(name string) bool

func Matches

func Matches(spec RuleSpec, rel string) bool

func PackageOf

func PackageOf(member string) string

PackageOf returns the package path that owns a member path.

func Register

func Register(member Member)

func SortDiagnostics

func SortDiagnostics(d []validation.Diagnostic)

func Unify

func Unify(layers []Layer) (map[string]UnifiedRule, error)

func WriteDoc

func WriteDoc(w io.Writer, manifest Manifest)

WriteDoc renders one member's documentation from its manifest.

func WriteList

func WriteList(w io.Writer, members []Member)

WriteList renders members in a consistently aligned table. Registry.All and PackageMembers both provide members in lexical order.

Types

type BundleRootError

type BundleRootError struct {
	Root    string
	Docsets []string
}

BundleRootError reports that validation was requested above independently governed docsets. Running bundle rules there would silently choose the wrong configuration boundary.

func (*BundleRootError) Error

func (e *BundleRootError) Error() string

type Check

type Check interface {
	Evaluate(context.Context, Subject) ([]Finding, error)
	OnWrite(context.Context, string, []byte, string) error
	OnRemove(context.Context, string, string) error
	OnMove(context.Context, string, string) error
}

type CompiledRule

type CompiledRule struct {
	Name    string
	Spec    RuleSpec
	Origins []string
	Scope   string
	Member  Member
	Check   Check
}

func Compile

func Compile(reg *Registry, env func(string) Env, unified map[string]UnifiedRule) ([]CompiledRule, error)

type Defaults

type Defaults struct {
	Growth float64
}

type Engine

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

func New

func New(options Options) *Engine

func (*Engine) AdmitLeaf

func (e *Engine) AdmitLeaf(ctx context.Context, leaf vfs.Change, actor string, existing func() ([]byte, bool, error)) error

func (*Engine) CheckConfigFile

func (e *Engine) CheckConfigFile(ctx context.Context, dir string, content []byte) error

CheckConfigFile validates a proposed .lore/config.yaml against every layer above its containing directory.

func (*Engine) Effective

func (e *Engine) Effective(ctx context.Context, target string) ([]CompiledRule, error)

func (*Engine) Invalidate

func (e *Engine) Invalidate(dir string)

Invalidate drops cached folder layers at and below dir.

func (*Engine) OnMove

func (e *Engine) OnMove(ctx context.Context, from, to string) error

func (*Engine) OnRemove

func (e *Engine) OnRemove(ctx context.Context, target, actor string) error

func (*Engine) OnWrite

func (e *Engine) OnWrite(ctx context.Context, target string, content []byte, actor string) error

func (*Engine) PreApplyLeaf

func (e *Engine) PreApplyLeaf(ctx context.Context, leaf vfs.Change, actor string, existing func() ([]byte, bool, error)) error

func (*Engine) ValidateBundle

func (e *Engine) ValidateBundle(ctx context.Context, fsys vfs.FileSystem, root string) ([]validation.Diagnostic, error)

func (*Engine) ValidateFile

func (e *Engine) ValidateFile(ctx context.Context, fsys vfs.FileSystem, bundleRoot, target string, content []byte) []validation.Diagnostic

func (*Engine) Validator

func (e *Engine) Validator() validation.Validator

type Env

type Env struct {
	Defaults  Defaults
	State     packagestate.Store
	Tokenizer tokenizer.Tokenizer
	Logger    *slog.Logger
	// AliasRoots contains configured docset aliases, longest first.
	AliasRoots []string
}

type FileConfig

type FileConfig struct {
	Version    int                 `yaml:"version"`
	Rules      map[string]RuleSpec `yaml:"rules,omitempty"`
	Packages   map[string]any      `yaml:"packages,omitempty"`
	Hooks      map[string]any      `yaml:"hooks,omitempty"`
	Operations map[string]any      `yaml:"operations,omitempty"`
}

FileConfig is the strictly decoded contents of a folder's .lore/config.yaml file.

func DecodeFile

func DecodeFile(content []byte) (FileConfig, error)

DecodeFile decodes a folder rule configuration. Reserved sections are accepted while empty so configurations remain forward-compatible.

type Finding

type Finding struct {
	Code   string
	Path   string
	Line   int
	Column int
	// Warning never rejects a write and remains a warning during validation,
	// even when its enclosing rule is enforcing.
	Warning  bool
	Measured string
	Limit    string
	Remedy   string
	Override string
}

type FolderLayerSource

type FolderLayerSource interface {
	LayersForDir(context.Context, string) ([]Layer, error)
	LayersAbove(context.Context, string) ([]Layer, error)
	Invalidate(string)
}

FolderLayerSource additionally supports validating a proposed folder config without reading the current config in that same folder.

type Kind

type Kind string
const (
	KindRule      Kind = "rule"
	KindHook      Kind = "hook"
	KindOperation Kind = "operation"
)

type Layer

type Layer struct {
	Origin string
	Scope  string
	Rules  map[string]RuleSpec
}

type LayerSource

type LayerSource interface {
	LayersFor(context.Context, string) ([]Layer, error)
}

type Manifest

type Manifest struct {
	Path    string
	Kind    Kind
	Scope   Scope
	Summary string
	Doc     string
	Params  []Param
	Example string
}

type Member

type Member interface {
	Manifest() Manifest
	Compile(with map[string]any, env Env) (Check, error)
}

func PackageMembers

func PackageMembers(registry *Registry, packagePath string) []Member

PackageMembers returns the registered members beneath a package path.

type Mode

type Mode int
const (
	ModeAdmit Mode = iota
	ModePreApply
	ModeValidate
)

type Options

type Options struct {
	Registry *Registry
	Config   LayerSource
	Folders  FolderLayerSource
	Env      func(string) Env
	Logger   *slog.Logger
}

type Param

type Param struct {
	Name     string
	Type     ParamType
	Required bool
	Default  any
	Doc      string
}

type ParamType

type ParamType string
const (
	ParamInteger          ParamType = "integer"
	ParamNumber           ParamType = "number"
	ParamString           ParamType = "string"
	ParamBool             ParamType = "boolean"
	ParamIntegerOrInitial ParamType = "integer|initial"
)

type Registry

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

func DefaultRegistry

func DefaultRegistry() *Registry

func NewRegistry

func NewRegistry() *Registry

func (*Registry) All

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

func (*Registry) Lookup

func (r *Registry) Lookup(name string) (Member, bool)

func (*Registry) Register

func (r *Registry) Register(member Member)

func (*Registry) Suggest

func (r *Registry) Suggest(name string) string

type Rejection

type Rejection struct {
	Path     string
	Rule     string
	Member   string
	Origin   string
	Findings []Finding
	Err      error
}

func (*Rejection) Error

func (r *Rejection) Error() string

func (*Rejection) Unwrap

func (r *Rejection) Unwrap() error

type RuleSpec

type RuleSpec struct {
	Match   []string       `yaml:"match" json:"match"`
	Exclude []string       `yaml:"exclude,omitempty" json:"exclude,omitempty"`
	Use     string         `yaml:"use" json:"use"`
	With    map[string]any `yaml:"with,omitempty" json:"with,omitempty"`
	Enforce *bool          `yaml:"enforce,omitempty" json:"enforce,omitempty"`
	Default bool           `yaml:"default,omitempty" json:"default,omitempty"`
}

func (RuleSpec) Equal

func (s RuleSpec) Equal(other RuleSpec) bool

Equal compares normalized rule semantics. In particular, an omitted enforce value is equal to enforce: true and nil/empty collections are equivalent.

func (RuleSpec) IsEnforcing

func (s RuleSpec) IsEnforcing() bool

type Scope

type Scope string
const (
	ScopeFile   Scope = "file"
	ScopeBundle Scope = "bundle"
)

type Subject

type Subject struct {
	Mode       Mode
	Path       string
	Dir        string
	Content    []byte
	Existing   func() ([]byte, bool, error)
	Commit     string
	Actor      string
	FS         vfs.FileSystem
	BundleRoot string
	Bundle     *validation.Bundle
}

type UnificationError

type UnificationError struct {
	Rule          string
	OuterOrigin   string
	InnerOrigin   string
	DifferingKeys []string
}

func (*UnificationError) Error

func (e *UnificationError) Error() string

type UnifiedRule

type UnifiedRule struct {
	Spec    RuleSpec
	Origins []string
	Scope   string
}

UnifiedRule retains the declaring scope while layers are unified. Child declarations may refine a rule, but identical declarations do not move the outer declaration's relative glob root.

Directories

Path Synopsis
Package tokenizer provides the stable Phase 1 token estimator.
Package tokenizer provides the stable Phase 1 token estimator.

Jump to

Keyboard shortcuts

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