@optique/core 1.0.0-dev.908 → 1.0.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 (109) hide show
  1. package/dist/annotation-state.cjs +425 -0
  2. package/dist/annotation-state.d.cts +24 -0
  3. package/dist/annotation-state.d.ts +24 -0
  4. package/dist/annotation-state.js +414 -0
  5. package/dist/annotations.cjs +2 -248
  6. package/dist/annotations.d.cts +2 -137
  7. package/dist/annotations.d.ts +2 -137
  8. package/dist/annotations.js +2 -238
  9. package/dist/completion.cjs +611 -100
  10. package/dist/completion.d.cts +1 -1
  11. package/dist/completion.d.ts +1 -1
  12. package/dist/completion.js +611 -100
  13. package/dist/constructs.cjs +3338 -827
  14. package/dist/constructs.d.cts +48 -7
  15. package/dist/constructs.d.ts +48 -7
  16. package/dist/constructs.js +3338 -827
  17. package/dist/context.cjs +0 -23
  18. package/dist/context.d.cts +119 -53
  19. package/dist/context.d.ts +119 -53
  20. package/dist/context.js +0 -22
  21. package/dist/dependency-metadata.cjs +139 -0
  22. package/dist/dependency-metadata.d.cts +112 -0
  23. package/dist/dependency-metadata.d.ts +112 -0
  24. package/dist/dependency-metadata.js +138 -0
  25. package/dist/dependency-runtime.cjs +698 -0
  26. package/dist/dependency-runtime.d.cts +149 -0
  27. package/dist/dependency-runtime.d.ts +149 -0
  28. package/dist/dependency-runtime.js +687 -0
  29. package/dist/dependency.cjs +7 -928
  30. package/dist/dependency.d.cts +2 -794
  31. package/dist/dependency.d.ts +2 -794
  32. package/dist/dependency.js +2 -899
  33. package/dist/displaywidth.cjs +44 -0
  34. package/dist/displaywidth.js +43 -0
  35. package/dist/doc.cjs +285 -23
  36. package/dist/doc.d.cts +57 -2
  37. package/dist/doc.d.ts +57 -2
  38. package/dist/doc.js +283 -25
  39. package/dist/execution-context.cjs +56 -0
  40. package/dist/execution-context.js +53 -0
  41. package/dist/extension.cjs +87 -0
  42. package/dist/extension.d.cts +97 -0
  43. package/dist/extension.d.ts +97 -0
  44. package/dist/extension.js +76 -0
  45. package/dist/facade.cjs +718 -523
  46. package/dist/facade.d.cts +87 -18
  47. package/dist/facade.d.ts +87 -18
  48. package/dist/facade.js +718 -523
  49. package/dist/index.cjs +14 -29
  50. package/dist/index.d.cts +10 -10
  51. package/dist/index.d.ts +10 -10
  52. package/dist/index.js +7 -7
  53. package/dist/input-trace.cjs +56 -0
  54. package/dist/input-trace.d.cts +77 -0
  55. package/dist/input-trace.d.ts +77 -0
  56. package/dist/input-trace.js +55 -0
  57. package/dist/internal/annotations.cjs +316 -0
  58. package/dist/internal/annotations.d.cts +140 -0
  59. package/dist/internal/annotations.d.ts +140 -0
  60. package/dist/internal/annotations.js +306 -0
  61. package/dist/internal/dependency.cjs +984 -0
  62. package/dist/internal/dependency.d.cts +539 -0
  63. package/dist/internal/dependency.d.ts +539 -0
  64. package/dist/internal/dependency.js +964 -0
  65. package/dist/{mode-dispatch.cjs → internal/mode-dispatch.cjs} +1 -3
  66. package/dist/{mode-dispatch.d.cts → internal/mode-dispatch.d.cts} +3 -7
  67. package/dist/{mode-dispatch.d.ts → internal/mode-dispatch.d.ts} +3 -7
  68. package/dist/{mode-dispatch.js → internal/mode-dispatch.js} +1 -3
  69. package/dist/internal/parser.cjs +728 -0
  70. package/dist/internal/parser.d.cts +947 -0
  71. package/dist/internal/parser.d.ts +947 -0
  72. package/dist/internal/parser.js +711 -0
  73. package/dist/message.cjs +84 -26
  74. package/dist/message.d.cts +49 -9
  75. package/dist/message.d.ts +49 -9
  76. package/dist/message.js +84 -27
  77. package/dist/modifiers.cjs +1023 -240
  78. package/dist/modifiers.d.cts +42 -1
  79. package/dist/modifiers.d.ts +42 -1
  80. package/dist/modifiers.js +1023 -240
  81. package/dist/parser.cjs +11 -463
  82. package/dist/parser.d.cts +3 -537
  83. package/dist/parser.d.ts +3 -537
  84. package/dist/parser.js +2 -433
  85. package/dist/phase2-seed.cjs +59 -0
  86. package/dist/phase2-seed.js +56 -0
  87. package/dist/primitives.cjs +557 -208
  88. package/dist/primitives.d.cts +10 -14
  89. package/dist/primitives.d.ts +10 -14
  90. package/dist/primitives.js +557 -208
  91. package/dist/program.cjs +5 -1
  92. package/dist/program.d.cts +5 -3
  93. package/dist/program.d.ts +5 -3
  94. package/dist/program.js +6 -1
  95. package/dist/suggestion.cjs +22 -8
  96. package/dist/suggestion.js +22 -8
  97. package/dist/usage-internals.cjs +3 -2
  98. package/dist/usage-internals.js +4 -2
  99. package/dist/usage.cjs +195 -40
  100. package/dist/usage.d.cts +92 -11
  101. package/dist/usage.d.ts +92 -11
  102. package/dist/usage.js +194 -41
  103. package/dist/validate.cjs +170 -0
  104. package/dist/validate.js +164 -0
  105. package/dist/valueparser.cjs +1278 -191
  106. package/dist/valueparser.d.cts +330 -20
  107. package/dist/valueparser.d.ts +330 -20
  108. package/dist/valueparser.js +1277 -192
  109. package/package.json +9 -9
