2.8 KiB
TASK templater-segments: split a template into text and <% %> commands in the new crate notescraft-templater
Runs through OpenCode (specs/agents/opencode-runbook.md is attached automatically). Task 1 of specs/plugins-parity/templater/tasks.md.
Precondition (lead step 0, done when this runs): crates/templater exists with a Cargo.toml, registered at L3. If crates/templater/Cargo.toml is missing, stop and say so.
OWNS
crates/templater/src/lib.rs(crate//!doc,mod segment; mod error;,pub use),crates/templater/src/error.rs(onethiserrorError,pub type Result<T>),crates/templater/src/segment.rscrates/templater/tests/segment.rs
FILES TO OPEN
specs/plugins-parity/templater/spec.mdD2 (syntax and limits)crates/templates/src/expand.rs(how the existing{{ }}scanner usesfindandget()without indexing; copy that style)
GOAL / API
Write crates/templater/src/segment.rs as your first tool call.
pub enum Kind { Output, Js }
pub enum Segment {
Text(String),
Command { src: String, line: u32, kind: Kind }, // src = text between the tags, trimmed of the marker chars
}
pub struct Template { pub segments: Vec<Segment> }
pub fn parse(src: &str) -> Result<Template>;
<% x %>Output.<%* x %>Js (kept, never run). Tag markers:<%-strips ALL whitespace including newlines before the tag;<%_strips spaces and tabs before;-%>strips the following newline plus spaces;_%>strips following spaces and tabs.- Whitespace control edits the neighbouring
Textsegments (and may leave one empty: drop emptyText). lineis the 1-based line of the<%.- Errors: unterminated
<%->Error::Unterminated { line }; more than 2 000 commands ->Error::TooManyCommands; input over 1 MiB ->Error::TooLarge. - Text without any tag is exactly one
Textequal to the input (or no segment for an empty input).
TESTS
crates/templater/tests/segment.rs: plain text; one command; three commands with lines 1, 3, 5; each of the four whitespace markers on a before/after example (write the expected strings out by hand); <%* is Js; <% with no %> gives line number; %> without an opening is plain text; CRLF input keeps \r\n outside stripped areas and -%> strips \r\n; 2 001 commands refused; 1 MiB of repeated <% returns an error in under 200 ms (use std::time::Instant, assert < 200 ms generously).
VERIFY
export PATH="$HOME/.cargo/bin:$PATH"
cargo test -p notescraft-templater
cargo clippy -p notescraft-templater --all-targets -- -D warnings
rustfmt --edition 2024 crates/templater/src/*.rs crates/templater/tests/*.rs
DO NOT
Edit Cargo files or the templates crate; parse the expression inside a command (task 2); evaluate anything; use [i] or [a..b] on the input; add a dependency.