Skip to content

Rust API

Rust API

The items of the trex crate, grouped by task, each linking the page its examples are on. The generated reference lists every item of the released crate with its full signature.

The dependency

The package on crates.io is trex-re, and the crate it names in code is trex:

[dependencies]
trex-re = "0.2.0"

From a checkout: trex = { package = "trex-re", path = "../trex" }. The published crate leaves out the prior the compress feature compiles in, so that feature builds from a checkout.

Patterns and matches

ItemWhat it is
parse(src) -> Result<Pattern, ParseError>compile a pattern (pattern syntax)
parser::parse_with_shapes(src, &shapes), parse_with_inputs(src, &shapes, &[(name, bytes)]), parse_with_empty_loopcompile against declarations, named second inputs, or an empty-loop reading
PatternBuildernew(src), case_insensitive, orbit(group), swap_greed, nest_limit, empty_loop, build()
Pattern::capture_names(), capture_kinds()the registers a pattern binds, and the one kind each binds where it has one
scan(&pattern, input) -> Vec<Span>every leftmost, non-overlapping match (matches)
scan_with_shapes(&pattern, input, &shapes), scan_with_backend(&pattern, input, Backend)the same under declarations, or on a chosen backend
is_match(&pattern, input)whether the pattern matches
Spanstart, end as u32; start(), end() and range() as usize
captures(&pattern, input, &spans) -> Vec<Match>the registers bound over each span; captures_over, captures_with_shapes, captures_with_empty_loop, and the _with_lists forms that keep every binding under a repetition
Matchstart, end; group(name, input), group_span(name), group_at(i, input), captures() and names() sorted by name, span(), extract::<N>(input), list(name) and lists() for the bindings under a repetition
Regs, INLINE_REGShow a match holds its registers: up to INLINE_REGS inline, the rest shared
find, find_at, find_iter, is_match_atthe first match, the first at or after a byte, or each in turn as asked for
captures_first, captures_at, captures_iter, Cursor, MatchCursor, MatchRefthe same with their registers
shortest_match, shortest_match_athow far the soonest-ending match reaches
split, splitn, split_at_depththe input split on the matches, or on those at one bracket depth
escape(text)a pattern matching exactly text
capture_names(&pattern), captures_len(&pattern)the names a pattern binds, in binding order
CaptureSlots, captures_read, captures_read_at, captures_read_iter, SlotCursormatches written into one reused buffer
static_captures_len, static_token_extentthe registers, or the tokens, every match of a pattern has, where fixed

Typed values and the clock

ItemWhat it is
typed::TypedValuea register’s value through the parse a :value clause compares on; json(kind, ValueStyle, Clock) spells it (typed values)
typed::value_of(kind, text)the value of a span of a kind
typed::ValueStyle, ValueSpelling (Exact, Natural, Tagged), DurationUnit (Nanoseconds, Milliseconds, Seconds)how a value is spelled as JSON
set_now(Option<i64>), now_override(), typed::Clockthe instant now reads (typed value predicates)
set_tz_offset(seconds), set_date_order_day_first(bool), date_order_day_first()the zone of an unzoned timestamp, and which field of an all-numeric date is the day

Pattern sets and streams

ItemWhat it is
PatternSetnew(patterns), named, from_text(text, &mut shapes), from_file; matches, matched, matches_at, is_match, matches_with_spans, scan, scan_matches, first_matches; names, name(i), shapes (pattern sets)
SetMatcheswhich members matched, as a bitset: matched(i), matched_any, matched_all, iter
StreamScannernew(pattern), with_shapes, over_set(set); push(chunk), drain_committed, finish, and the _with_members forms (streams)
HeldStreama stream scanner over an input that starts mid-way: push and finish return the committed matches with their lines
scan_chunked(&pattern, chunks)the whole-input matches of an input fed in chunks

Rewriting and redaction

ItemWhat it is
Template::parse(src, &capture_names)a rewrite template checked against the pattern (templates)
rewrite, rewrite_first, rewrite_nreplace every match, the first, or the first n (the first matches)
rewrite_with, rewrite_n_withreplace through a closure taking a Matched (computed replacements)
rewrite_with_backendrewrite on a chosen backend
Matchedstart(), end(), text(), group(name), names(), value(name) -> Option<TypedValue>, get(reference)
Reference, Fielda template reference parsed against the pattern’s names: read(&match, input), apply(text)
Keep::parse_list(src, &names), Mask::parse(src)the fields a redaction keeps and how it masks (mask and keep)
redactions(input, &matches, &keeps, &mut mask), redactions_with_shapesthe edits a redaction makes
ReportAt, ReportField, ReportRulewhat a report template reads: the place of a match and the rule that made it

Records, windows and files

