@codama/fragments 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (97) hide show
  1. package/LICENSE +23 -0
  2. package/README.md +542 -0
  3. package/dist/index.browser.cjs +208 -0
  4. package/dist/index.browser.cjs.map +1 -0
  5. package/dist/index.browser.mjs +176 -0
  6. package/dist/index.browser.mjs.map +1 -0
  7. package/dist/index.node.cjs +195 -0
  8. package/dist/index.node.cjs.map +1 -0
  9. package/dist/index.node.mjs +163 -0
  10. package/dist/index.node.mjs.map +1 -0
  11. package/dist/index.react-native.mjs +176 -0
  12. package/dist/index.react-native.mjs.map +1 -0
  13. package/dist/javascript.browser.cjs +403 -0
  14. package/dist/javascript.browser.cjs.map +1 -0
  15. package/dist/javascript.browser.mjs +353 -0
  16. package/dist/javascript.browser.mjs.map +1 -0
  17. package/dist/javascript.node.cjs +390 -0
  18. package/dist/javascript.node.cjs.map +1 -0
  19. package/dist/javascript.node.mjs +340 -0
  20. package/dist/javascript.node.mjs.map +1 -0
  21. package/dist/javascript.react-native.mjs +353 -0
  22. package/dist/javascript.react-native.mjs.map +1 -0
  23. package/dist/rust.browser.cjs +371 -0
  24. package/dist/rust.browser.cjs.map +1 -0
  25. package/dist/rust.browser.mjs +322 -0
  26. package/dist/rust.browser.mjs.map +1 -0
  27. package/dist/rust.node.cjs +358 -0
  28. package/dist/rust.node.cjs.map +1 -0
  29. package/dist/rust.node.mjs +309 -0
  30. package/dist/rust.node.mjs.map +1 -0
  31. package/dist/rust.react-native.mjs +322 -0
  32. package/dist/rust.react-native.mjs.map +1 -0
  33. package/dist/types/core/BaseFragment.d.ts +21 -0
  34. package/dist/types/core/BaseFragment.d.ts.map +1 -0
  35. package/dist/types/core/casing.d.ts +52 -0
  36. package/dist/types/core/casing.d.ts.map +1 -0
  37. package/dist/types/core/createFragmentTemplate.d.ts +38 -0
  38. package/dist/types/core/createFragmentTemplate.d.ts.map +1 -0
  39. package/dist/types/core/fs.d.ts +28 -0
  40. package/dist/types/core/fs.d.ts.map +1 -0
  41. package/dist/types/core/index.d.ts +9 -0
  42. package/dist/types/core/index.d.ts.map +1 -0
  43. package/dist/types/core/mapFragmentContent.d.ts +43 -0
  44. package/dist/types/core/mapFragmentContent.d.ts.map +1 -0
  45. package/dist/types/core/path.d.ts +43 -0
  46. package/dist/types/core/path.d.ts.map +1 -0
  47. package/dist/types/core/renderMap.d.ts +61 -0
  48. package/dist/types/core/renderMap.d.ts.map +1 -0
  49. package/dist/types/core/setFragmentContent.d.ts +23 -0
  50. package/dist/types/core/setFragmentContent.d.ts.map +1 -0
  51. package/dist/types/index.d.ts +17 -0
  52. package/dist/types/index.d.ts.map +1 -0
  53. package/dist/types/javascript/ImportMap.d.ts +61 -0
  54. package/dist/types/javascript/ImportMap.d.ts.map +1 -0
  55. package/dist/types/javascript/addToImportMap.d.ts +25 -0
  56. package/dist/types/javascript/addToImportMap.d.ts.map +1 -0
  57. package/dist/types/javascript/fragment.d.ts +135 -0
  58. package/dist/types/javascript/fragment.d.ts.map +1 -0
  59. package/dist/types/javascript/getDocblockFragment.d.ts +53 -0
  60. package/dist/types/javascript/getDocblockFragment.d.ts.map +1 -0
  61. package/dist/types/javascript/getExportAllFragment.d.ts +21 -0
  62. package/dist/types/javascript/getExportAllFragment.d.ts.map +1 -0
  63. package/dist/types/javascript/getExternalDependencies.d.ts +29 -0
  64. package/dist/types/javascript/getExternalDependencies.d.ts.map +1 -0
  65. package/dist/types/javascript/importMapToString.d.ts +40 -0
  66. package/dist/types/javascript/importMapToString.d.ts.map +1 -0
  67. package/dist/types/javascript/index.d.ts +23 -0
  68. package/dist/types/javascript/index.d.ts.map +1 -0
  69. package/dist/types/javascript/mergeImportMaps.d.ts +34 -0
  70. package/dist/types/javascript/mergeImportMaps.d.ts.map +1 -0
  71. package/dist/types/javascript/removeFromImportMap.d.ts +21 -0
  72. package/dist/types/javascript/removeFromImportMap.d.ts.map +1 -0
  73. package/dist/types/javascript/resolveImportMap.d.ts +33 -0
  74. package/dist/types/javascript/resolveImportMap.d.ts.map +1 -0
  75. package/dist/types/rust/ImportMap.d.ts +52 -0
  76. package/dist/types/rust/ImportMap.d.ts.map +1 -0
  77. package/dist/types/rust/addAliasToImportMap.d.ts +24 -0
  78. package/dist/types/rust/addAliasToImportMap.d.ts.map +1 -0
  79. package/dist/types/rust/addToImportMap.d.ts +27 -0
  80. package/dist/types/rust/addToImportMap.d.ts.map +1 -0
  81. package/dist/types/rust/fragment.d.ts +118 -0
  82. package/dist/types/rust/fragment.d.ts.map +1 -0
  83. package/dist/types/rust/getDocblockFragment.d.ts +53 -0
  84. package/dist/types/rust/getDocblockFragment.d.ts.map +1 -0
  85. package/dist/types/rust/getExternalDependencies.d.ts +30 -0
  86. package/dist/types/rust/getExternalDependencies.d.ts.map +1 -0
  87. package/dist/types/rust/importMapToString.d.ts +30 -0
  88. package/dist/types/rust/importMapToString.d.ts.map +1 -0
  89. package/dist/types/rust/index.d.ts +23 -0
  90. package/dist/types/rust/index.d.ts.map +1 -0
  91. package/dist/types/rust/mergeImportMaps.d.ts +23 -0
  92. package/dist/types/rust/mergeImportMaps.d.ts.map +1 -0
  93. package/dist/types/rust/removeFromImportMap.d.ts +20 -0
  94. package/dist/types/rust/removeFromImportMap.d.ts.map +1 -0
  95. package/dist/types/rust/resolveImportMap.d.ts +32 -0
  96. package/dist/types/rust/resolveImportMap.d.ts.map +1 -0
  97. package/package.json +106 -0
