From a87ec97e92dc3cc9616a2443a1237a4764f3c02a Mon Sep 17 00:00:00 2001 From: Roman Zaiev Date: Thu, 3 Dec 2026 18:07:52 +0000 Subject: [PATCH] Release 0.7.2: check the rules by litany and send agents to the docs make lint checked formatting, odoc and the release build, so the rules a compiler does not hold were held by reading. litany now runs there on Hypha's lint set, with the gold copy held whole in the litany file, and the code meets it. What the deriver writes for a type that holds itself names the type of the description it forces, so a project that runs litany's untyped-ignore has no finding there either. The deriver's open of Ppxlib is allowed with its reason, and the generators under src/gen/, build-time programs, are the one per-path. An agent using wiretype reaches its signatures in the switch's lib/, by go-to-definition, a hover or a read, and never opens its doc/, where the catalogue and the pages are installed. So every interface names its page, where a page is about it, or its package's catalogue, by its path through opam var, and the README's With an agent says the same. --- .github/workflows/ci.yml | 4 + AGENTS.md | 10 +- CHANGES.md | 9 ++ Makefile | 7 +- README.md | 12 +- dune | 11 +- dune-project | 7 +- litany | 230 ++++++++++++++++++++++++++++++++++++++ ppx/ppx_wiretype.ml | 10 +- ppx/ppx_wiretype.mli | 6 + src/bounds.mli | 5 +- src/decode.ml | 9 +- src/decode.mli | 5 +- src/encode.mli | 5 +- src/grammar.ml | 4 +- src/grammar.mli | 4 +- src/itself.mli | 4 +- src/problem.mli | 4 +- src/schema.ml | 34 ++++-- src/schema.mli | 5 + src/shape.mli | 3 + src/text.ml | 2 +- src/text.mli | 4 +- src/unwritable.mli | 4 +- src/value.mli | 2 + src/wiretype.mli | 2 + test/test_ppx_wiretype.ml | 4 +- test/test_schema.ml | 3 +- test/test_wiretype.ml | 11 +- wiretype.opam | 1 + 30 files changed, 375 insertions(+), 46 deletions(-) create mode 100644 litany diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8ffbddc..6c4914c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,6 +18,10 @@ jobs: - uses: ocaml/setup-ocaml@v3 with: ocaml-compiler: ${{ matrix.ocaml }} + # litany, which make lint runs, is open-opam's. + opam-repositories: | + open-opam: https://github.com/open-opam/opam-repository.git + default: https://opam.ocaml.org - uses: oven-sh/setup-bun@v2 - run: opam install . --deps-only --with-test --with-dev-setup -y - run: bun install --frozen-lockfile diff --git a/AGENTS.md b/AGENTS.md index daa6337..6d96a7d 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -13,7 +13,7 @@ documentation site, [docs/](docs/src/content/docs/index.mdx). make setup # once: the switch wiretype, all dependencies, and Zod make test # every suite, the kinds' rows through Zod, and the shared rows # through the printed Melange module -make lint # formatting, odoc, and the release build +make lint # formatting, odoc, the release build, and Hypha's rules by litany make fmt # format in place make docs # the documentation site into _build/docs, as Pages publishes it, and # agents/ from its pages; docs-serve serves it @@ -185,8 +185,12 @@ break. the pages on every pull request and fails if `agents/` then differs. - **The opens are `Tuple`'s, inside `Tuple.( ... )` as its `.mli` says, and the deriver's `Ppxlib` and `Ast_pattern`**, which every ppxlib rewriter - opens to read the AST it matches. Symptom: - `rg -n '\bopen |\b[A-Z]\w*\.[(\[{]' src ppx` finds any other. + opens to read the AST it matches. Symptom: `make lint`'s litany reports + `open-of-ordinary-module`, which each `open Ppxlib` is allowed with its + reason beside it. +- **The `litany` file's one addition is a `per-path` for `src/gen/`**: + build-time programs that print the tables dune writes into modules, which + the gold copy's paths take for the library's code. ## Changes and releases diff --git a/CHANGES.md b/CHANGES.md index 1895467..0e44f9a 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,14 @@ # Changes +## 0.7.2 (2026-12-03) + +- What `[@@deriving wiretype]` writes for a type that holds itself names the + type of the description it forces, so a project that runs litany's + `untyped-ignore` no longer has a finding at each such type. +- Every package installs the documentation for agents, `llms.txt` and every + page, in `$(opam var :doc)`, and every interface it installs + names its page there, or `llms.txt`. The README's *With an agent* says so. + ## 0.7.1 (2026-11-22) - The wiretype package installs its documentation for agents: each page as diff --git a/Makefile b/Makefile index 610200a..350b0f1 100644 --- a/Makefile +++ b/Makefile @@ -15,6 +15,7 @@ help: ## list targets setup: ## create the switch and install dependencies @command -v bun >/dev/null 2>&1 || echo "The Zod rows and the documentation site need Bun 1.3 or later." @opam switch list --short | grep -qx $(SWITCH) || opam switch create $(SWITCH) --empty -y + opam repo add open-opam https://github.com/open-opam/opam-repository.git --switch=$(SWITCH) --rank=1 opam install . --switch=$(SWITCH) --deps-only --with-test --with-dev-setup -y cd test/zod && bun install --frozen-lockfile @@ -28,10 +29,12 @@ test: test-zod ## run the tests test-zod: ## run the kinds' rows through Zod cd test/zod && bun test -# Release is the profile opam installs with. -lint: ## check formatting, docs and the release build +# Release is the profile opam installs with. litany holds the code to +# Hypha's rules, as the litany file selects them. +lint: ## check formatting, docs, the release build and Hypha's rules $(OPAM) dune build @all @fmt @doc $(OPAM) dune build --profile release @all + $(OPAM) litany check fmt: ## format the code $(OPAM) dune fmt diff --git a/README.md b/README.md index 0ba4005..dbe1794 100644 --- a/README.md +++ b/README.md @@ -63,14 +63,14 @@ and prints the same description as JSON Schema and Zod. ## With an agent -Each release installs its documentation for an agent with it, at -`$(opam var wiretype:doc)`: `llms.txt`, and every page in Markdown. An agent -reads it when your project's `AGENTS.md` tells it to: +Every package of wiretype installs its documentation for an agent with it, +at `$(opam var :doc)`: `llms.txt`, and every page in Markdown. An +agent reads it when your project's `AGENTS.md` tells it to: ```md -Before writing code with wiretype, read `$(opam var wiretype:doc)/llms.txt` and -the pages it lists for the parts you need, and use those parts rather than -writing your own. +Before working with wiretype, read `llms.txt` in `$(opam var :doc)`, +for any wiretype package the project installs, then the pages it lists for the +parts you need, and use those parts rather than writing your own. ``` ## Contributing diff --git a/dune b/dune index 52af2f9..09fdcdc 100644 --- a/dune +++ b/dune @@ -8,8 +8,15 @@ (:standard -w +a-4-40-41-42-44-45-48-67-70)))) ; The documentation for a user's agent, agents/, written from the site's pages -; by `make docs`: wiretype installs it whole, so $(opam var wiretype:doc) -; holds llms.txt and each page. +; by `make docs`: every package installs it whole, so a package installed +; alone, ppx_wiretype without wiretype, still holds llms.txt and each page in +; $(opam var :doc). + +(install + (package ppx_wiretype) + (section doc) + (source_trees + (agents as .))) (install (package wiretype) diff --git a/dune-project b/dune-project index bac20f7..9ef92d5 100644 --- a/dune-project +++ b/dune-project @@ -55,7 +55,12 @@ (ocamlformat (and :with-dev-setup - (= 0.29.0))))) + (= 0.29.0))) + (hypha-litany + (and + :with-dev-setup + (>= 1.3.0) + (< 2.0.0))))) (package (name ppx_wiretype) diff --git a/litany b/litany new file mode 100644 index 0000000..0af57fc --- /dev/null +++ b/litany @@ -0,0 +1,230 @@ +; Hypha's lint set, as litany holds it. Every Hypha repository's litany file +; selects each rule here and keeps each forbid as it is written here; what is +; the repository's own -- a forbid more, a per-path -- is added with its +; reason. Hypha's OCAML.md says what each rule is for. + +(litany-config 1) + +(lint + (select default) + (extend + unsafe-partial-stdlib + unsafe-obj-magic + untyped-ignore + ignored-result + wildcard-over-owned-variant + open-of-ordinary-module + restricted-dependency)) + +(rule restricted-dependency + ; Refused in every file. + (forbid Stdlib.failwith + (use "a result; a test that raises on purpose declares its own exception")) + (forbid Stdlib.compare + (use "the type's own comparison: Int.compare, String.compare")) + (forbid Stdlib.min + (use "the type's own: Int.min, Float.min")) + (forbid Stdlib.max + (use "the type's own: Int.max, Float.max")) + (forbid Unix.time + (use "the clock the caller gives, read as a Ptime.t")) + (forbid Unix.gettimeofday + (use "the clock the caller gives, read as a Ptime.t")) + (forbid Stdlib.Sys.time + (use "Eio.Time.Mono.now, for time that has passed")) + (forbid Unix.sleep + (use "Eio.Time.Mono.sleep_span")) + (forbid Unix.alarm + (use "Eio.Time.Timeout.v")) + (forbid Unix.setitimer + (use "Eio.Time.Timeout.v")) + (forbid Unix.getitimer + (use "Eio.Time.Mono.now")) + (forbid Eio_unix.sleep + (use "Eio.Time.Mono.sleep_span")) + (forbid Eio.Time.sleep + (use "Eio.Time.Mono.sleep_span")) + (forbid Eio.Time.sleep_until + (use "Eio.Time.Mono.sleep_span, over the span from now to the instant")) + (forbid Eio.Time.with_timeout + (use "Eio.Time.Timeout.v, given an Mtime.Span.t")) + (forbid Eio.Time.with_timeout_exn + (use "Eio.Time.Timeout.v, given an Mtime.Span.t")) + (forbid Eio.Time.Timeout.seconds + (use "Eio.Time.Timeout.v, given an Mtime.Span.t")) + (forbid Eio.Time.Mono.sleep + (use "Eio.Time.Mono.sleep_span")) + (forbid Uri.query + (use "what the router decoded and validated")) + (forbid Uri.get_query_param + (use "what the router decoded and validated")) + ; Refused outside a library's clock module. + (forbid Eio.Time.now + (use "the library's instant.ml, which gives a Ptime.t") + (except **/instant.ml)) + (forbid Unix.sleepf + (use "the library's instant.ml, which takes an Mtime.Span.t") + (except **/instant.ml)) + ; Refused in a library: a program, a test and an example own their process, + ; wherever in the tree its bin/, test/, examples/ or bench/ is. + (forbid Stdlib.Sys.getenv + (use "a value the executable read at startup and passed in") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.Sys.getenv_opt + (use "a value the executable read at startup and passed in") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Unix.getenv + (use "a value the executable read at startup and passed in") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Unix.environment + (use "a value the executable read at startup and passed in") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Unix.putenv + (use "an argument: a library sets no global state") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.Sys.argv + (use "a value the executable read at startup and passed in") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.Sys.signal + (use "the executable: it owns the process's signals") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.Sys.set_signal + (use "the executable: it owns the process's signals") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.Sys.chdir + (use "a directory capability the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.Sys.command + (use "Eio.Process, on the process manager the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.at_exit + (use "Eio.Switch.on_release, on the switch the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.Random.self_init + (use "a Random.State.t, or the secure random source the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.Random.init + (use "a Random.State.t, or the secure random source the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Logs.set_reporter + (use "the executable: it configures the log reporter") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Logs.set_level + (use "the executable: it configures the log level") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.print_string + (use "a log on the library's own source, or a formatter the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.print_endline + (use "a log on the library's own source, or a formatter the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.print_bytes + (use "a log on the library's own source, or a formatter the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.print_char + (use "a log on the library's own source, or a formatter the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.print_int + (use "a log on the library's own source, or a formatter the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.print_float + (use "a log on the library's own source, or a formatter the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.print_newline + (use "a log on the library's own source, or a formatter the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.prerr_string + (use "a log on the library's own source, or a formatter the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.prerr_endline + (use "a log on the library's own source, or a formatter the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.prerr_bytes + (use "a log on the library's own source, or a formatter the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.prerr_char + (use "a log on the library's own source, or a formatter the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.prerr_int + (use "a log on the library's own source, or a formatter the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.prerr_float + (use "a log on the library's own source, or a formatter the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.prerr_newline + (use "a log on the library's own source, or a formatter the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.stdout + (use "a sink the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.stderr + (use "a sink the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.Printf.printf + (use "a log on the library's own source, or a formatter the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.Printf.eprintf + (use "a log on the library's own source, or a formatter the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.Format.printf + (use "a log on the library's own source, or a formatter the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.Format.eprintf + (use "a log on the library's own source, or a formatter the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.Format.print_string + (use "a log on the library's own source, or a formatter the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.Format.std_formatter + (use "a formatter the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Stdlib.Format.err_formatter + (use "a formatter the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Fmt.pr + (use "a log on the library's own source, or a formatter the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Fmt.epr + (use "a log on the library's own source, or a formatter the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Eio_main.run + (use "the env, switch or capability the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Ptime_clock.now + (use "the clock or ~now the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Mtime_clock.now + (use "the clock the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Mtime_clock.elapsed + (use "the clock the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Mtime_clock.counter + (use "the clock the caller gives") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + ; Refused in a Melange library: the console is the page's. + (forbid Js.log + (use "a value returned to the caller") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Js.log2 + (use "a value returned to the caller") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Js.log3 + (use "a value returned to the caller") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Js.log4 + (use "a value returned to the caller") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Js.logMany + (use "a value returned to the caller") + (except **/bin/** **/test/** **/examples/** **/bench/**)) + (forbid Js.Console + (use "a value returned to the caller") + (except **/bin/** **/test/** **/examples/** **/bench/**))) + +; wiretype's own. + +(per-path + (paths src/gen/**) + (ignore restricted-dependency) + (reason "build-time programs: each prints the table dune writes into a module")) diff --git a/ppx/ppx_wiretype.ml b/ppx/ppx_wiretype.ml index 6921e11..4d26ec5 100644 --- a/ppx/ppx_wiretype.ml +++ b/ppx/ppx_wiretype.ml @@ -1,4 +1,7 @@ -open Ppxlib +open Ppxlib [@@litany.allow + "open-of-ordinary-module: every ppxlib rewriter opens Ppxlib to \ + read the AST it matches"] + module B = Ast_builder.Default (* ------------------------------------------------------------------ *) @@ -1296,7 +1299,8 @@ let str_type_decl ~ctxt (rec_flag, tds) = [%expr Stdlib.ignore (Stdlib.Lazy.force - [%e B.evar ~loc (lazy_name td.ptype_name.txt)])]) + [%e B.evar ~loc (lazy_name td.ptype_name.txt)] + : _ Wiretype.t)]) plain)); ]; ]) @@ -1358,7 +1362,7 @@ let () = Attribute.T A.ct_with; ] in - ignore + Deriving.ignore (Deriving.add "wiretype" ~str_type_decl: (Deriving.Generator.V2.make_noarg ~attributes str_type_decl) diff --git a/ppx/ppx_wiretype.mli b/ppx/ppx_wiretype.mli index f9dc162..52fcb65 100644 --- a/ppx/ppx_wiretype.mli +++ b/ppx/ppx_wiretype.mli @@ -2,6 +2,12 @@ [_json] -- [json] for a type named [t] -- and in a signature its [val]. + Its documents, a page for each part: + [$(opam var ppx_wiretype:doc)/llms.txt]. + + Its page, with its examples: + [$(opam var ppx_wiretype:doc)/pages/guide/deriver.md]. + - A {b record} is an object named by the type (a [t] by its module), each field a member; one with parameters is named only by [[@@kind]]. An [option] field may be left out or [null] ([Object.opt_mem]). An option of diff --git a/src/bounds.mli b/src/bounds.mli index 77cb77e..874feca 100644 --- a/src/bounds.mli +++ b/src/bounds.mli @@ -1,7 +1,10 @@ (** The bounds a description states, each one a value breaks said with a code and a sentence: the reader and the writer hold a value to them alike, so what {!Encode} writes {!Decode} reads, and the two say a bound in the same - words. *) + words. + + Its page, with its examples: + [$(opam var wiretype:doc)/pages/guide/bounds.md]. *) type broken = (Problem.code * string) list (** Each bound broken, in the order the description states them; empty where the diff --git a/src/decode.ml b/src/decode.ml index 483e2ef..6981c65 100644 --- a/src/decode.ml +++ b/src/decode.ml @@ -816,7 +816,10 @@ and dict : type k v. st -> path -> int -> (k, v) S.dict -> (k * v) list option = let spelling k = match d.key with | S.String _ -> None - | _ -> Result.to_option (Encode.run d.key k) + | S.Null _ | S.Bool | S.Int _ | S.Int64 _ | S.Number _ | S.Enum _ + | S.List _ | S.Tuple _ | S.Dict _ | S.Nullable _ | S.Object _ | S.Any _ + | S.Map _ | S.Value | S.Rec _ -> + Result.to_option (Encode.run d.key k) in let repeated at = report st at P.Repeated_member "This member is given more than once."; @@ -874,7 +877,9 @@ and key : type k. st -> path -> k S.t -> string -> k option = fun st at shape name -> match shape with | S.String l -> string_bounded st at l name - | _ -> ( + | S.Null _ | S.Bool | S.Int _ | S.Int64 _ | S.Number _ | S.Enum _ | S.List _ + | S.Tuple _ | S.Dict _ | S.Nullable _ | S.Object _ | S.Any _ | S.Map _ + | S.Value | S.Rec _ -> ( let quoted = state ~max_depth:st.max_depth ~max_problems:st.max_problems (Text.quote name) diff --git a/src/decode.mli b/src/decode.mli index a6faff6..721ac52 100644 --- a/src/decode.mli +++ b/src/decode.mli @@ -1,5 +1,8 @@ (** Reading a document by its description: every problem it has, but for one - that ends reading, which is reported alone. *) + that ends reading, which is reported alone. + + Its page, with its examples: + [$(opam var wiretype:doc)/pages/guide/reading.md]. *) val run : ?max_depth:int -> diff --git a/src/encode.mli b/src/encode.mli index a0d156a..98beaee 100644 --- a/src/encode.mli +++ b/src/encode.mli @@ -1,4 +1,7 @@ -(** Writing a value by its description, minified. *) +(** Writing a value by its description, minified. + + Its page, with its examples: + [$(opam var wiretype:doc)/pages/guide/writing.md]. *) val max_depth : int (** How deep a document may nest: what {!Decode.run} reads unless told diff --git a/src/grammar.ml b/src/grammar.ml index 1493510..b2f1498 100644 --- a/src/grammar.ml +++ b/src/grammar.ml @@ -760,7 +760,9 @@ let domain host = let cps = List.filter_map Fun.id read in let rtl = Array.exists (fun c -> - match bidi_class c with R | AL | AN -> true | _ -> false) + match bidi_class c with + | R | AL | AN -> true + | L | EN | ES | CS | ET | ON | BN | NSM | Other -> false) in (not (List.exists rtl cps)) || List.for_all bidi_label cps diff --git a/src/grammar.mli b/src/grammar.mli index 11bcce9..063d424 100644 --- a/src/grammar.mli +++ b/src/grammar.mli @@ -1,5 +1,7 @@ (** The grammars behind the ready-made kinds. Each accepts no string the - browser's Zod check for it refuses; each refusal is a sentence. *) + browser's Zod check for it refuses; each refusal is a sentence. + + Its documents, a page for each part: [$(opam var wiretype:doc)/llms.txt]. *) val instant_of_string : string -> (Ptime.t, string) result (** RFC 3339 [date-time] as Zod's [iso.datetime({ offset: true })] reads it: a diff --git a/src/itself.mli b/src/itself.mli index 9c2c0b5..8e19cb3 100644 --- a/src/itself.mli +++ b/src/itself.mli @@ -1,4 +1,6 @@ -(** A description of itself, built where it is first reached. *) +(** A description of itself, built where it is first reached. + + Its documents, a page for each part: [$(opam var wiretype:doc)/llms.txt]. *) val force : 'a Lazy.t -> 'a (** [Lazy.force], by whichever domain reaches it first, while another reaching diff --git a/src/problem.mli b/src/problem.mli index eedfb91..8d2d588 100644 --- a/src/problem.mli +++ b/src/problem.mli @@ -2,7 +2,9 @@ sentence about that one place. Decoding reports every problem a document has, but for a {!Syntax} or {!Too_deep} one, which is reported alone, and past its limit of problems, where it stops and ends the list with a - {!Too_many_problems}. *) + {!Too_many_problems}. + + Its documents, a page for each part: [$(opam var wiretype:doc)/llms.txt]. *) type segment = | Member of string diff --git a/src/schema.ml b/src/schema.ml index e51f17b..de062fb 100644 --- a/src/schema.ml +++ b/src/schema.ml @@ -348,7 +348,8 @@ let rec walk_at_depth : type a. ctx -> dir -> string -> int -> a S.t -> t = let keys = walk_at_depth ctx dir (at ^ "[name]") depth d.key in (match undescribed keys with | String _ -> () - | _ -> + | Any | Null | Boolean | Number _ | Integer _ | Array _ | Tuple _ | Dict _ + | Object _ | Ref _ | Nullable _ | Union _ | Tagged _ | Described _ -> error ctx at Key_not_text "A map's names are text, and its key's description is not."); Dict @@ -760,7 +761,11 @@ module Json_schema = struct let document components root = let root = - match of_t root with Value.Object members -> members | _ -> [] + match of_t root with + | Value.Object members -> members + | Value.Null | Value.Bool _ | Value.Number _ | Value.String _ + | Value.Array _ -> + [] in Value.Object ((("$schema", str "https://json-schema.org/draft/2020-12/schema") :: root) @@ -923,7 +928,11 @@ module Zod = struct let record = match undescribed d.keys with | String { words = Some _; _ } -> "z.partialRecord(" - | _ -> "z.record(" + | String { words = None; _ } + | Any | Null | Boolean | Number _ | Integer _ | Array _ | Tuple _ + | Dict _ | Object _ | Ref _ | Nullable _ | Union _ | Tagged _ + | Described _ -> + "z.record(" in checked (record @@ -1003,7 +1012,9 @@ module Zod = struct ((match s with | Object o -> comment "" o.about | Described d -> comment "" d.doc - | _ -> "") + | Any | Null | Boolean | Number _ | Integer _ | String _ | Array _ + | Tuple _ | Dict _ | Ref _ | Nullable _ | Union _ | Tagged _ -> + "") ^ Printf.sprintf "export const %sSchema = %s;\n\ export type %s = z.infer;\n\n" @@ -1266,12 +1277,11 @@ module Melange = struct | `Null -> "JSONNull" | `Any -> "_" - let either = function + let rec either = function | [] -> "nothing at all" | [ w ] -> w - | ws -> - let rev = List.rev ws in - String.concat ", " (List.rev (List.tl rev)) ^ " or " ^ List.hd rev + | [ w; last ] -> w ^ " or " ^ last + | w :: rest -> w ^ ", " ^ either rest let declare st ~hint ~doc body = let tname = fresh st.types hint in @@ -1399,12 +1409,14 @@ module Melange = struct let n = List.length items in let vars = List.init n (fun i -> Printf.sprintf "i%d" i) in let tuple vs = - if n = 1 then List.hd vs else "(" ^ String.concat ", " vs ^ ")" + match vs with [ v ] -> v | vs -> "(" ^ String.concat ", " vs ^ ")" in { ty = - (if n = 1 then (List.hd items).ty - else String.concat " * " (List.map (fun p -> paren p.ty) items)); + (match items with + | [ item ] -> item.ty + | items -> + String.concat " * " (List.map (fun p -> paren p.ty) items)); read = Printf.sprintf "(fun ctx path j -> match Wire.tuple %d ctx path j with | None \ diff --git a/src/schema.mli b/src/schema.mli index 3c4f97c..a55b98b 100644 --- a/src/schema.mli +++ b/src/schema.mli @@ -2,6 +2,11 @@ 2020-12 and as Zod, so the two cannot disagree. It knows no HTTP: a web framework's document is built from it, and so can anybody's. + Its page, with its examples: + [$(opam var wiretype:doc)/pages/guide/json-schema.md]. + + Its page, with its examples: [$(opam var wiretype:doc)/pages/guide/zod.md]. + A description is walked in a {!dir}ection: what a request's body reads ({!Decode}) and what an answer writes ({!Encode}) differ where a member is read-only, write-only, or made by {!Wiretype.Object.opt_mem}, which reads diff --git a/src/shape.mli b/src/shape.mli index 15fe011..1b1ecd7 100644 --- a/src/shape.mli +++ b/src/shape.mli @@ -2,6 +2,9 @@ and everything that reads a description walks -- the decoder, the encoder, the schema printers, and anybody with a use of their own. + Its page, with its examples: + [$(opam var wiretype:doc)/pages/guide/walking.md]. + It is the library's API and its promise: a constructor added is a new version, so a walk that matches every case is told by the compiler. A description is built with the combinators in {!Wiretype}; one built here diff --git a/src/text.ml b/src/text.ml index 341c116..7e136f9 100644 --- a/src/text.ml +++ b/src/text.ml @@ -106,7 +106,7 @@ let spelled x = let exp = e + n - 1 in let b = Buffer.create 24 in if Float.sign_bit x then Buffer.add_char b '-'; - if exp >= -4 && exp < max 16 n then + if exp >= -4 && exp < Int.max 16 n then begin if e >= 0 then ( Buffer.add_string b digits; Buffer.add_string b (String.make e '0')) diff --git a/src/text.mli b/src/text.mli index 40140b9..2268729 100644 --- a/src/text.mli +++ b/src/text.mli @@ -1,6 +1,8 @@ (** JSON's two spellings that are not obvious, written into a buffer: what a document is written with, and what a caller writing JSON by hand -- a log - line -- writes with too, so the two spell alike. *) + line -- writes with too, so the two spell alike. + + Its documents, a page for each part: [$(opam var wiretype:doc)/llms.txt]. *) val number : Buffer.t -> float -> unit (** In the fewest digits that read back as the same double, and of those the diff --git a/src/unwritable.mli b/src/unwritable.mli index ac8bb2c..73eca6b 100644 --- a/src/unwritable.mli +++ b/src/unwritable.mli @@ -1,6 +1,8 @@ (** What could not be written: where in the value, a code a caller branches on, and a sentence about that one place. Writing stops at the first, since a - document written in part is no document. *) + document written in part is no document. + + Its documents, a page for each part: [$(opam var wiretype:doc)/llms.txt]. *) type code = | Read_only diff --git a/src/value.mli b/src/value.mli index d70e535..876e53e 100644 --- a/src/value.mli +++ b/src/value.mli @@ -1,5 +1,7 @@ (** Any JSON value: what reads what nobody described. + Its documents, a page for each part: [$(opam var wiretype:doc)/llms.txt]. + A number is a double, as I-JSON's (RFC 7493) are; an exact integer is read through a description ({!Wiretype.int}), which reads the digits. An object's members are in the order the document has them. *) diff --git a/src/wiretype.mli b/src/wiretype.mli index 45bce05..f06057e 100644 --- a/src/wiretype.mli +++ b/src/wiretype.mli @@ -1,6 +1,8 @@ (** JSON as descriptions: one value describes a shape, reads it, writes it and says what it is. + Its documents, a page for each part: [$(opam var wiretype:doc)/llms.txt]. + {[ module J = Wiretype diff --git a/test/test_ppx_wiretype.ml b/test/test_ppx_wiretype.ml index 5f8da50..6a7810c 100644 --- a/test/test_ppx_wiretype.ml +++ b/test/test_ppx_wiretype.ml @@ -3,7 +3,9 @@ in-process by the deriver this executable links, which reports a refusal as an error in its output. *) -open Ppxlib +open Ppxlib [@@litany.allow + "open-of-ordinary-module: every ppxlib rewriter opens Ppxlib to \ + read the AST it matches"] let contains s sub = let n = String.length s and m = String.length sub in diff --git a/test/test_schema.ml b/test/test_schema.ml index b3ceee7..b7ccaa9 100644 --- a/test/test_schema.ml +++ b/test/test_schema.ml @@ -246,7 +246,8 @@ let test_cases_are_one_of_each () = (* A tag that may be left out is optional in the case it stands for, and in no other: a move is [{"pass": true}] or a point with no [pass] at all. *) -type move = Pass | Play of (int * int) +type square = int * int +type move = Pass | Play of square let move_json = let passed = diff --git a/test/test_wiretype.ml b/test/test_wiretype.ml index ae82101..d9604ee 100644 --- a/test/test_wiretype.ml +++ b/test/test_wiretype.ml @@ -1042,9 +1042,9 @@ let only_itself () = (J.decode chain_json {|{"next":{"next":null}}|} = Ok chain) let malformed_descriptions () = - let refused name message f = + let refused (type a) name message (f : unit -> a) = Alcotest.check_raises name (Invalid_argument message) (fun () -> - ignore (f ())) + ignore (f () : a)) in let obj () = J.Object.map (fun a -> a) in let case tag = @@ -1855,9 +1855,12 @@ let raised_across_domains make read = while not (Atomic.get go) do Domain.cpu_relax () done; - match read shape with + (match read shape with | () -> () - | exception _ -> Atomic.incr raised)) + | exception _ -> Atomic.incr raised) + [@litany.allow + "suspicious-catch-all-handler: the count is of every exception \ + a read raises, whichever it is"])) in Atomic.set go true; List.iter Domain.join readers diff --git a/wiretype.opam b/wiretype.opam index c1a0b53..9401a07 100644 --- a/wiretype.opam +++ b/wiretype.opam @@ -20,6 +20,7 @@ depends: [ "merlin" {with-dev-setup & >= "5.6-504"} "ocaml-lsp-server" {with-dev-setup & >= "1.25.0"} "ocamlformat" {with-dev-setup & = "0.29.0"} + "hypha-litany" {with-dev-setup & >= "1.3.0" & < "2.0.0"} "odoc" {with-doc} ] build: [