mirror of
https://github.com/larvit/adf-codec.git
synced 2026-11-18 22:25:44 +01:00
Answer architecture round 1: 2 🔴, 6 🟡 (0 misread); carried JSON read through adf/, one spelling home per element in html/, block readers in one table, the first paragraph's form shared, a lint boundary for html/parse, a strictness oracle
This commit is contained in:
+25
-5
@@ -1,17 +1,37 @@
|
||||
import type { AdfNode } from './document.ts'
|
||||
import type { JsonSpelling } from '../canonical-json.ts'
|
||||
import type { JsonValue } from '../json-value.ts'
|
||||
import { canonicalJsonPieces } from '../canonical-json.ts'
|
||||
import { failure, success, type ConvertErrorPath, type Result } from '../result.ts'
|
||||
import { isJsonValue, nestingDepth } from '../json-value.ts'
|
||||
import { faulted, success, type ConvertErrorPath, type ConvertFault, type Read, type Result } from '../result.ts'
|
||||
import { isAdfNode } from './document.ts'
|
||||
import { isJsonValue, nestingDepth, overNested } from '../json-value.ts'
|
||||
import { joinedWithin } from '../length-bound.ts'
|
||||
import { parseJsonText } from '../json-text.ts'
|
||||
|
||||
// `spelled` is the node as the carry holds it, `type` dropped where the format names it apart; `levels` the nesting its position leaves.
|
||||
export function carriedJson(node: AdfNode, spelled: object, spelling: JsonSpelling, path: ConvertErrorPath, levels: number): Result<{ headroom: number; json: string }> {
|
||||
const headroom = levels - nestingDepth(node)
|
||||
if (!isJsonValue(spelled) || headroom < 0) {
|
||||
return failure('unsupported-nesting-depth', `a carried node's JSON nests deeper than the ${levels} levels its position leaves`, path)
|
||||
}
|
||||
if (!isJsonValue(spelled) || headroom < 0) return faulted(nestedPast(levels), path)
|
||||
// Two-space JSON indents each value by its depth, so a short compact value can spell past the bound: join only up to it.
|
||||
const json = joinedWithin(canonicalJsonPieces(spelled, spelling))
|
||||
return json.ok ? success({ headroom, json: json.value }) : json
|
||||
}
|
||||
|
||||
// The read inverse of `carriedJson` takes three steps, value, nesting and node, so each format's own refusals keep their place between them; `invalid` is the format's refusal of text that is no JSON, and `holder` names the carry in a message.
|
||||
export function carriedValue(json: string, holder: string, invalid: ConvertFault): Read<JsonValue> {
|
||||
const parsed = parseJsonText(json)
|
||||
if (parsed.refusal === undefined) return { value: parsed.value }
|
||||
return { fault: parsed.refusal === 'syntax' ? invalid : { code: 'unsupported-node-shape', message: `${holder} holds a number JSON cannot spell` } }
|
||||
}
|
||||
|
||||
export function carriedNesting(held: JsonValue, levels: number): ConvertFault | undefined {
|
||||
return overNested(held, levels) ? nestedPast(levels) : undefined
|
||||
}
|
||||
|
||||
export function carriedNode(held: JsonValue, holder: string): Read<AdfNode> {
|
||||
return isAdfNode(held) ? { value: held } : { fault: { code: 'unsupported-node-shape', message: `${holder} holds one ADF node's JSON: this JSON is no ADF node` } }
|
||||
}
|
||||
|
||||
function nestedPast(levels: number): ConvertFault {
|
||||
return { code: 'unsupported-nesting-depth', message: `a carried node's JSON nests deeper than the ${levels} levels its position leaves` }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user