@optique/core 1.0.0-dev.1598 → 1.0.0-dev.1608

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.
@@ -0,0 +1,140 @@
1
+ const require_dependency = require('./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 bare `DependencySourceState`.
54
+ * Used as the base `extractSourceValue` for plain dependency sources.
55
+ */
56
+ function extractFromBareState(state) {
57
+ if (!require_dependency.isDependencySourceState(state)) return void 0;
58
+ return state.result;
59
+ }
60
+ /**
61
+ * Wraps an inner `extractSourceValue` to unwrap `[innerState]` first.
62
+ * Used by `optional()` and `withDefault()` which wrap state in a
63
+ * single-element array.
64
+ */
65
+ function unwrapArrayThenExtract(innerExtract) {
66
+ return (state) => {
67
+ if (Array.isArray(state) && state.length === 1) return innerExtract(state[0]);
68
+ return innerExtract(state);
69
+ };
70
+ }
71
+ /**
72
+ * Composes dependency metadata through a modifier wrapper.
73
+ *
74
+ * - `"optional"`: composes `extractSourceValue` with array unwrapping.
75
+ * - `"withDefault"`: adds `getMissingSourceValue` if the inner parser
76
+ * preserves a dependency source.
77
+ * - `"map"`: sets `transformsSourceValue` and clears
78
+ * `preservesSourceValue`.
79
+ *
80
+ * Returns `undefined` if `inner` is `undefined`.
81
+ *
82
+ * @param inner The inner parser's metadata.
83
+ * @param wrapperKind The type of modifier being applied.
84
+ * @param options Additional options for certain wrapper kinds.
85
+ * @returns Composed metadata, or `undefined`.
86
+ * @internal
87
+ * @since 1.0.0
88
+ */
89
+ function composeDependencyMetadata(inner, wrapperKind, options) {
90
+ if (inner === void 0) return void 0;
91
+ switch (wrapperKind) {
92
+ case "optional": {
93
+ if (inner.source != null && inner.source.extractSourceValue != null) return {
94
+ ...inner,
95
+ source: {
96
+ ...inner.source,
97
+ extractSourceValue: unwrapArrayThenExtract(inner.source.extractSourceValue)
98
+ }
99
+ };
100
+ return inner;
101
+ }
102
+ case "withDefault": {
103
+ const wrappedExtract = inner.source?.extractSourceValue != null ? unwrapArrayThenExtract(inner.source.extractSourceValue) : void 0;
104
+ if (inner.source != null && inner.source.preservesSourceValue) return {
105
+ ...inner,
106
+ source: {
107
+ ...inner.source,
108
+ ...wrappedExtract != null && { extractSourceValue: wrappedExtract },
109
+ getMissingSourceValue: options?.defaultValue
110
+ }
111
+ };
112
+ if (wrappedExtract != null && inner.source != null) return {
113
+ ...inner,
114
+ source: {
115
+ ...inner.source,
116
+ extractSourceValue: wrappedExtract
117
+ }
118
+ };
119
+ return inner;
120
+ }
121
+ case "map": {
122
+ const result = {
123
+ ...inner,
124
+ transform: { transformsSourceValue: true }
125
+ };
126
+ if (inner.source != null) return {
127
+ ...result,
128
+ source: {
129
+ ...inner.source,
130
+ preservesSourceValue: false
131
+ }
132
+ };
133
+ return result;
134
+ }
135
+ }
136
+ }
137
+
138
+ //#endregion
139
+ exports.composeDependencyMetadata = composeDependencyMetadata;
140
+ exports.extractDependencyMetadata = extractDependencyMetadata;
@@ -0,0 +1,108 @@
1
+ import { ValueParserResult } from "./valueparser.cjs";
2
+ import { Suggestion } from "./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
+ * Each wrapper composes this method to handle its state shape:
21
+ * - plain source: reads from `DependencySourceState`
22
+ * - `optional()` / `withDefault()`: unwraps `[innerState]` first
23
+ * - `map()`: reads the pre-transform value from inner state
24
+ *
25
+ * Returns the `ValueParserResult` (which may be successful with any
26
+ * value including `undefined`, or failed), or `undefined` if the state
27
+ * does not contain a source result at all (unpopulated / wrong shape).
28
+ */
29
+ readonly extractSourceValue: (state: unknown) => ValueParserResult<unknown> | undefined;
30
+ /**
31
+ * When present, provides a missing-source value (e.g., from a
32
+ * `withDefault()` wrapper). Called during the `fillMissingSourceDefaults`
33
+ * phase of the dependency runtime.
34
+ */
35
+ readonly getMissingSourceValue?: () => ValueParserResult<unknown> | Promise<ValueParserResult<unknown>>;
36
+ /**
37
+ * Whether the parser's output value is the actual dependency source value.
38
+ * `false` when a transform like `map()` has been applied.
39
+ */
40
+ readonly preservesSourceValue: boolean;
41
+ }
42
+ /**
43
+ * Metadata for a parser that depends on one or more dependency sources.
44
+ *
45
+ * @internal
46
+ * @since 1.0.0
47
+ */
48
+ interface DerivedDependencyCapability {
49
+ /** Discriminant tag. */
50
+ readonly kind: "derived";
51
+ /** The dependency source IDs this parser depends on. */
52
+ readonly dependencyIds: readonly symbol[];
53
+ /**
54
+ * Returns default values for each dependency, used when the
55
+ * corresponding sources are not provided.
56
+ */
57
+ readonly getDefaultDependencyValues?: () => readonly unknown[];
58
+ /**
59
+ * Replays a parse with the given raw input and resolved dependency values.
60
+ */
61
+ readonly replayParse: (rawInput: string, dependencyValues: readonly unknown[]) => ValueParserResult<unknown> | Promise<ValueParserResult<unknown>>;
62
+ /**
63
+ * Replays suggestions with the given prefix and resolved dependency values.
64
+ */
65
+ readonly replaySuggest?: (prefix: string, dependencyValues: readonly unknown[]) => Iterable<Suggestion> | AsyncIterable<Suggestion>;
66
+ }
67
+ /**
68
+ * Metadata indicating that a wrapper transforms the dependency source value.
69
+ *
70
+ * @internal
71
+ * @since 1.0.0
72
+ */
73
+ interface DependencyTransformCapability {
74
+ /** Whether the wrapper transforms the source value. */
75
+ readonly transformsSourceValue: boolean;
76
+ }
77
+ /**
78
+ * Composed dependency metadata for a parser node.
79
+ *
80
+ * A parser may have any combination of source, derived, and transform
81
+ * capabilities. In practice, a parser is typically either a source or
82
+ * derived, never both.
83
+ *
84
+ * @internal
85
+ * @since 1.0.0
86
+ */
87
+ interface ParserDependencyMetadata {
88
+ /** Present if the parser is (or wraps) a dependency source. */
89
+ readonly source?: DependencySourceCapability;
90
+ /** Present if the parser depends on one or more sources. */
91
+ readonly derived?: DerivedDependencyCapability;
92
+ /** Present if a transform has been applied. */
93
+ readonly transform?: DependencyTransformCapability;
94
+ }
95
+ /**
96
+ * Extracts {@link ParserDependencyMetadata} from a value parser by reading
97
+ * old-protocol markers (`dependencySourceMarker`, `derivedValueParserMarker`,
98
+ * etc.).
99
+ *
100
+ * Returns `undefined` if the parser has no dependency-related markers.
101
+ *
102
+ * @param valueParser The value parser to inspect.
103
+ * @returns Metadata, or `undefined` for plain parsers.
104
+ * @internal
105
+ * @since 1.0.0
106
+ */
107
+ //#endregion
108
+ export { ParserDependencyMetadata };
@@ -0,0 +1,108 @@
1
+ import { ValueParserResult } from "./valueparser.js";
2
+ import { Suggestion } from "./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
+ * Each wrapper composes this method to handle its state shape:
21
+ * - plain source: reads from `DependencySourceState`
22
+ * - `optional()` / `withDefault()`: unwraps `[innerState]` first
23
+ * - `map()`: reads the pre-transform value from inner state
24
+ *
25
+ * Returns the `ValueParserResult` (which may be successful with any
26
+ * value including `undefined`, or failed), or `undefined` if the state
27
+ * does not contain a source result at all (unpopulated / wrong shape).
28
+ */
29
+ readonly extractSourceValue: (state: unknown) => ValueParserResult<unknown> | undefined;
30
+ /**
31
+ * When present, provides a missing-source value (e.g., from a
32
+ * `withDefault()` wrapper). Called during the `fillMissingSourceDefaults`
33
+ * phase of the dependency runtime.
34
+ */
35
+ readonly getMissingSourceValue?: () => ValueParserResult<unknown> | Promise<ValueParserResult<unknown>>;
36
+ /**
37
+ * Whether the parser's output value is the actual dependency source value.
38
+ * `false` when a transform like `map()` has been applied.
39
+ */
40
+ readonly preservesSourceValue: boolean;
41
+ }
42
+ /**
43
+ * Metadata for a parser that depends on one or more dependency sources.
44
+ *
45
+ * @internal
46
+ * @since 1.0.0
47
+ */
48
+ interface DerivedDependencyCapability {
49
+ /** Discriminant tag. */
50
+ readonly kind: "derived";
51
+ /** The dependency source IDs this parser depends on. */
52
+ readonly dependencyIds: readonly symbol[];
53
+ /**
54
+ * Returns default values for each dependency, used when the
55
+ * corresponding sources are not provided.
56
+ */
57
+ readonly getDefaultDependencyValues?: () => readonly unknown[];
58
+ /**
59
+ * Replays a parse with the given raw input and resolved dependency values.
60
+ */
61
+ readonly replayParse: (rawInput: string, dependencyValues: readonly unknown[]) => ValueParserResult<unknown> | Promise<ValueParserResult<unknown>>;
62
+ /**
63
+ * Replays suggestions with the given prefix and resolved dependency values.
64
+ */
65
+ readonly replaySuggest?: (prefix: string, dependencyValues: readonly unknown[]) => Iterable<Suggestion> | AsyncIterable<Suggestion>;
66
+ }
67
+ /**
68
+ * Metadata indicating that a wrapper transforms the dependency source value.
69
+ *
70
+ * @internal
71
+ * @since 1.0.0
72
+ */
73
+ interface DependencyTransformCapability {
74
+ /** Whether the wrapper transforms the source value. */
75
+ readonly transformsSourceValue: boolean;
76
+ }
77
+ /**
78
+ * Composed dependency metadata for a parser node.
79
+ *
80
+ * A parser may have any combination of source, derived, and transform
81
+ * capabilities. In practice, a parser is typically either a source or
82
+ * derived, never both.
83
+ *
84
+ * @internal
85
+ * @since 1.0.0
86
+ */
87
+ interface ParserDependencyMetadata {
88
+ /** Present if the parser is (or wraps) a dependency source. */
89
+ readonly source?: DependencySourceCapability;
90
+ /** Present if the parser depends on one or more sources. */
91
+ readonly derived?: DerivedDependencyCapability;
92
+ /** Present if a transform has been applied. */
93
+ readonly transform?: DependencyTransformCapability;
94
+ }
95
+ /**
96
+ * Extracts {@link ParserDependencyMetadata} from a value parser by reading
97
+ * old-protocol markers (`dependencySourceMarker`, `derivedValueParserMarker`,
98
+ * etc.).
99
+ *
100
+ * Returns `undefined` if the parser has no dependency-related markers.
101
+ *
102
+ * @param valueParser The value parser to inspect.
103
+ * @returns Metadata, or `undefined` for plain parsers.
104
+ * @internal
105
+ * @since 1.0.0
106
+ */
107
+ //#endregion
108
+ export { ParserDependencyMetadata };
@@ -0,0 +1,139 @@
1
+ import { defaultValues, dependencyId, dependencyIds, isDependencySource, isDependencySourceState, isDerivedValueParser, parseWithDependency, singleDefaultValue, suggestWithDependency } from "./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 bare `DependencySourceState`.
54
+ * Used as the base `extractSourceValue` for plain dependency sources.
55
+ */
56
+ function extractFromBareState(state) {
57
+ if (!isDependencySourceState(state)) return void 0;
58
+ return state.result;
59
+ }
60
+ /**
61
+ * Wraps an inner `extractSourceValue` to unwrap `[innerState]` first.
62
+ * Used by `optional()` and `withDefault()` which wrap state in a
63
+ * single-element array.
64
+ */
65
+ function unwrapArrayThenExtract(innerExtract) {
66
+ return (state) => {
67
+ if (Array.isArray(state) && state.length === 1) return innerExtract(state[0]);
68
+ return innerExtract(state);
69
+ };
70
+ }
71
+ /**
72
+ * Composes dependency metadata through a modifier wrapper.
73
+ *
74
+ * - `"optional"`: composes `extractSourceValue` with array unwrapping.
75
+ * - `"withDefault"`: adds `getMissingSourceValue` if the inner parser
76
+ * preserves a dependency source.
77
+ * - `"map"`: sets `transformsSourceValue` and clears
78
+ * `preservesSourceValue`.
79
+ *
80
+ * Returns `undefined` if `inner` is `undefined`.
81
+ *
82
+ * @param inner The inner parser's metadata.
83
+ * @param wrapperKind The type of modifier being applied.
84
+ * @param options Additional options for certain wrapper kinds.
85
+ * @returns Composed metadata, or `undefined`.
86
+ * @internal
87
+ * @since 1.0.0
88
+ */
89
+ function composeDependencyMetadata(inner, wrapperKind, options) {
90
+ if (inner === void 0) return void 0;
91
+ switch (wrapperKind) {
92
+ case "optional": {
93
+ if (inner.source != null && inner.source.extractSourceValue != null) return {
94
+ ...inner,
95
+ source: {
96
+ ...inner.source,
97
+ extractSourceValue: unwrapArrayThenExtract(inner.source.extractSourceValue)
98
+ }
99
+ };
100
+ return inner;
101
+ }
102
+ case "withDefault": {
103
+ const wrappedExtract = inner.source?.extractSourceValue != null ? unwrapArrayThenExtract(inner.source.extractSourceValue) : void 0;
104
+ if (inner.source != null && inner.source.preservesSourceValue) return {
105
+ ...inner,
106
+ source: {
107
+ ...inner.source,
108
+ ...wrappedExtract != null && { extractSourceValue: wrappedExtract },
109
+ getMissingSourceValue: options?.defaultValue
110
+ }
111
+ };
112
+ if (wrappedExtract != null && inner.source != null) return {
113
+ ...inner,
114
+ source: {
115
+ ...inner.source,
116
+ extractSourceValue: wrappedExtract
117
+ }
118
+ };
119
+ return inner;
120
+ }
121
+ case "map": {
122
+ const result = {
123
+ ...inner,
124
+ transform: { transformsSourceValue: true }
125
+ };
126
+ if (inner.source != null) return {
127
+ ...result,
128
+ source: {
129
+ ...inner.source,
130
+ preservesSourceValue: false
131
+ }
132
+ };
133
+ return result;
134
+ }
135
+ }
136
+ }
137
+
138
+ //#endregion
139
+ export { composeDependencyMetadata, extractDependencyMetadata };