Skip to main content

Crate tsv_css

Crate tsv_css 

Source
Expand description

CSS formatting, and parsing to the wire JSON of Svelte’s parseCss — part of tsv.

format() formats one stylesheet in one call; a Formatter formats many on one thread, keeping its arenas warm between calls. The formatter is non-configurable: Prettier’s style with a print width of 100, tabs and single quotes. Formatting output is not frozen — it changes between releases as formatting bugs are fixed.

§Usage

use tsv_css::{Formatter, format};

assert_eq!(format("a{color:red}")?, "a {\n\tcolor: red;\n}\n");
let mut formatter = Formatter::new();
assert_eq!(formatter.format("b{}")?, "b {\n}\n");

// Parse to the wire JSON AST (the `json` feature, on by default)
let json = tsv_css::parse_to_json("a{}")?;
assert!(json.starts_with(r#"{"type":"StyleSheetFile""#));

The wire JSON is the AST as JSON in the canonical parser’s shape — Svelte’s parseCss’s here. Its start / end are UTF-16 offsets, and the default wire is span-only: no loc (the locations feature adds it). Error::span, unlike the wire, is a UTF-8 byte range.

§Errors

A source that does not parse is an Error:

use tsv_css::{ErrorKind, format};

let err = format("a{").unwrap_err();
assert_eq!(err.kind(), ErrorKind::Syntax);
assert_eq!(err.span(), Some(2..2));

Every tsv language crate returns the same Error: its Error::kind, its Error::span in the input’s UTF-8 bytes, and Error::to_report — how tsv’s bindings (its npm packages) report it — in the wire’s UTF-16 offsets and lines; one line under {}, the tsv CLI’s frame under {:#}.

§Deep input

The parser refuses a tree past its depth limits with ErrorKind::TooDeep, so every walk of an accepted tree fits a 2 MiB thread in an optimized build and a 16 MiB one in a debug build. A stack overflow aborts the process rather than panicking, so a debug build handling input it does not control — cargo test runs each test on a 2 MiB thread — wraps the call in run_with_stack, which runs it on a thread reserving STACK_SIZE.

§Features

  • json (default) — parse_to_json, parse_to_json_with and JsonOptions. Turn default features off for a format-only build.
  • locations — JsonOptions::locations, adding a loc (line and column) to every positioned object of the wire.

The _internal_* features are tsv’s own diagnostics, not for consumers (Stability).

§Stability

This release exposes no AST: parsing returns the wire JSON, in parseCss’s shape, which @fuzdev/tsv-parse-wasm’s tsv_ast.d.ts types.

Items hidden from these docs (__private, ast and every other #[doc(hidden)] item) and the _internal_* features are tsv’s own plumbing for its sibling crates, which pin each other exactly; they change in any release, outside semver.

Structs§

Error
Why a parse or format produced no output — a syntax error, a depth limit, a refusal of a source that parses, or a source over the size cap (ErrorKind): what went wrong (Error::kind, Error::message), where (Error::span, UTF-8 byte offsets), and the pointed report tsv’s bindings (its npm packages) render (Error::to_report). One pointer wide, so a Result carrying it costs a successful call almost nothing.
ErrorReport
A pointed error as tsv’s bindings (its npm packages) report it: the message and the frame apart, and the span in the document’s wire coordinates (Error::to_report). The error’s full text (its {:#} form) is exactly message, a \n, then frame.
Formatter
Formats many CSS sources on one thread, reusing its allocations from one source to the next.
JsonOptions
What parse_to_json_with’s wire carries. The default is parse_to_json’s: the span-only wire. Its one option, locations, requires the locations feature.
WirePoint
One position in a language’s wire coordinates, the positions its wire JSON (the parse output: the AST as JSON in the canonical parser’s shape) carries. It holds the offset a wire node starting at that byte would carry as its start, and the loc point tsv parse --locations (and the npm packages’ create_locator(…).position_at(start)) gives it. The coordinates are the language’s canonical parser’s: a leading byte-order mark counted for TypeScript (acorn) and elided for Svelte and CSS, and lines ended by ECMAScript’s terminators for TypeScript and by \n alone for Svelte and CSS.

Enums§

ErrorKind
What kind of failure an Error is (Error::kind).

Constants§

STACK_SIZE
Stack reserved by run_with_stack: 32 MiB, twice the debug build’s contract.

Functions§

format
Parse and format source in one call.
parse_to_json
Parse source and return its wire JSON AST: compact JSON in Svelte’s parseCss() shape (a standalone StyleSheetFile), every node’s start / end a UTF-16 offset and no loc.
parse_to_json_with
parse_to_json under options: whether the wire carries loc.
run_with_stack
Run f on a thread reserving STACK_SIZE and return its value.

Type Aliases§

Result
std::result::Result with Error as the default error type, so a glob import leaves the two-parameter form usable.