atproto made easy crates.io/crates/jacquard

codegen: add extern_path #79

Open jogga.fit
targeting main from jogga.fit/jacquard:extern-path
Open opened by jogga.fit
targeting main from jogga.fit/jacquard:extern-path

CodeGenerator::extern_path(nsid_prefix, root) makes refs under a prefix use another crate's types, such as jacquard-api's, and skips emitting those lexicons. It works like prost-build's Config::extern_path.

CodeGenerator::with_mode(&corpus, "crate", CodegenMode::Pretty)
    .extern_path("com.atproto", "jacquard_api")
  • LexiconCorpus::insert adds a doc, such as StrongRef::lexicon_doc(), without its JSON.
  • When several prefixes match, the longest wins. Matches are by whole segment only.
  • Extern namespaces are left out of the emitted modules and the Cargo feature deps.
  • An extern ref that is not in the corpus is CodegenError::MissingExternRef, not a silent Data field.
  • A prefix that covers no lexicon in the corpus, such as a typo, is CodegenError::UnusedExternPath.

Why? #

It helps to not have to copy and maintain a copy of a lexicon, such as the StrongRef, by source file(json) in the user's repo and instead depend on a well maintained version, through a (compatibly generated) crate, exporting both the type definitions and the generated code.

Tests: unit tests for prefix matching, the unused-prefix and missing-ref errors, and feature deps. jacquard-codegen-tests generates a fixture against jacquard-api's StrongRef (own-module path) and label.defs#label (*.defs path, in an array) and compile-checks the types.

NOTE: we have double and triple checked the changes in this PR, as required by AGENTS.md

Commits 3

[jacquard-codegen-tests] test extern_path with jacquard-api types
[jacquard-lexicon] codegen: add extern_path
[jacquard-lexicon] corpus: add insert
Sign up or Sign in to add to the discussion.