kinds

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: MIT Imports: 16 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package kinds resolves what kind an icon-pack item is (icon or illustration) from the pack's config.json, and suggests kinds for items a human has not classified. It is the Go side of the "kinds and flavours" contract; core's Support\KindResolver and Support\KindGlob are the PHP side, and the glob vector table and heuristic constants are identical in both.

Index

Constants

View Source
const (
	// HeuristicAspectMin and HeuristicAspectMax bound the viewBox width/height
	// ratio an icon is expected to sit inside.
	HeuristicAspectMin = 0.8
	HeuristicAspectMax = 1.25
	// HeuristicPaletteMin is the number of distinct literal colours that fires.
	HeuristicPaletteMin = 4
	// HeuristicBytesMax and HeuristicElementsMax fire when exceeded.
	HeuristicBytesMax    = 20_000
	HeuristicElementsMax = 150
	// HeuristicCanvasMax fires when the larger viewBox side exceeds it.
	HeuristicCanvasMax = 256
	// HeuristicMinSignals is how many signals must fire to suggest.
	HeuristicMinSignals = 3
	// HeuristicGroupMin is how many items a folder needs before its suggestions
	// collapse into one rule (at least 80% of them suggested).
	HeuristicGroupMin = 5
)

The row 7 heuristic (contract section 3). It looks at an SVG's shape and suggests that a human reclassify it. It never decides anything: no code path here writes a kind anywhere.

These constants are identical in core (IchavaConstants::KIND_HEURISTIC_*).

View Source
const (
	SignalAspect   = "aspect"
	SignalPalette  = "palette"
	SignalBytes    = "bytes"
	SignalElements = "elements"
	SignalCanvas   = "canvas"
)

Signal ids.

View Source
const (
	IssueInvalidValue = "kind.invalid_value"
	IssueInvalidGlob  = "kind.invalid_glob"
)

Issue codes.

Variables

This section is empty.

Functions

func CategoryOf

func CategoryOf(itemPath string) string

CategoryOf is the category an item files under: its first folder, slugged, or "" for a file at the pack's root. This is IconClassifier::categoryOf for an item path. The slug is Str::slug's for ASCII (lower case, runs of anything else become one "-"); non-ASCII letters are not transliterated.

func Qualifies

func Qualifies(signals []Signal) bool

Qualifies applies the suggestion rule: enough signals, and at least one of them is bytes or elements. That requirement keeps flags out (4:3, multicolour, small).

func WriteRecords

func WriteRecords(file string, recs []Record) error

WriteRecords writes a pack's suggestions as a JSON array, replacing the file atomically. An empty list is written as [] so a reader can tell "ran, found nothing" from "never ran".

Types

type Config

type Config struct {

	// Issues lists every entry that was dropped, in document order where the
	// document has one.
	Issues []Issue
	// contains filtered or unexported fields
}

Config is the kind-relevant part of a pack's resources/assets/svg/config.json.

func ParseConfig

func ParseConfig(data []byte) (*Config, error)

ParseConfig reads a pack's config.json.

It mirrors core's PackConfigData: the documented shape is metadata.data.<section>, the top-level key is a fallback, and the documented section wins whenever it holds anything. An entry that names no valid kind or glob is dropped and recorded in Issues rather than failing the pack.

func (*Config) Resolve

func (c *Config) Resolve(itemPath string) Resolution

Resolve decides one item's kind. The first row that yields a kind wins: item, then the first matching rule, then category, then the pack, then icon. It reads nothing but the config, so it is the same answer at any time.

func (*Config) ThemeSuffixes

func (c *Config) ThemeSuffixes(itemPath string) []string

ThemeSuffixes returns the filename suffixes (before ".svg") of the theme twins the item's category declares, in theme-name order. A file whose stem ends in one of them, beside a light sibling, is a twin and not an item.

type Current

type Current struct {
	Kind   Kind   `json:"kind"`
	Source Source `json:"source"`
}

Current is the kind and source an item resolved to before any suggestion.

type Glob

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

Glob is a compiled item-path pattern (contract section 1.2). In a pattern, "*" is zero or more characters other than "/", "?" is exactly one character other than "/", and "**" is zero or more whole segments (it must be a whole segment).

The match is anchored (the whole path) and case-sensitive. Everything else is literal. It is matched segment by segment rather than compiled to a regexp, which keeps "never /" true by construction and makes the cost bounded.

func ParseGlob

func ParseGlob(pattern string) (*Glob, error)

ParseGlob validates and compiles a pattern. The error says why it is invalid.