@@ -0,0 +1,34 @@
1
+ import type { ImportInfo, ImportMap } from './ImportMap';
2
+ /**
3
+ * Merge multiple import maps into one. Modules and identifiers from later
4
+ * maps are layered over earlier ones; collisions on the same `usedIdentifier`
5
+ * are resolved by {@link preferIncoming}.
6
+ *
7
+ * The merge is a pure function: input maps are not mutated. The returned map
8
+ * is frozen.
9
+ *
10
+ * @param importMaps - The import maps to merge, in priority order.
11
+ * @return A frozen import map that contains every entry from every input.
12
+ *
13
+ * @example
14
+ * ```ts
15
+ * import { addToImportMap, createImportMap, mergeImportMaps } from '@codama/fragments/javascript';
16
+ *
17
+ * const a = addToImportMap(createImportMap(), './foo', ['Foo']);
18
+ * const b = addToImportMap(createImportMap(), './bar', ['Bar']);
19
+ * const merged = mergeImportMaps([a, b]);
20
+ * ```
21
+ */
22
+ export declare function mergeImportMaps(importMaps: readonly ImportMap[]): ImportMap;
23
+ /**
24
+ * Decide whether an incoming `ImportInfo` should replace an existing entry
25
+ * for the same `usedIdentifier`.
26
+ *
27
+ * The single rule we apply: if both refer to the same source identifier and
28
+ * one is type-only while the other is a value import, the value import wins.
29
+ * In every other "tied" case the existing entry stays. This keeps a value
30
+ * import from being silently downgraded to type-only when both are
31
+ * encountered.
32
+ */
33
+ export declare function preferIncoming(existing: ImportInfo | undefined, incoming: ImportInfo): boolean;
34
+ //# sourceMappingURL=mergeImportMaps.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mergeImportMaps.d.ts","sourceRoot":"","sources":["../../../src/javascript/mergeImportMaps.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,SAAS,EAA0B,MAAM,aAAa,CAAC;AAGjF;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,eAAe,CAAC,UAAU,EAAE,SAAS,SAAS,EAAE,GAAG,SAAS,CAmB3E;AAED;;;;;;;;;GASG;AACH,wBAAgB,cAAc,CAAC,QAAQ,EAAE,UAAU,GAAG,SAAS,EAAE,QAAQ,EAAE,UAAU,GAAG,OAAO,CAG9F"}
@@ -0,0 +1,21 @@
1
+ import type { ImportMap, Module, UsedIdentifier } from './ImportMap';
2
+ /**
3
+ * Remove identifiers from a module entry. If the module ends up with no
4
+ * remaining identifiers, the module entry itself is dropped from the map.
5
+ *
6
+ * @param importMap - The import map to modify.
7
+ * @param module - The source module to remove identifiers from.
8
+ * @param usedIdentifiers - The used-identifier names to drop. Names that
9
+ * aren't present in the module entry are silently ignored.
10
+ * @return A new frozen import map without those identifiers.
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * import { addToImportMap, createImportMap, removeFromImportMap } from '@codama/fragments/javascript';
15
+ *
16
+ * let map = addToImportMap(createImportMap(), './foo', ['Foo', 'Bar']);
17
+ * map = removeFromImportMap(map, './foo', ['Foo']);
18
+ * ```
19
+ */
20
+ export declare function removeFromImportMap(importMap: ImportMap, module: Module, usedIdentifiers: UsedIdentifier[]): ImportMap;
21
+ //# sourceMappingURL=removeFromImportMap.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"removeFromImportMap.d.ts","sourceRoot":"","sources":["../../../src/javascript/removeFromImportMap.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAErE;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,mBAAmB,CAC/B,SAAS,EAAE,SAAS,EACpB,MAAM,EAAE,MAAM,EACd,eAAe,EAAE,cAAc,EAAE,GAClC,SAAS,CAUX"}
@@ -0,0 +1,33 @@
1
+ import type { ImportMap } from './ImportMap';
2
+ /**
3
+ * Rewrite a JavaScript import map's module keys against a dependency map.
4
+ *
5
+ * Resolution is by exact module-name lookup: a module key `'solanaAddresses'`
6
+ * against `{ solanaAddresses: '@solana/kit' }` becomes `'@solana/kit'`.
7
+ * Keys that aren't present in `dependencies` are kept unchanged. The
8
+ * inner `UsedIdentifier → ImportInfo` map for each module is preserved
9
+ * exactly. When two source modules resolve to the same target, their
10
+ * inner maps are merged via {@link mergeImportMaps}.
11
+ *
12
+ * Renderers typically pre-define a base set of symbolic modules
13
+ * (`solanaAddresses → @solana/kit`, `generated → ..`, etc.) and pass
14
+ * them through this function just before calling
15
+ * {@link importMapToString} or {@link getExternalDependencies}.
16
+ *
17
+ * @param importMap - The import map to resolve.
18
+ * @param dependencies - A record mapping symbolic module names to
19
+ * resolved module specifiers.
20
+ * @return A new frozen import map with all module keys resolved.
21
+ *
22
+ * @example
23
+ * ```ts
24
+ * import { addToImportMap, createImportMap, resolveImportMap } from '@codama/fragments/javascript';
25
+ *
26
+ * let map = createImportMap();
27
+ * map = addToImportMap(map, 'solanaAddresses', ['Address']);
28
+ * const resolved = resolveImportMap(map, { solanaAddresses: '@solana/kit' });
29
+ * // resolved keys: ['@solana/kit']
30
+ * ```
31
+ */
32
+ export declare function resolveImportMap(importMap: ImportMap, dependencies: Record<string, string>): ImportMap;
33
+ //# sourceMappingURL=resolveImportMap.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"resolveImportMap.d.ts","sourceRoot":"","sources":["../../../src/javascript/resolveImportMap.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAc,SAAS,EAA0B,MAAM,aAAa,CAAC;AAGjF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,wBAAgB,gBAAgB,CAAC,SAAS,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,SAAS,CAStG"}
@@ -0,0 +1,52 @@
1
+ /**
2
+ * Rust crate keywords that should never be reported as external dependencies.
3
+ *
4
+ * {@link getExternalDependencies} filters these out when computing the
5
+ * package's Cargo dependencies from the imports actually used. They
6
+ * correspond to paths that resolve inside the crate or to the standard
7
+ * library.
8
+ */
9
+ export declare const RUST_CORE_IMPORTS: ReadonlySet<string>;
10
+ /**
11
+ * The fully-qualified Rust path of an imported item — e.g.
12
+ * `'solana_program::pubkey::Pubkey'` or `'crate::generated::accounts::AccountNode'`.
13
+ *
14
+ * Symbolic prefixes (e.g. `'generated::accounts::Foo'`) are accepted too;
15
+ * they are resolved into concrete paths by {@link resolveImportMap} just
16
+ * before stringification.
17
+ */
18
+ export type ImportPath = string;
19
+ /** The local name an imported path is bound to (the right-hand side of `as`). */
20
+ export type Alias = string;
21
+ /** A parsed Rust import. */
22
+ export interface ImportInfo {
23
+ readonly alias?: Alias;
24
+ readonly importedPath: ImportPath;
25
+ }
26
+ /**
27
+ * The data model used by `@codama/fragments/rust` to track imports
28
+ * symbolically until they are stringified into actual `use foo::Bar;` lines.
29
+ *
30
+ * Unlike the JavaScript flavor, Rust's `use` statement always references a
31
+ * single fully-qualified path. There is no per-module identifier list, so
32
+ * the map is flat: keyed by the imported path, with optional alias info as
33
+ * the value.
34
+ *
35
+ * The map is frozen and its operations are pure functions (no methods, no
36
+ * mutation), matching the JavaScript subpath's paradigm.
37
+ */
38
+ export type ImportMap = ReadonlyMap<ImportPath, ImportInfo>;
39
+ /**
40
+ * Construct an empty, frozen import map.
41
+ *
42
+ * @return A new {@link ImportMap} with no entries.
43
+ *
44
+ * @example
45
+ * ```ts
46
+ * import { createImportMap } from '@codama/fragments/rust';
47
+ *
48
+ * const empty = createImportMap();
49
+ * ```
50
+ */
51
+ export declare function createImportMap(): ImportMap;
52
+ //# sourceMappingURL=ImportMap.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ImportMap.d.ts","sourceRoot":"","sources":["../../../src/rust/ImportMap.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,eAAO,MAAM,iBAAiB,EAAE,WAAW,CAAC,MAAM,CAQhD,CAAC;AAEH;;;;;;;GAOG;AACH,MAAM,MAAM,UAAU,GAAG,MAAM,CAAC;AAEhC,iFAAiF;AACjF,MAAM,MAAM,KAAK,GAAG,MAAM,CAAC;AAE3B,4BAA4B;AAC5B,MAAM,WAAW,UAAU;IACvB,QAAQ,CAAC,KAAK,CAAC,EAAE,KAAK,CAAC;IACvB,QAAQ,CAAC,YAAY,EAAE,UAAU,CAAC;CACrC;AAED;;;;;;;;;;;GAWG;AACH,MAAM,MAAM,SAAS,GAAG,WAAW,CAAC,UAAU,EAAE,UAAU,CAAC,CAAC;AAE5D;;;;;;;;;;;GAWG;AACH,wBAAgB,eAAe,IAAI,SAAS,CAE3C"}
@@ -0,0 +1,24 @@
1
+ import type { Alias, ImportMap, ImportPath } from './ImportMap';
2
+ /**
3
+ * Record an alias (`use foo::Bar as Baz;`) for an imported path. If the
4
+ * path isn't yet in the map, it is added; if it's already present, the
5
+ * alias is set or replaced. The input map is not mutated.
6
+ *
7
+ * @param importMap - The import map to extend.
8
+ * @param path - The full Rust path being aliased (e.g. `'foo::Bar'`).
9
+ * @param alias - The local name to use (e.g. `'Baz'`).
10
+ * @return A new frozen import map with the alias recorded.
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * import { addAliasToImportMap, createImportMap } from '@codama/fragments/rust';
15
+ *
16
+ * const map = addAliasToImportMap(
17
+ * createImportMap(),
18
+ * 'solana_program::program_error::ProgramError',
19
+ * 'ProgError',
20
+ * );
21
+ * ```
22
+ */
23
+ export declare function addAliasToImportMap(importMap: ImportMap, path: ImportPath, alias: Alias): ImportMap;
24
+ //# sourceMappingURL=addAliasToImportMap.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"addAliasToImportMap.d.ts","sourceRoot":"","sources":["../../../src/rust/addAliasToImportMap.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEhE;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,mBAAmB,CAAC,SAAS,EAAE,SAAS,EAAE,IAAI,EAAE,UAAU,EAAE,KAAK,EAAE,KAAK,GAAG,SAAS,CAInG"}
@@ -0,0 +1,27 @@
1
+ import type { ImportMap } from './ImportMap';
2
+ /**
3
+ * Append imports to an import map, returning a new frozen map. The input
4
+ * map is not mutated.
5
+ *
6
+ * Aliases are not added by this function — call
7
+ * {@link addAliasToImportMap} separately when the import needs an `as`
8
+ * clause.
9
+ *
10
+ * @param importMap - The import map to extend.
11
+ * @param paths - The Rust paths to add. May be a single string, an array,
12
+ * or a {@link Set}. An empty array short-circuits and returns `importMap`
13
+ * unchanged.
14
+ * @return A frozen import map that includes the new entries.
15
+ *
16
+ * @example
17
+ * ```ts
18
+ * import { addToImportMap, createImportMap } from '@codama/fragments/rust';
19
+ *
20
+ * const map = addToImportMap(createImportMap(), [
21
+ * 'borsh::BorshDeserialize',
22
+ * 'borsh::BorshSerialize',
23
+ * ]);
24
+ * ```
25
+ */
26
+ export declare function addToImportMap(importMap: ImportMap, paths: ReadonlySet<string> | string | readonly string[]): ImportMap;
27
+ //# sourceMappingURL=addToImportMap.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"addToImportMap.d.ts","sourceRoot":"","sources":["../../../src/rust/addToImportMap.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAc,MAAM,aAAa,CAAC;AAGzD;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,cAAc,CAC1B,SAAS,EAAE,SAAS,EACpB,KAAK,EAAE,WAAW,CAAC,MAAM,CAAC,GAAG,MAAM,GAAG,SAAS,MAAM,EAAE,GACxD,SAAS,CAWX"}
@@ -0,0 +1,118 @@
1
+ import type { BaseFragment } from '../core/BaseFragment';
2
+ import type { Alias, ImportMap, ImportPath } from './ImportMap';
3
+ /**
4
+ * The Rust-flavored fragment shape: {@link BaseFragment} plus a frozen
5
+ * {@link ImportMap} carrying the `use` paths the content depends on.
6
+ *
7
+ * Both the fragment and its import map are immutable. Every operation in
8
+ * this subpath that returns a fragment produces a new frozen wrapper, so
9
+ * fragments compose without aliasing risks.
10
+ */
11
+ export type Fragment = BaseFragment & Readonly<{
12
+ imports: ImportMap;
13
+ }>;
14
+ /**
15
+ * Type guard for the Rust-flavored {@link Fragment} shape.
16
+ *
17
+ * @param value - The value to test.
18
+ * @return `true` when `value` is an object carrying both `content` and
19
+ * `imports` fields.
20
+ */
21
+ export declare function isFragment(value: unknown): value is Fragment;
22
+ /**
23
+ * Tagged-template helper for composing Rust-flavored fragments.
24
+ * Interpolated values may be:
25
+ *
26
+ * - A {@link Fragment} — content is inlined and imports propagate.
27
+ * - `undefined` — rendered as the empty string.
28
+ * - Anything else — coerced to a string via `String(value)`.
29
+ *
30
+ * Rust does not have a `use(input, module)` shorthand because identifiers
31
+ * can be referenced inline by their full `::`-qualified path; build
32
+ * content with the tag and attach imports separately via
33
+ * {@link addFragmentImports}.
34
+ *
35
+ * @param template - The template-strings array supplied by the tag call site.
36
+ * @param items - The interpolated values, in order.
37
+ * @return A frozen {@link Fragment} with merged content and imports.
38
+ *
39
+ * @example
40
+ * ```ts
41
+ * import { addFragmentImports, fragment } from '@codama/fragments/rust';
42
+ *
43
+ * const body = fragment`pub struct AccountNode { pubkey: Pubkey }`;
44
+ * const withImports = addFragmentImports(body, ['solana_program::pubkey::Pubkey']);
45
+ * ```
46
+ */
47
+ export declare function fragment(template: TemplateStringsArray, ...items: unknown[]): Fragment;
48
+ /**
49
+ * Combine multiple fragments into one. The merge strategy for content is
50
+ * supplied by the caller (`mergeContent`); imports are merged
51
+ * automatically via {@link mergeImportMaps}. Undefined inputs are skipped.
52
+ *
53
+ * @param fragments - The fragments to merge, in order.
54
+ * @param mergeContent - A function that produces the final content string
55
+ * from each surviving fragment's content.
56
+ * @return A frozen merged {@link Fragment}.
57
+ */
58
+ export declare function mergeFragments(fragments: readonly (Fragment | undefined)[], mergeContent: (contents: string[]) => string): Fragment;
59
+ /**
60
+ * Append imports to an existing fragment's import map. The fragment's
61
+ * content and any other fields are preserved.
62
+ *
63
+ * @param fragment - The source fragment.
64
+ * @param paths - The Rust paths to add. May be a single string, an array,
65
+ * or a {@link Set}.
66
+ * @return A new frozen fragment with the extended import map.
67
+ *
68
+ * @example
69
+ * ```ts
70
+ * import { addFragmentImports, fragment } from '@codama/fragments/rust';
71
+ *
72
+ * const f = addFragmentImports(fragment`Pubkey`, ['solana_program::pubkey::Pubkey']);
73
+ * ```
74
+ */
75
+ export declare function addFragmentImports(fragment: Fragment, paths: ImportPath | ReadonlySet<string> | readonly ImportPath[]): Fragment;
76
+ /**
77
+ * Record an alias for an imported path on the fragment's import map. If
78
+ * the path isn't yet imported, it is added; if it's already present, the
79
+ * alias replaces any existing one. The fragment's content and any other
80
+ * fields are preserved.
81
+ *
82
+ * @param fragment - The source fragment.
83
+ * @param path - The full Rust path being aliased.
84
+ * @param alias - The local name to use.
85
+ * @return A new frozen fragment with the alias recorded.
86
+ *
87
+ * @example
88
+ * ```ts
89
+ * import { addFragmentImportAlias, fragment } from '@codama/fragments/rust';
90
+ *
91
+ * const f = addFragmentImportAlias(
92
+ * fragment`ProgError::InvalidArgument`,
93
+ * 'solana_program::program_error::ProgramError',
94
+ * 'ProgError',
95
+ * );
96
+ * ```
97
+ */
98
+ export declare function addFragmentImportAlias(fragment: Fragment, path: ImportPath, alias: Alias): Fragment;
99
+ /**
100
+ * Merge additional import maps into an existing fragment's import map.
101
+ * The fragment's content and any other fields are preserved.
102
+ *
103
+ * @param fragment - The source fragment.
104
+ * @param importMaps - The maps to merge in.
105
+ * @return A new frozen fragment with the merged import map.
106
+ */
107
+ export declare function mergeFragmentImports(fragment: Fragment, importMaps: readonly ImportMap[]): Fragment;
108
+ /**
109
+ * Drop paths from a fragment's import map. The fragment's content and any
110
+ * other fields are preserved.
111
+ *
112
+ * @param fragment - The source fragment.
113
+ * @param paths - The Rust paths to remove. May be a single string, an
114
+ * array, or a {@link Set}.
115
+ * @return A new frozen fragment with the trimmed import map.
116
+ */
117
+ export declare function removeFragmentImports(fragment: Fragment, paths: ImportPath | ReadonlySet<string> | readonly ImportPath[]): Fragment;
118
+ //# sourceMappingURL=fragment.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fragment.d.ts","sourceRoot":"","sources":["../../../src/rust/fragment.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAIzD,OAAO,KAAK,EAAE,KAAK,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAIhE;;;;;;;GAOG;AACH,MAAM,MAAM,QAAQ,GAAG,YAAY,GAAG,QAAQ,CAAC;IAAE,OAAO,EAAE,SAAS,CAAA;CAAE,CAAC,CAAC;AAEvE;;;;;;GAMG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,QAAQ,CAE5D;AAED;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,QAAQ,CAAC,QAAQ,EAAE,oBAAoB,EAAE,GAAG,KAAK,EAAE,OAAO,EAAE,GAAG,QAAQ,CAEtF;AAED;;;;;;;;;GASG;AACH,wBAAgB,cAAc,CAC1B,SAAS,EAAE,SAAS,CAAC,QAAQ,GAAG,SAAS,CAAC,EAAE,EAC5C,YAAY,EAAE,CAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,MAAM,GAC7C,QAAQ,CAMV;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,kBAAkB,CAC9B,QAAQ,EAAE,QAAQ,EAClB,KAAK,EAAE,UAAU,GAAG,WAAW,CAAC,MAAM,CAAC,GAAG,SAAS,UAAU,EAAE,GAChE,QAAQ,CAKV;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,sBAAsB,CAAC,QAAQ,EAAE,QAAQ,EAAE,IAAI,EAAE,UAAU,EAAE,KAAK,EAAE,KAAK,GAAG,QAAQ,CAKnG;AAED;;;;;;;GAOG;AACH,wBAAgB,oBAAoB,CAAC,QAAQ,EAAE,QAAQ,EAAE,UAAU,EAAE,SAAS,SAAS,EAAE,GAAG,QAAQ,CAKnG;AAED;;;;;;;;GAQG;AACH,wBAAgB,qBAAqB,CACjC,QAAQ,EAAE,QAAQ,EAClB,KAAK,EAAE,UAAU,GAAG,WAAW,CAAC,MAAM,CAAC,GAAG,SAAS,UAAU,EAAE,GAChE,QAAQ,CAKV"}
@@ -0,0 +1,53 @@
1
+ import type { Fragment } from './fragment';
2
+ /**
3
+ * Build a Rust doc-comment fragment from an array of lines.
4
+ *
5
+ * Empty or `undefined` input returns `undefined` so the helper composes
6
+ * naturally with the {@link fragment} tag's optional-interpolation
7
+ * behavior — a node's `docs` attribute can be threaded straight in
8
+ * without a ternary guard:
9
+ *
10
+ * ```ts
11
+ * fragment`${getDocblockFragment(node.docs)}\npub struct X;`;
12
+ * ```
13
+ *
14
+ * Each line is prefixed with `///` (outer doc) by default, or `//!`
15
+ * (inner doc) when `internal` is `true`. Empty elements in the array
16
+ * render as a bare prefix line (`///` or `//!` with no trailing space),
17
+ * useful for paragraph breaks inside a doc comment.
18
+ *
19
+ * @param lines - The lines of the doc comment, or `undefined`. Empty
20
+ * array and `undefined` both return `undefined`.
21
+ * @param options - Optional settings.
22
+ * @param options.internal - When `true`, emit inner doc comments (`//!`)
23
+ * instead of outer doc comments (`///`). Useful for module- or crate-level
24
+ * documentation.
25
+ * @param options.withLineJump - When `true`, appends a trailing `\n` after
26
+ * the last line.
27
+ * @return A {@link Fragment} carrying the rendered doc comment, or
28
+ * `undefined` when `lines` is empty or `undefined`.
29
+ *
30
+ * @example
31
+ * ```ts
32
+ * import { getDocblockFragment } from '@codama/fragments/rust';
33
+ *
34
+ * getDocblockFragment(['Greets the user.'])?.content;
35
+ * // /// Greets the user.
36
+ *
37
+ * getDocblockFragment(['First line.', '', 'Second paragraph.'])?.content;
38
+ * // /// First line.
39
+ * // ///
40
+ * // /// Second paragraph.
41
+ *
42
+ * getDocblockFragment(['Module docs.'], { internal: true })?.content;
43
+ * // //! Module docs.
44
+ *
45
+ * getDocblockFragment(undefined);
46
+ * // undefined
47
+ * ```
48
+ */
49
+ export declare function getDocblockFragment(lines: readonly string[] | undefined, options?: {
50
+ internal?: boolean;
51
+ withLineJump?: boolean;
52
+ }): Fragment | undefined;
53
+ //# sourceMappingURL=getDocblockFragment.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getDocblockFragment.d.ts","sourceRoot":"","sources":["../../../src/rust/getDocblockFragment.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAG3C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8CG;AACH,wBAAgB,mBAAmB,CAC/B,KAAK,EAAE,SAAS,MAAM,EAAE,GAAG,SAAS,EACpC,OAAO,GAAE;IAAE,QAAQ,CAAC,EAAE,OAAO,CAAC;IAAC,YAAY,CAAC,EAAE,OAAO,CAAA;CAAO,GAC7D,QAAQ,GAAG,SAAS,CAMtB"}
@@ -0,0 +1,30 @@
1
+ import type { ImportMap } from './ImportMap';
2
+ /**
3
+ * Compute the top-level crate names actually imported, with
4
+ * {@link RUST_CORE_IMPORTS} excluded. Useful for syncing a renderer's
5
+ * generated `Cargo.toml` from the imports it ends up emitting.
6
+ *
7
+ * Imports are first resolved against the dependency map (so symbolic
8
+ * prefixes like `'generated::…'` are expanded before crate names are
9
+ * extracted), then the leading `::` segment of each path is collected.
10
+ *
11
+ * @param importMap - The import map to inspect.
12
+ * @param dependencies - The dependency map to apply before extracting
13
+ * crate names. Defaults to no resolution.
14
+ * @return A {@link Set} of external crate names.
15
+ *
16
+ * @example
17
+ * ```ts
18
+ * import { addToImportMap, createImportMap, getExternalDependencies } from '@codama/fragments/rust';
19
+ *
20
+ * const map = addToImportMap(createImportMap(), [
21
+ * 'borsh::BorshSerialize',
22
+ * 'std::collections::HashMap',
23
+ * 'generated::accounts::A',
24
+ * ]);
25
+ * getExternalDependencies(map, { generated: 'crate::generated' });
26
+ * // → Set { 'borsh' }
27
+ * ```
28
+ */
29
+ export declare function getExternalDependencies(importMap: ImportMap, dependencies?: Record<string, string>): Set<string>;
30
+ //# sourceMappingURL=getExternalDependencies.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getExternalDependencies.d.ts","sourceRoot":"","sources":["../../../src/rust/getExternalDependencies.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAI7C;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,uBAAuB,CAAC,SAAS,EAAE,SAAS,EAAE,YAAY,GAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAM,GAAG,GAAG,CAAC,MAAM,CAAC,CAQpH"}
@@ -0,0 +1,30 @@
1
+ import type { ImportMap } from './ImportMap';
2
+ /**
3
+ * Render an import map as a block of `use foo::Bar;` (and `use foo::Bar as Baz;`)
4
+ * statements.
5
+ *
6
+ * The map is first resolved against `dependencies` so symbolic prefixes
7
+ * like `'generated::…'` expand to concrete crate paths. The output is
8
+ * sorted alphabetically by path for stable, reviewable diffs.
9
+ *
10
+ * @param importMap - The import map to render.
11
+ * @param dependencies - The dependency map to apply before rendering.
12
+ * Defaults to no resolution.
13
+ * @return The block of `use` statements joined by newlines, or the empty
14
+ * string if the map is empty.
15
+ *
16
+ * @example
17
+ * ```ts
18
+ * import { addAliasToImportMap, addToImportMap, createImportMap, importMapToString } from '@codama/fragments/rust';
19
+ *
20
+ * let map = createImportMap();
21
+ * map = addToImportMap(map, ['borsh::BorshSerialize', 'solana_program::pubkey::Pubkey']);
22
+ * map = addAliasToImportMap(map, 'solana_program::program_error::ProgramError', 'ProgError');
23
+ * importMapToString(map);
24
+ * // use borsh::BorshSerialize;
25
+ * // use solana_program::program_error::ProgramError as ProgError;
26
+ * // use solana_program::pubkey::Pubkey;
27
+ * ```
28
+ */
29
+ export declare function importMapToString(importMap: ImportMap, dependencies?: Record<string, string>): string;
30
+ //# sourceMappingURL=importMapToString.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"importMapToString.d.ts","sourceRoot":"","sources":["../../../src/rust/importMapToString.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAG7C;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,iBAAiB,CAAC,SAAS,EAAE,SAAS,EAAE,YAAY,GAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAM,GAAG,MAAM,CAMzG"}
@@ -0,0 +1,23 @@
1
+ /**
2
+ * `@codama/fragments/rust`
3
+ *
4
+ * The Rust flavor of the fragment library: a concrete `Fragment` type
5
+ * carrying a frozen, functional `ImportMap`, a `fragment` tagged template
6
+ * that propagates imports through interpolation, and helpers for
7
+ * building, merging, resolving, and rendering.
8
+ *
9
+ * Re-exports the language-agnostic core too, so consumers only need a
10
+ * single import in the typical case.
11
+ */
12
+ export * from '../core';
13
+ export * from './ImportMap';
14
+ export * from './addToImportMap';
15
+ export * from './addAliasToImportMap';
16
+ export * from './mergeImportMaps';
17
+ export * from './removeFromImportMap';
18
+ export * from './resolveImportMap';
19
+ export * from './getExternalDependencies';
20
+ export * from './importMapToString';
21
+ export * from './fragment';
22
+ export * from './getDocblockFragment';
23
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/rust/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,cAAc,SAAS,CAAC;AACxB,cAAc,aAAa,CAAC;AAC5B,cAAc,kBAAkB,CAAC;AACjC,cAAc,uBAAuB,CAAC;AACtC,cAAc,mBAAmB,CAAC;AAClC,cAAc,uBAAuB,CAAC;AACtC,cAAc,oBAAoB,CAAC;AACnC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,qBAAqB,CAAC;AACpC,cAAc,YAAY,CAAC;AAC3B,cAAc,uBAAuB,CAAC"}
@@ -0,0 +1,23 @@
1
+ import type { ImportMap } from './ImportMap';
2
+ /**
3
+ * Merge multiple import maps into one. Paths from later maps are layered
4
+ * over earlier ones; if the same path appears in multiple maps, the
5
+ * latest occurrence's alias info wins.
6
+ *
7
+ * The merge is a pure function: input maps are not mutated. The returned
8
+ * map is frozen.
9
+ *
10
+ * @param importMaps - The import maps to merge, in priority order.
11
+ * @return A frozen import map containing every entry from every input.
12
+ *
13
+ * @example
14
+ * ```ts
15
+ * import { addToImportMap, createImportMap, mergeImportMaps } from '@codama/fragments/rust';
16
+ *
17
+ * const a = addToImportMap(createImportMap(), ['borsh::BorshDeserialize']);
18
+ * const b = addToImportMap(createImportMap(), ['solana_program::pubkey::Pubkey']);
19
+ * const merged = mergeImportMaps([a, b]);
20
+ * ```
21
+ */
22
+ export declare function mergeImportMaps(importMaps: readonly ImportMap[]): ImportMap;
23
+ //# sourceMappingURL=mergeImportMaps.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mergeImportMaps.d.ts","sourceRoot":"","sources":["../../../src/rust/mergeImportMaps.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAG7C;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,eAAe,CAAC,UAAU,EAAE,SAAS,SAAS,EAAE,GAAG,SAAS,CAU3E"}
@@ -0,0 +1,20 @@
1
+ import type { ImportMap, ImportPath } from './ImportMap';
2
+ /**
3
+ * Drop one or more imported paths from an import map. Paths that aren't
4
+ * present are silently ignored. The input map is not mutated.
5
+ *
6
+ * @param importMap - The import map to trim.
7
+ * @param paths - The Rust paths to remove. May be a single string, an
8
+ * array, or a {@link Set}.
9
+ * @return A new frozen import map without those entries.
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * import { addToImportMap, createImportMap, removeFromImportMap } from '@codama/fragments/rust';
14
+ *
15
+ * let map = addToImportMap(createImportMap(), ['foo::A', 'foo::B']);
16
+ * map = removeFromImportMap(map, 'foo::A');
17
+ * ```
18
+ */
19
+ export declare function removeFromImportMap(importMap: ImportMap, paths: ImportPath | ReadonlySet<string> | readonly ImportPath[]): ImportMap;
20
+ //# sourceMappingURL=removeFromImportMap.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"removeFromImportMap.d.ts","sourceRoot":"","sources":["../../../src/rust/removeFromImportMap.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzD;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,mBAAmB,CAC/B,SAAS,EAAE,SAAS,EACpB,KAAK,EAAE,UAAU,GAAG,WAAW,CAAC,MAAM,CAAC,GAAG,SAAS,UAAU,EAAE,GAChE,SAAS,CAMX"}
@@ -0,0 +1,32 @@
1
+ import type { ImportMap } from './ImportMap';
2
+ /**
3
+ * Rewrite the symbolic prefixes of an import map's paths against a
4
+ * dependency map.
5
+ *
6
+ * Resolution is by prefix match on the `::` separator: an import
7
+ * `'generated::accounts::AccountNode'` against
8
+ * `{ generated: 'crate::generated' }` becomes
9
+ * `'crate::generated::accounts::AccountNode'`. Paths that don't match any
10
+ * prefix are kept unchanged. Aliases follow their path.
11
+ *
12
+ * Renderers typically pre-define a base set of symbolic modules
13
+ * (`generated → crate::generated`, `mplToolbox → mpl_toolbox`, etc.) and
14
+ * pass them through this function just before calling
15
+ * {@link importMapToString} or {@link getExternalDependencies}.
16
+ *
17
+ * @param importMap - The import map to resolve.
18
+ * @param dependencies - A record mapping symbolic prefixes to resolved
19
+ * Rust paths.
20
+ * @return A new frozen import map with all paths resolved.
21
+ *
22
+ * @example
23
+ * ```ts
24
+ * import { addToImportMap, createImportMap, resolveImportMap } from '@codama/fragments/rust';
25
+ *
26
+ * const map = addToImportMap(createImportMap(), 'generated::accounts::AccountNode');
27
+ * const resolved = resolveImportMap(map, { generated: 'crate::generated' });
28
+ * // resolved has 'crate::generated::accounts::AccountNode'
29
+ * ```
30
+ */
31
+ export declare function resolveImportMap(importMap: ImportMap, dependencies: Record<string, string>): ImportMap;
32
+ //# sourceMappingURL=resolveImportMap.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"resolveImportMap.d.ts","sourceRoot":"","sources":["../../../src/rust/resolveImportMap.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAc,MAAM,aAAa,CAAC;AAEzD;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAgB,gBAAgB,CAAC,SAAS,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,SAAS,CAatG"}