@@ -0,0 +1,139 @@
1
+ const require_dependency = require('./internal/dependency.cjs');
2
+
3
+ //#region src/dependency-metadata.ts
4
+ /**
5
+ * Extracts {@link ParserDependencyMetadata} from a value parser by reading
6
+ * old-protocol markers (`dependencySourceMarker`, `derivedValueParserMarker`,
7
+ * etc.).
8
+ *
9
+ * Returns `undefined` if the parser has no dependency-related markers.
10
+ *
11
+ * @param valueParser The value parser to inspect.
12
+ * @returns Metadata, or `undefined` for plain parsers.
13
+ * @internal
14
+ * @since 1.0.0
15
+ */
16
+ function extractDependencyMetadata(valueParser) {
17
+ if (require_dependency.isDependencySource(valueParser)) return { source: {
18
+ kind: "source",
19
+ sourceId: valueParser[require_dependency.dependencyId],
20
+ extractSourceValue: extractFromBareState,
21
+ preservesSourceValue: true
22
+ } };
23
+ if (require_dependency.isDerivedValueParser(valueParser)) {
24
+ const isMultiSource = require_dependency.dependencyIds in valueParser && valueParser[require_dependency.dependencyIds] != null;
25
+ const allIds = isMultiSource ? valueParser[require_dependency.dependencyIds] : [valueParser[require_dependency.dependencyId]];
26
+ let defaultValuesFn;
27
+ if (require_dependency.defaultValues in valueParser && valueParser[require_dependency.defaultValues] != null) defaultValuesFn = valueParser[require_dependency.defaultValues];
28
+ else if (require_dependency.singleDefaultValue in valueParser && valueParser[require_dependency.singleDefaultValue] != null) {
29
+ const singleFn = valueParser[require_dependency.singleDefaultValue];
30
+ defaultValuesFn = () => [singleFn()];
31
+ }
32
+ const parser = valueParser;
33
+ const replayParse = (rawInput, depValues) => {
34
+ const depArg = isMultiSource ? depValues : depValues[0];
35
+ return parser[require_dependency.parseWithDependency](rawInput, depArg);
36
+ };
37
+ const suggestFn = require_dependency.suggestWithDependency in parser ? parser[require_dependency.suggestWithDependency] : void 0;
38
+ const replaySuggest = suggestFn != null ? (prefix, depValues) => {
39
+ const depArg = isMultiSource ? depValues : depValues[0];
40
+ return suggestFn(prefix, depArg);
41
+ } : void 0;
42
+ return { derived: {
43
+ kind: "derived",
44
+ dependencyIds: allIds,
45
+ getDefaultDependencyValues: defaultValuesFn,
46
+ replayParse,
47
+ replaySuggest
48
+ } };
49
+ }
50
+ return void 0;
51
+ }
52
+ /**
53
+ * Extracts the source parse result from a plain source state.
54
+ *
55
+ * Accepts either a `DependencySourceState` or a bare
56
+ * `ValueParserResult`-shaped object, and returns `undefined` for
57
+ * unrelated states. Used as the base `extractSourceValue` for plain
58
+ * dependency sources.
59
+ */
60
+ function extractFromBareState(state) {
61
+ if (require_dependency.isDependencySourceState(state)) return state.result;
62
+ if (state != null && typeof state === "object" && Object.hasOwn(state, "success") && typeof state.success === "boolean" && (state.success && Object.hasOwn(state, "value") || !state.success && Object.hasOwn(state, "error"))) return state;
63
+ return void 0;
64
+ }
65
+ /**
66
+ * Wraps an inner `extractSourceValue` to unwrap `[innerState]` first.
67
+ * Used by `optional()` and `withDefault()` which wrap state in a
68
+ * single-element array.
69
+ */
70
+ function unwrapArrayThenExtract(innerExtract) {
71
+ return (state) => {
72
+ if (Array.isArray(state) && state.length === 1) return innerExtract(state[0]);
73
+ return innerExtract(state);
74
+ };
75
+ }
76
+ /**
77
+ * Composes dependency metadata through a modifier wrapper.
78
+ *
79
+ * - `"optional"`: composes `extractSourceValue` with array unwrapping.
80
+ * - `"withDefault"`: adds `getMissingSourceValue` if the inner parser
81
+ * preserves a dependency source.
82
+ * - `"map"`: sets `transformsSourceValue` and clears
83
+ * `preservesSourceValue`.
84
+ *
85
+ * Returns `undefined` if `inner` is `undefined`.
86
+ *
87
+ * @param inner The inner parser's metadata.
88
+ * @param wrapperKind The type of modifier being applied.
89
+ * @param options Additional options for certain wrapper kinds.
90
+ * @returns Composed metadata, or `undefined`.
91
+ * @internal
92
+ * @since 1.0.0
93
+ */
94
+ function composeDependencyMetadata(inner, wrapperKind, options) {
95
+ if (inner === void 0) return void 0;
96
+ switch (wrapperKind) {
97
+ case "optional": {
98
+ if (inner.source != null && inner.source.extractSourceValue != null) return {
99
+ ...inner,
100
+ source: {
101
+ ...inner.source,
102
+ extractSourceValue: unwrapArrayThenExtract(inner.source.extractSourceValue)
103
+ }
104
+ };
105
+ return inner;
106
+ }
107
+ case "withDefault": {
108
+ const wrappedExtract = inner.source?.extractSourceValue != null ? unwrapArrayThenExtract(inner.source.extractSourceValue) : void 0;
109
+ const preservesSourceValue = inner.source?.preservesSourceValue !== false;
110
+ if (inner.source != null) return {
111
+ ...inner,
112
+ source: {
113
+ ...inner.source,
114
+ ...wrappedExtract != null && { extractSourceValue: wrappedExtract },
115
+ ...preservesSourceValue && options?.defaultValue != null && { getMissingSourceValue: options.defaultValue }
116
+ }
117
+ };
118
+ return inner;
119
+ }
120
+ case "map": {
121
+ const result = {
122
+ ...inner,
123
+ transform: { transformsSourceValue: true }
124
+ };
125
+ if (inner.source != null) return {
126
+ ...result,
127
+ source: {
128
+ ...inner.source,
129
+ preservesSourceValue: false
130
+ }
131
+ };
132
+ return result;
133
+ }
134
+ }
135
+ }
136
+
137
+ //#endregion
138
+ exports.composeDependencyMetadata = composeDependencyMetadata;
139
+ exports.extractDependencyMetadata = extractDependencyMetadata;
@@ -0,0 +1,112 @@
1
+ import { ValueParserResult } from "./valueparser.cjs";
2
+ import { Suggestion } from "./internal/parser.cjs";
3
+
4
+ //#region src/dependency-metadata.d.ts
5
+
6
+ /**
7
+ * Metadata for a parser that is a dependency source.
8
+ *
9
+ * @internal
10
+ * @since 1.0.0
11
+ */
12
+ interface DependencySourceCapability {
13
+ /** Discriminant tag. */
14
+ readonly kind: "source";
15
+ /** The unique dependency source identifier. */
16
+ readonly sourceId: symbol;
17
+ /**
18
+ * Extracts the dependency source parse result from the parser's state.
19
+ *
20
+ * The `state` argument is the current parser state for the source-owning
21
+ * parser. Each wrapper composes this method to handle its own state shape:
22
+ * - plain source: reads from `DependencySourceState`
23
+ * - `optional()` / `withDefault()`: unwraps `[innerState]` first
24
+ * - `map()`: reads the pre-transform value from inner state
25
+ *
26
+ * Returns the `ValueParserResult` (which may be successful with any
27
+ * value including `undefined`, or failed), a promise that resolves to the
28
+ * same shape when extraction needs async work, or `undefined` if the state
29
+ * does not contain a source result at all (unpopulated / wrong shape).
30
+ * Callers and wrapper authors must handle both direct and promise-wrapped
31
+ * results when composing `extractSourceValue`.
32
+ */
33
+ readonly extractSourceValue: (state: unknown) => ValueParserResult<unknown> | Promise<ValueParserResult<unknown> | undefined> | undefined;
34
+ /**
35
+ * When present, provides a missing-source value (e.g., from a
36
+ * `withDefault()` wrapper). Called during the `fillMissingSourceDefaults`
37
+ * phase of the dependency runtime.
38
+ */
39
+ readonly getMissingSourceValue?: () => ValueParserResult<unknown> | Promise<ValueParserResult<unknown>>;
40
+ /**
41
+ * Whether the parser's output value is the actual dependency source value.
42
+ * `false` when a transform like `map()` has been applied.
43
+ */
44
+ readonly preservesSourceValue: boolean;
45
+ }
46
+ /**
47
+ * Metadata for a parser that depends on one or more dependency sources.
48
+ *
49
+ * @internal
50
+ * @since 1.0.0
51
+ */
52
+ interface DerivedDependencyCapability {
53
+ /** Discriminant tag. */
54
+ readonly kind: "derived";
55
+ /** The dependency source IDs this parser depends on. */
56
+ readonly dependencyIds: readonly symbol[];
57
+ /**
58
+ * Returns default values for each dependency, used when the
59
+ * corresponding sources are not provided.
60
+ */
61
+ readonly getDefaultDependencyValues?: () => readonly unknown[];
62
+ /**
63
+ * Replays a parse with the given raw input and resolved dependency values.
64
+ */
65
+ readonly replayParse: (rawInput: string, dependencyValues: readonly unknown[]) => ValueParserResult<unknown> | Promise<ValueParserResult<unknown>>;
66
+ /**
67
+ * Replays suggestions with the given prefix and resolved dependency values.
68
+ */
69
+ readonly replaySuggest?: (prefix: string, dependencyValues: readonly unknown[]) => Iterable<Suggestion> | AsyncIterable<Suggestion>;
70
+ }
71
+ /**
72
+ * Metadata indicating that a wrapper transforms the dependency source value.
73
+ *
74
+ * @internal
75
+ * @since 1.0.0
76
+ */
77
+ interface DependencyTransformCapability {
78
+ /** Whether the wrapper transforms the source value. */
79
+ readonly transformsSourceValue: boolean;
80
+ }
81
+ /**
82
+ * Composed dependency metadata for a parser node.
83
+ *
84
+ * A parser may have any combination of source, derived, and transform
85
+ * capabilities. In practice, a parser is typically either a source or
86
+ * derived, never both.
87
+ *
88
+ * @internal
89
+ * @since 1.0.0
90
+ */
91
+ interface ParserDependencyMetadata {
92
+ /** Present if the parser is (or wraps) a dependency source. */
93
+ readonly source?: DependencySourceCapability;
94
+ /** Present if the parser depends on one or more sources. */
95
+ readonly derived?: DerivedDependencyCapability;
96
+ /** Present if a transform has been applied. */
97
+ readonly transform?: DependencyTransformCapability;
98
+ }
99
+ /**
100
+ * Extracts {@link ParserDependencyMetadata} from a value parser by reading
101
+ * old-protocol markers (`dependencySourceMarker`, `derivedValueParserMarker`,
102
+ * etc.).
103
+ *
104
+ * Returns `undefined` if the parser has no dependency-related markers.
105
+ *
106
+ * @param valueParser The value parser to inspect.
107
+ * @returns Metadata, or `undefined` for plain parsers.
108
+ * @internal
109
+ * @since 1.0.0
110
+ */
111
+ //#endregion
112
+ export { ParserDependencyMetadata };
@@ -0,0 +1,112 @@
1
+ import { ValueParserResult } from "./valueparser.js";
2
+ import { Suggestion } from "./internal/parser.js";
3
+
4
+ //#region src/dependency-metadata.d.ts
5
+
6
+ /**
7
+ * Metadata for a parser that is a dependency source.
8
+ *
9
+ * @internal
10
+ * @since 1.0.0
11
+ */
12
+ interface DependencySourceCapability {
13
+ /** Discriminant tag. */
14
+ readonly kind: "source";
15
+ /** The unique dependency source identifier. */
16
+ readonly sourceId: symbol;
17
+ /**
18
+ * Extracts the dependency source parse result from the parser's state.
19
+ *
20
+ * The `state` argument is the current parser state for the source-owning
21
+ * parser. Each wrapper composes this method to handle its own state shape:
22
+ * - plain source: reads from `DependencySourceState`
23
+ * - `optional()` / `withDefault()`: unwraps `[innerState]` first
24
+ * - `map()`: reads the pre-transform value from inner state
25
+ *
26
+ * Returns the `ValueParserResult` (which may be successful with any
27
+ * value including `undefined`, or failed), a promise that resolves to the
28
+ * same shape when extraction needs async work, or `undefined` if the state
29
+ * does not contain a source result at all (unpopulated / wrong shape).
30
+ * Callers and wrapper authors must handle both direct and promise-wrapped
31
+ * results when composing `extractSourceValue`.
32
+ */
33
+ readonly extractSourceValue: (state: unknown) => ValueParserResult<unknown> | Promise<ValueParserResult<unknown> | undefined> | undefined;
34
+ /**
35
+ * When present, provides a missing-source value (e.g., from a
36
+ * `withDefault()` wrapper). Called during the `fillMissingSourceDefaults`
37
+ * phase of the dependency runtime.
38
+ */
39
+ readonly getMissingSourceValue?: () => ValueParserResult<unknown> | Promise<ValueParserResult<unknown>>;
40
+ /**
41
+ * Whether the parser's output value is the actual dependency source value.
42
+ * `false` when a transform like `map()` has been applied.
43
+ */
44
+ readonly preservesSourceValue: boolean;
45
+ }
46
+ /**
47
+ * Metadata for a parser that depends on one or more dependency sources.
48
+ *
49
+ * @internal
50
+ * @since 1.0.0
51
+ */
52
+ interface DerivedDependencyCapability {
53
+ /** Discriminant tag. */
54
+ readonly kind: "derived";
55
+ /** The dependency source IDs this parser depends on. */
56
+ readonly dependencyIds: readonly symbol[];
57
+ /**
58
+ * Returns default values for each dependency, used when the
59
+ * corresponding sources are not provided.
60
+ */
61
+ readonly getDefaultDependencyValues?: () => readonly unknown[];
62
+ /**
63
+ * Replays a parse with the given raw input and resolved dependency values.
64
+ */
65
+ readonly replayParse: (rawInput: string, dependencyValues: readonly unknown[]) => ValueParserResult<unknown> | Promise<ValueParserResult<unknown>>;
66
+ /**
67
+ * Replays suggestions with the given prefix and resolved dependency values.
68
+ */
69
+ readonly replaySuggest?: (prefix: string, dependencyValues: readonly unknown[]) => Iterable<Suggestion> | AsyncIterable<Suggestion>;
70
+ }
71
+ /**
72
+ * Metadata indicating that a wrapper transforms the dependency source value.
73
+ *
74
+ * @internal
75
+ * @since 1.0.0
76
+ */
77
+ interface DependencyTransformCapability {
78
+ /** Whether the wrapper transforms the source value. */
79
+ readonly transformsSourceValue: boolean;
80
+ }
81
+ /**
82
+ * Composed dependency metadata for a parser node.
83
+ *
84
+ * A parser may have any combination of source, derived, and transform
85
+ * capabilities. In practice, a parser is typically either a source or
86
+ * derived, never both.
87
+ *
88
+ * @internal
89
+ * @since 1.0.0
90
+ */
91
+ interface ParserDependencyMetadata {
92
+ /** Present if the parser is (or wraps) a dependency source. */
93
+ readonly source?: DependencySourceCapability;
94
+ /** Present if the parser depends on one or more sources. */
95
+ readonly derived?: DerivedDependencyCapability;
96
+ /** Present if a transform has been applied. */
97
+ readonly transform?: DependencyTransformCapability;
98
+ }
99
+ /**
100
+ * Extracts {@link ParserDependencyMetadata} from a value parser by reading
101
+ * old-protocol markers (`dependencySourceMarker`, `derivedValueParserMarker`,
102
+ * etc.).
103
+ *
104
+ * Returns `undefined` if the parser has no dependency-related markers.
105
+ *
106
+ * @param valueParser The value parser to inspect.
107
+ * @returns Metadata, or `undefined` for plain parsers.
108
+ * @internal
109
+ * @since 1.0.0
110
+ */
111
+ //#endregion
112
+ export { ParserDependencyMetadata };
@@ -0,0 +1,138 @@
1
+ import { defaultValues, dependencyId, dependencyIds, isDependencySource, isDependencySourceState, isDerivedValueParser, parseWithDependency, singleDefaultValue, suggestWithDependency } from "./internal/dependency.js";
2
+
3
+ //#region src/dependency-metadata.ts
4
+ /**
5
+ * Extracts {@link ParserDependencyMetadata} from a value parser by reading
6
+ * old-protocol markers (`dependencySourceMarker`, `derivedValueParserMarker`,
7
+ * etc.).
8
+ *
9
+ * Returns `undefined` if the parser has no dependency-related markers.
10
+ *
11
+ * @param valueParser The value parser to inspect.
12
+ * @returns Metadata, or `undefined` for plain parsers.
13
+ * @internal
14
+ * @since 1.0.0
15
+ */
16
+ function extractDependencyMetadata(valueParser) {
17
+ if (isDependencySource(valueParser)) return { source: {
18
+ kind: "source",
19
+ sourceId: valueParser[dependencyId],
20
+ extractSourceValue: extractFromBareState,
21
+ preservesSourceValue: true
22
+ } };
23
+ if (isDerivedValueParser(valueParser)) {
24
+ const isMultiSource = dependencyIds in valueParser && valueParser[dependencyIds] != null;
25
+ const allIds = isMultiSource ? valueParser[dependencyIds] : [valueParser[dependencyId]];
26
+ let defaultValuesFn;
27
+ if (defaultValues in valueParser && valueParser[defaultValues] != null) defaultValuesFn = valueParser[defaultValues];
28
+ else if (singleDefaultValue in valueParser && valueParser[singleDefaultValue] != null) {
29
+ const singleFn = valueParser[singleDefaultValue];
30
+ defaultValuesFn = () => [singleFn()];
31
+ }
32
+ const parser = valueParser;
33
+ const replayParse = (rawInput, depValues) => {
34
+ const depArg = isMultiSource ? depValues : depValues[0];
35
+ return parser[parseWithDependency](rawInput, depArg);
36
+ };
37
+ const suggestFn = suggestWithDependency in parser ? parser[suggestWithDependency] : void 0;
38
+ const replaySuggest = suggestFn != null ? (prefix, depValues) => {
39
+ const depArg = isMultiSource ? depValues : depValues[0];
40
+ return suggestFn(prefix, depArg);
41
+ } : void 0;
42
+ return { derived: {
43
+ kind: "derived",
44
+ dependencyIds: allIds,
45
+ getDefaultDependencyValues: defaultValuesFn,
46
+ replayParse,
47
+ replaySuggest
48
+ } };
49
+ }
50
+ return void 0;
51
+ }
52
+ /**
53
+ * Extracts the source parse result from a plain source state.
54
+ *
55
+ * Accepts either a `DependencySourceState` or a bare
56
+ * `ValueParserResult`-shaped object, and returns `undefined` for
57
+ * unrelated states. Used as the base `extractSourceValue` for plain
58
+ * dependency sources.
59
+ */
60
+ function extractFromBareState(state) {
61
+ if (isDependencySourceState(state)) return state.result;
62
+ if (state != null && typeof state === "object" && Object.hasOwn(state, "success") && typeof state.success === "boolean" && (state.success && Object.hasOwn(state, "value") || !state.success && Object.hasOwn(state, "error"))) return state;
63
+ return void 0;
64
+ }
65
+ /**
66
+ * Wraps an inner `extractSourceValue` to unwrap `[innerState]` first.
67
+ * Used by `optional()` and `withDefault()` which wrap state in a
68
+ * single-element array.
69
+ */
70
+ function unwrapArrayThenExtract(innerExtract) {
71
+ return (state) => {
72
+ if (Array.isArray(state) && state.length === 1) return innerExtract(state[0]);
73
+ return innerExtract(state);
74
+ };
75
+ }
76
+ /**
77
+ * Composes dependency metadata through a modifier wrapper.
78
+ *
79
+ * - `"optional"`: composes `extractSourceValue` with array unwrapping.
80
+ * - `"withDefault"`: adds `getMissingSourceValue` if the inner parser
81
+ * preserves a dependency source.
82
+ * - `"map"`: sets `transformsSourceValue` and clears
83
+ * `preservesSourceValue`.
84
+ *
85
+ * Returns `undefined` if `inner` is `undefined`.
86
+ *
87
+ * @param inner The inner parser's metadata.
88
+ * @param wrapperKind The type of modifier being applied.
89
+ * @param options Additional options for certain wrapper kinds.
90
+ * @returns Composed metadata, or `undefined`.
91
+ * @internal
92
+ * @since 1.0.0
93
+ */
94
+ function composeDependencyMetadata(inner, wrapperKind, options) {
95
+ if (inner === void 0) return void 0;
96
+ switch (wrapperKind) {
97
+ case "optional": {
98
+ if (inner.source != null && inner.source.extractSourceValue != null) return {
99
+ ...inner,
100
+ source: {
101
+ ...inner.source,
102
+ extractSourceValue: unwrapArrayThenExtract(inner.source.extractSourceValue)
103
+ }
104
+ };
105
+ return inner;
106
+ }
107
+ case "withDefault": {
108
+ const wrappedExtract = inner.source?.extractSourceValue != null ? unwrapArrayThenExtract(inner.source.extractSourceValue) : void 0;
109
+ const preservesSourceValue = inner.source?.preservesSourceValue !== false;
110
+ if (inner.source != null) return {
111
+ ...inner,
112
+ source: {
113
+ ...inner.source,
114
+ ...wrappedExtract != null && { extractSourceValue: wrappedExtract },
115
+ ...preservesSourceValue && options?.defaultValue != null && { getMissingSourceValue: options.defaultValue }
116
+ }
117
+ };
118
+ return inner;
119
+ }
120
+ case "map": {
121
+ const result = {
122
+ ...inner,
123
+ transform: { transformsSourceValue: true }
124
+ };
125
+ if (inner.source != null) return {
126
+ ...result,
127
+ source: {
128
+ ...inner.source,
129
+ preservesSourceValue: false
130
+ }
131
+ };
132
+ return result;
133
+ }
134
+ }
135
+ }
136
+
137
+ //#endregion
138
+ export { composeDependencyMetadata, extractDependencyMetadata };