func (*Glob) Match

func (g *Glob) Match(path string) bool

Match reports whether an item path matches the whole pattern.

func (*Glob) String

func (g *Glob) String() string

String is the pattern as written.

type Issue

type Issue struct {
	Code   string `json:"code"`   // kind.invalid_value or kind.invalid_glob
	Where  string `json:"where"`  // the key path
	Detail string `json:"detail"` // what is wrong
}

Issue is something wrong in a pack's kind keys. Codes match core's doctor check ids, so one vocabulary covers both tools.

type Kind

type Kind string

Kind is what an item is. The values are the ones core's IconKind enum and js-ui-kernel's IconKind union use.

const (
	Icon         Kind = "icon"
	Illustration Kind = "illustration"
)

The two kinds. There is no "mixed": a pack is mixed when its items resolve to more than one kind, and that is derived, never declared.

func ParseKind

func ParseKind(v any) (Kind, bool)

ParseKind reads a config value as a kind, forgiving case and surrounding whitespace and nothing else, as IconKind::fromConfig does. A value that names no kind returns false so resolution falls through to the next row.

type Metrics

type Metrics struct {
	Bytes    int64
	Elements int
	// HasViewBox is true only for a viewBox with four numbers and a positive
	// width and height. Width and height attributes are not consulted.
	HasViewBox    bool
	Width, Height float64
	// Colours are the distinct literal paint colours, normalised: hex as
	// lowercase #rrggbb (alpha dropped), rgb()/rgba() converted to the same, a
	// named colour as its lowercase name. none, transparent, currentColor,
	// inherit and anything that is not a literal (url(), var(), hsl()) are not
	// colours here.
	Colours []string
}

Metrics are the measured facts about one SVG.

func Measure

func Measure(raw []byte, size int64) (Metrics, error)

Measure reads an SVG without building a tree and without fetching anything: Go's decoder does not resolve entities or load DTDs. size is the file size, which may exceed len(raw) when the caller reads only a prefix.

func (Metrics) Signals

func (m Metrics) Signals() []Signal

Signals lists the signals that fire for these metrics, in the contract's order. Aspect and canvas need a viewBox; the other three do not.

type Mode

type Mode string

Mode says which resolved sources the heuristic may second-guess.

const (
	// ModeDefault looks only at items that resolved icon by falling all the way
	// through to the default.
	ModeDefault Mode = "default"
	// ModeAll also looks at items whose kind came from an explicit package.kind.
	ModeAll Mode = "all"
)

func ParseMode

func ParseMode(s string) (Mode, error)

ParseMode reads the --suggest-kinds value. The empty string is the default.

type Record

type Record struct {
	Package   string         `json:"package"`
	Path      string         `json:"path"`
	Current   Current        `json:"current"`
	Suggested Kind           `json:"suggested"`
	Signals   []Signal       `json:"signals"`
	Accept    map[string]any `json:"accept"`
}

Record is one suggestion, the same JSON shape core's doctor emits. A human accepts it by merging Accept into the pack's config.json; nothing here does.

type Resolution

type Resolution struct {
	Kind   Kind
	Source Source
	// Rule is the matching rule's pattern (SourceRule) or the category slug
	// (SourceCategory), and empty otherwise.
	Rule string
}

Resolution is the outcome for one item.

type ScanResult

type ScanResult struct {
	Records []Record
	// Items is the number of items looked at: every .svg under files/ that is
	// not a theme twin, whether or not it could be measured.
	Items int
	// Skipped lists items that could not be measured, with the reason.
	Skipped []string
	// Issues are problems in the pack's own kind keys.
	Issues []Issue
}

ScanResult is everything a scan found.

func Scan

func Scan(pkg, svgDir string, mode Mode) (*ScanResult, error)

Scan measures every item in a pack and returns the suggestions.

svgDir is the pack's resources/assets/svg directory: config.json beside a files/ tree. It only reads. A missing config.json is a pack with no kind keys, and every item then reads icon/default.

type Signal

type Signal struct {
	ID    string  `json:"id"`
	Value float64 `json:"value"`
}

Signal is one fired signal with the value that fired it.

type Source

type Source string

Source says which D6 row decided a kind. A pack can set rows 2 to 6; row 1 (the host's kinds.overrides) belongs to the Laravel app and never applies to a sync run, so it has no value here.

const (
	SourceItem     Source = "item"
	SourceRule     Source = "rule"
	SourceCategory Source = "category"
	SourcePack     Source = "pack"
	SourceDefault  Source = "default"
)

Jump to

Keyboard shortcuts

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