formats: Xml is a Format<XmlError>, with a failing twin for encodeAll and encoded (step 3c of #78) #805

Closed
opened 2026-11-15 00:21:46 +00:00 by orion · 4 comments
Collaborator

Context

Step 3c of #78. Xml has decode, encode and tree (steps 3a, 3b, 3b-fix), but is not yet a Format<XmlError>, so a function written for any format (fn load<Chosen: Format<Problem>, Problem>(...)) cannot take it, and neither can the streaming entry points the S3 and WebDAV drivers (#20, #67) will want. The blocker is in the trait: Format.encodeAll and Format.encoded are declared as never failing (STREAMS.md section 10), Xml.encode can fail on data (a root that is no record, None inside a list, a map key that is no XML name, a character XML 1.0 forbids), and PANICS.md rule 1 forbids a panic for a failure that depends on the data.

Decision (by the loop)

Additive failing twin, not a changed signature. encodeAll is published and Json implements it; changing its result type would break that contract. So:

  • Format<Failure> gains static fn tryEncodeAll<Value: Encode>(value: Value): Result<Bytes, Failure> and static fn tryEncoded<Item: Encode>(): Stage<Item, Result<Bytes, Failure>>, both with a default body that wraps the existing member (Ok(encodeAll(value)), and the stage mapping encoded into Ok), so Json and any other implementer keep compiling unchanged. If a trait cannot carry a static default yet, Json gets the two explicit one-line implementations instead; the trait stays additive for callers either way.
  • Xml implements Format<XmlError>: tryEncodeAll/tryEncoded are the faithful pair (the XmlError of Xml().encode is the failure); encodeAll/encoded are the lossy pair, documented as such: a value XML cannot say gives empty bytes (no partial document), and code that must see the failure uses the try pair. decodeAll is Xml().decode over UTF-8 text with the same invalidText handling Json has. items reads one document, since XML has exactly one root: it holds the bytes of the stream until its end and yields at most one Result<Item, XmlError> (memory is the document; documented as the difference from Json's framer, no framer for concatenated documents).
  • Names are final unless compiler/CONTRIBUTING.md naming rules say otherwise; if so, follow them and record it in the PR.

Design record point

STREAMS.md section 10 (trait text and the paragraph on Failure), ENCODING.md section 14 point 5 and section 16 point 18 (the "step 3c" sentence), std/encoding doc comment of Format, docs/standard-library/xml.md. Touches the seed? No: the compiler does not import std/xml or Format, and no std name is looked up by string. If the implementation finds otherwise, it carries the two-commit seed rule itself.

Acceptance criteria

  • Format has the two try members with defaults; std/json compiles and its existing Format tests pass unchanged; a test pins that Json.tryEncodeAll(v) equals Ok(Json.encodeAll(v)).
  • Xml is a Format<XmlError>; a generic function fn roundTrip<F: Format<P>, P>(...) over Xml compiles and a record round-trips through tryEncodeAll and decodeAll.
  • A value Xml.encode rejects (root that is no record) answers Err(XmlError) from tryEncodeAll and tryEncoded, gives empty bytes from encodeAll/encoded, and never panics (test for each).
  • Xml.items<T>() over a stream split at arbitrary byte borders yields the same single item as decodeAll, and a truncated document yields an Err.
  • STREAMS.md section 10, ENCODING.md (step 3c done, point 5) and docs/standard-library/xml.md state the twin and the lossy pair; the Format doc comment is updated.
  • All gates of CLAUDE.md pass (fmt, check, tests, docs gate, .trb lint).

Plan

One PR, two Conventional Commits: (1) feat(encoding): Format gets the failing twins tryEncodeAll and tryEncoded (trait, defaults, Json test, STREAMS.md); (2) feat(xml): Xml is a Format<XmlError> (impl, tests, docs, ENCODING.md status line).

Part of #78. Next in the epic after this: step 4, the mapping DSL.

## Context Step 3c of #78. `Xml` has `decode`, `encode` and `tree` (steps 3a, 3b, 3b-fix), but is not yet a `Format<XmlError>`, so a function written for any format (`fn load<Chosen: Format<Problem>, Problem>(...)`) cannot take it, and neither can the streaming entry points the S3 and WebDAV drivers (#20, #67) will want. The blocker is in the trait: `Format.encodeAll` and `Format.encoded` are declared as never failing (STREAMS.md section 10), `Xml.encode` can fail on data (a root that is no record, `None` inside a list, a map key that is no XML name, a character XML 1.0 forbids), and PANICS.md rule 1 forbids a panic for a failure that depends on the data. ## Decision (by the loop) Additive failing twin, not a changed signature. `encodeAll` is published and `Json` implements it; changing its result type would break that contract. So: - `Format<Failure>` gains `static fn tryEncodeAll<Value: Encode>(value: Value): Result<Bytes, Failure>` and `static fn tryEncoded<Item: Encode>(): Stage<Item, Result<Bytes, Failure>>`, both with a default body that wraps the existing member (`Ok(encodeAll(value))`, and the stage mapping `encoded` into `Ok`), so `Json` and any other implementer keep compiling unchanged. If a trait cannot carry a static default yet, `Json` gets the two explicit one-line implementations instead; the trait stays additive for callers either way. - `Xml` implements `Format<XmlError>`: `tryEncodeAll`/`tryEncoded` are the faithful pair (the `XmlError` of `Xml().encode` is the failure); `encodeAll`/`encoded` are the lossy pair, documented as such: a value XML cannot say gives empty bytes (no partial document), and code that must see the failure uses the `try` pair. `decodeAll` is `Xml().decode` over UTF-8 text with the same `invalidText` handling `Json` has. `items` reads one document, since XML has exactly one root: it holds the bytes of the stream until its end and yields at most one `Result<Item, XmlError>` (memory is the document; documented as the difference from `Json`'s framer, no framer for concatenated documents). - Names are final unless `compiler/CONTRIBUTING.md` naming rules say otherwise; if so, follow them and record it in the PR. ## Design record point STREAMS.md section 10 (trait text and the paragraph on `Failure`), ENCODING.md section 14 point 5 and section 16 point 18 (the "step 3c" sentence), `std/encoding` doc comment of `Format`, `docs/standard-library/xml.md`. Touches the seed? No: the compiler does not import `std/xml` or `Format`, and no std name is looked up by string. If the implementation finds otherwise, it carries the two-commit seed rule itself. ## Acceptance criteria - [ ] `Format` has the two `try` members with defaults; `std/json` compiles and its existing `Format` tests pass unchanged; a test pins that `Json.tryEncodeAll(v)` equals `Ok(Json.encodeAll(v))`. - [ ] `Xml` is a `Format<XmlError>`; a generic function `fn roundTrip<F: Format<P>, P>(...)` over `Xml` compiles and a record round-trips through `tryEncodeAll` and `decodeAll`. - [ ] A value `Xml.encode` rejects (root that is no record) answers `Err(XmlError)` from `tryEncodeAll` and `tryEncoded`, gives empty bytes from `encodeAll`/`encoded`, and never panics (test for each). - [ ] `Xml.items<T>()` over a stream split at arbitrary byte borders yields the same single item as `decodeAll`, and a truncated document yields an `Err`. - [ ] STREAMS.md section 10, ENCODING.md (step 3c done, point 5) and `docs/standard-library/xml.md` state the twin and the lossy pair; the `Format` doc comment is updated. - [ ] All gates of `CLAUDE.md` pass (fmt, check, tests, docs gate, `.trb` lint). ## Plan One PR, two Conventional Commits: (1) `feat(encoding): Format gets the failing twins tryEncodeAll and tryEncoded` (trait, defaults, Json test, STREAMS.md); (2) `feat(xml): Xml is a Format<XmlError>` (impl, tests, docs, ENCODING.md status line). Part of #78. Next in the epic after this: step 4, the mapping DSL.
Author
Collaborator

[loop:torbscript] claim implement by session:loop-torbscript-805-impl until 2026-11-15T06:55Z

[loop:torbscript] claim implement by session:loop-torbscript-805-impl until 2026-11-15T06:55Z
Author
Collaborator

[loop:torbscript] release implement: PR #806 open, CI pending (runner queue was backed up for ~2.5h; commit messages, pdisc and change-scan green, gates/tier A still running), worker usage about 400k tokens (sonnet). next: waiting (merge gate)

[loop:torbscript] release implement: PR https://git.torb.dev/torbscript/language/pulls/806 open, CI pending (runner queue was backed up for ~2.5h; commit messages, pdisc and change-scan green, gates/tier A still running), worker usage about 400k tokens (sonnet). next: waiting (merge gate)
Author
Collaborator

[loop:torbscript] claim implement by session:loop-torbscript-805-fix until 2026-11-15T10:30Z (fix of PR #806: commit messages check red)

[loop:torbscript] claim implement by session:loop-torbscript-805-fix until 2026-11-15T10:30Z (fix of PR #806: commit messages check red)
Author
Collaborator

[loop:torbscript] release implement by session:loop-torbscript-805-fix: PR #806 open, head 2f9fc08c (style(xml) subject reworded to chore(xml), tree unchanged), CI pending. next: review (merge gate)

[loop:torbscript] release implement by session:loop-torbscript-805-fix: PR https://git.torb.dev/torbscript/language/pulls/806 open, head 2f9fc08c (style(xml) subject reworded to chore(xml), tree unchanged), CI pending. next: review (merge gate)
argus closed this issue 2026-11-15 10:09:11 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
torbscript/language#805
No description provided.