ItemWhat it is
records::RecordUnitparse(name), records(input) -> Vec<(usize, usize)> (record units)
records::Query, Quantifiera record query: hits(input, &shapes, ..) (record queries)
window::Select (Head, Tail, Range, parse_range), window::window_of(input, select, &unit)part of an input by records (first, last and a range)
follow::Followernew(&[(path, offset)]), wait, poll: what each file gains (following a file)
files::collect(&paths, &WalkOptions), Source, read_source, LineIndexwalk paths as a scan does and read what it finds (files and trees)
encoding::decode(bytes)a BOM-declared or strictly detected UTF-16 input as UTF-8

Declarations and rules

ItemWhat it is
ShapeSetnew, declare, declare_kind, declare_let, declare_test, declare_text, declare_file, declare_lines, run_tests; shapes, lets, tests, rules, rule_of, name_of(id), fields_of; base_dir, set_base_dir (pattern files)
Precedence (Before, After), TokenShape, ShapeErrorwhere a shape is tried, the shape, and a refused declaration (shapes)
LibTest, TestFailurea test line and an expectation it did not meet (tests)
Rule, Severitya declared rule: its pattern, message, fix, files, unless patterns, record unit and metadata (rules)
rule_scan::RuleScan::new(shapes)every rule of a set scanned as one
librarythe shipped library of named patterns (the shipped library)

Building patterns

ItemWhat it is
templates::Miningmine(input), mine_tokens, mine_records(&tokens, input, &records): each record shape once with its count (templates)
infer::infer(&examples), infer_against(&examples, &counters)the most specific pattern every example matches (a pattern from examples)
infer::marks::parse_lines(src)lines with fields marked {name:text}
infer::build::{Spec, Mint, build, Built}the pattern extracting marked fields (a pattern that extracts fields)
infer::build::fields_for(source, &pattern, &shapes), read_recordsthe fields a saved build reads, and the records they read (saving a build)

Explanations

ItemWhat it is
explain::Explainer::new(&pattern, input, &shapes)explain(&match, &route) -> Explanation, tokens(range) (explanations)
explain::Explanationtokens, guards, readings, route; field(axis, piece, at)
trace::Recording::start(), trace::take_recorded(), trace::clear(), explain::route_of(&rungs)keep the rungs a scan takes, and name the route they make

Tools

ItemWhat it is
index::Indexbuild(root, &paths), save(root), load(root), refuses(&pattern, path); Summary::of(input), Needs::of(&pattern) (indexes)
Grammar, Node, GrammarErrorGrammar::parse(src), with_start, parse_input, count_parses, best_parse_prob, total_prob, segment, count_segmentations (grammars)
bpe::Bpetrain(corpus, merges), encode(text), to_lines(), parse(lines) (byte-pair encoders)
prefilter::{BloomFilter, CuckooFilter, XorFilter}, Membership, verify(corpus)build(corpus), might_contain(literal) (prefilters)
seam coder itemswith the compress feature (compression)

Lexing

ItemWhat it is
lexer::lex(input) -> Vec<Token>the token stream a scan reads (tokens)
token::Token, TokenKinda token’s kind and span
supertokens(input) -> Vec<SuperToken>, Rolerole-tagged units: a call, an assignment, a key and value, a list

Axes

Each axis module reads a field keyed by byte offset (property axes).

ModuleEntryField
spectralanalyze, analyze_with, analyze_needing, regions, code_regions, high_entropy_runsSpectralField
seamanalyze, analyze_with, analyze_with_model, english_model, analyze_tokens, analyze_supertokensSeamField
shapeanalyze_bytes, analyze_over, classified_regionsShapeField
orbitcanonical, same_orbit, tokenize, collapse, matchesString, Vec<OrbitToken>
magnitudeanalyze, analyze_bytes, analyze_withMagnitudeField
stressanalyze_bytesStressField
flowanalyze, analyze_bytes(input, Signal), analytic_signalFlowField, AnalyticField
observationanalyze, analyze_withObservationField
echoanalyze, analyze_bytes, analyze_with, analyze_superEchoField, Vec<SuperEcho>
relationanalyze, analyze_bytesRelationField
holography, curvature, topology, geodesic, entanglementanalyze, analyze_bytes, analyze_overthe graph readings
gaugecanonicalize_bytes, alpha_equivalentString, bool
contextanalyze, analyze_with, relate, relate_bytes, record_period, fold_windows, agreementContextField, RelationContext
profilefold_tokens, fold_profilesProfile

Backends

ItemWhat it is
Backend, BackendUsedwhich engine a scan runs on, and which answered (engines)
scan_gpu, gpu_eligible, device_availablethe device scan and when it applies
scan_dual_grain(&pattern, input) -> (Vec<Span>, GrainTiming)the byte and token grains as a pipeline
notice::set_sinkwhere the scheduler’s notes go
version()the crate version