Skip to content

Commit

Permalink
URI-encoded formatting options
Browse files Browse the repository at this point in the history
  • Loading branch information
surol committed Aug 9, 2023
1 parent 562d2c2 commit 7b4c17f
Show file tree
Hide file tree
Showing 13 changed files with 169 additions and 157 deletions.
4 changes: 2 additions & 2 deletions src/compiler/serialization/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ export * from './ucs-lib.js';
export * from './ucs-models.js';
export * from './ucs-process-bigint.js';
export * from './ucs-process-boolean.js';
export * from './ucs-process-charge.js';
export * from './ucs-process-defaults.js';
export * from './ucs-process-inset.js';
export * from './ucs-process-integer.js';
export * from './ucs-process-list.js';
export * from './ucs-process-map.js';
Expand All @@ -18,6 +18,6 @@ export * from './ucs-process-plain-text.js';
export * from './ucs-process-primitives.js';
export * from './ucs-process-string.js';
export * from './ucs-process-unknown.js';
export * from './ucs-process-uri-encoded.js';
export * from './ucs-process-uri-params.js';
export * from './ucs-support-uri-encoded.js';
export * from './ucs-writer.class.js';
10 changes: 10 additions & 0 deletions src/compiler/serialization/ucs-process-charge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { UccFeature } from '../bootstrap/ucc-feature.js';
import { UcsBootstrap } from './ucs-bootstrap.js';

export function ucsProcessCharge(boot: UcsBootstrap): UccFeature.Handle {
return {
constrain({ schema }) {
boot.formatWith('charge', schema);
},
};
}
15 changes: 0 additions & 15 deletions src/compiler/serialization/ucs-process-inset.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,52 +20,56 @@ import { ucsProcessNumber } from './ucs-process-number.js';
import { ucsProcessString } from './ucs-process-string.js';
import { UcsWriterClass } from './ucs-writer.class.js';

export function ucsSupportURIEncoded(): UccFeature<UcsBootstrap> {
return boot => {
boot
.enable(ucsProcessURIEncodedDefaults)
.onConstraint(
{
processor: 'serializer',
use: ucsProcessBigInt.name,
from: COMPILER_MODULE,
},
({ schema, constraint: { with: options } }) => {
const { number = 'parse' } = options as UcBigInt.Variant;
export function ucsProcessURIEncoded(boot: UcsBootstrap): UccFeature.Handle {
boot
.enable(ucsProcessURIEncodedDefaults)
.onConstraint(
{
processor: 'serializer',
use: ucsProcessBigInt.name,
from: COMPILER_MODULE,
},
({ schema, constraint: { with: options } }) => {
const { number = 'parse' } = options as UcBigInt.Variant;

boot.formatWith('uriEncoded', schema, ucsFormatURIEncoded(ucsFormatBigInt({ number })));
},
)
.onConstraint(
{
processor: 'serializer',
use: ucsProcessInteger.name,
from: COMPILER_MODULE,
},
({ schema }) => {
boot.formatWith('uriEncoded', schema, ucsFormatURIEncoded(ucsFormatInteger()));
},
)
.onConstraint(
{
processor: 'serializer',
use: ucsProcessNumber.name,
from: COMPILER_MODULE,
},
({ schema }) => {
boot.formatWith('uriEncoded', schema, ucsFormatURIEncoded(ucsFormatNumber()));
},
)
.onConstraint(
{
processor: 'serializer',
use: ucsProcessString.name,
from: COMPILER_MODULE,
},
({ schema }) => {
boot.formatWith('uriEncoded', schema, ucsFormatURIEncodedString());
},
);
boot.formatWith('uriEncoded', schema, ucsFormatURIEncoded(ucsFormatBigInt({ number })));
},
)
.onConstraint(
{
processor: 'serializer',
use: ucsProcessInteger.name,
from: COMPILER_MODULE,
},
({ schema }) => {
boot.formatWith('uriEncoded', schema, ucsFormatURIEncoded(ucsFormatInteger()));
},
)
.onConstraint(
{
processor: 'serializer',
use: ucsProcessNumber.name,
from: COMPILER_MODULE,
},
({ schema }) => {
boot.formatWith('uriEncoded', schema, ucsFormatURIEncoded(ucsFormatNumber()));
},
)
.onConstraint(
{
processor: 'serializer',
use: ucsProcessString.name,
from: COMPILER_MODULE,
},
({ schema }) => {
boot.formatWith('uriEncoded', schema, ucsFormatURIEncodedString());
},
);

return {
constrain({ schema }) {
boot.formatWith('uriEncoded', schema);
},
};
}

Expand Down
6 changes: 3 additions & 3 deletions src/syntax/formats/charge/uc-charge.lexer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
} from '../../uc-token.js';
import { UcURIParamsLexer } from '../uri-params/uc-uri-params.lexer.js';
import { UcChargeLexer } from './uc-charge.lexer.js';
import { ucInsetCharge } from './uc-inset-charge.js';
import { ucFormatCharge } from './uc-inset-charge.js';

describe('UcChargeLexer', () => {
let lexer: UcChargeLexer;
Expand Down Expand Up @@ -241,7 +241,7 @@ describe('ucInsetCharge', () => {
models: {
readValue: {
model: ucMap({
a: ucUnknown({ within: { uriParam: ucInsetCharge() } }),
a: ucUnknown({ within: { uriParam: ucFormatCharge() } }),
}),
lexer: ({ emit }) => {
const Lexer = UC_MODULE_CHURI.import(UcURIParamsLexer.name);
Expand Down Expand Up @@ -272,7 +272,7 @@ describe('ucInsetCharge', () => {
model: ucMap({
a: ucUnknown({
within: {
uriParam: ucInsetCharge({ plusAsSpace: true }),
uriParam: ucFormatCharge({ plusAsSpace: true }),
},
}),
}),
Expand Down
38 changes: 17 additions & 21 deletions src/syntax/formats/charge/uc-inset-charge.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,17 @@
import { UcdInsetOptions } from '../../../compiler/deserialization/ucd-process-inset.js';
import { UcsInsetOptions } from '../../../compiler/serialization/ucs-process-inset.js';
import { CHURI_MODULE, COMPILER_MODULE } from '../../../impl/module-names.js';
import { UcOmniConstraints } from '../../../schema/uc-constraints.js';

/**
* Enables processing of inset encoded with {@link UcChargeLexer URI Charge Notation}.
* Enables{@link UcChargeLexer URI Charge Notation} format for schema or inset.
*
* @param options - Lexer options.
* @param options - Formatting options.
*
* @returns Schema constraints.
*/
export function ucFormatCharge(options?: UcChargeOptions): UcOmniConstraints;

export function ucInsetCharge(options?: {
/**
* Whether to decode _plus sign_ (`"+" (U+002B)`) as {@link UC_TOKEN_PREFIX_SPACE space padding}.
*
* @defaultValue `false`
*/
readonly plusAsSpace?: boolean | undefined;
}): UcOmniConstraints;

export function ucInsetCharge({
plusAsSpace,
}: {
readonly plusAsSpace?: boolean | undefined;
} = {}): UcOmniConstraints {
export function ucFormatCharge({ plusAsSpace }: UcChargeOptions = {}): UcOmniConstraints {
return {
deserializer: {
use: 'ucdProcessInset',
Expand All @@ -36,11 +23,20 @@ export function ucInsetCharge({
} satisfies UcdInsetOptions,
},
serializer: {
use: 'ucsProcessInset',
use: 'ucsProcessCharge',
from: COMPILER_MODULE,
with: {
format: 'charge',
} satisfies UcsInsetOptions,
},
};
}

/**
* Options for {@link ucFormatCharge URI charge} formatting.
*/
export interface UcChargeOptions {
/**
* Whether to decode _plus sign_ (`"+" (U+002B)`) as {@link UC_TOKEN_PREFIX_SPACE space padding}.
*
* @defaultValue `false`
*/
readonly plusAsSpace?: boolean | undefined;
}
6 changes: 1 addition & 5 deletions src/syntax/formats/plain-text/uc-format-plain-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ import { UcOmniConstraints } from '../../../schema/uc-constraints.js';
*/
export function ucFormatPlainText(options?: UcPlainTextOptions): UcOmniConstraints;

export function ucFormatPlainText({
raw,
}: {
readonly raw?: boolean | undefined;
} = {}): UcOmniConstraints {
export function ucFormatPlainText({ raw }: UcPlainTextOptions = {}): UcOmniConstraints {
return {
deserializer: {
use: 'ucdProcessInset',
Expand Down
2 changes: 1 addition & 1 deletion src/syntax/formats/uri-encoded/mod.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './uc-inset-uri-encoded.js';
export * from './uc-format-uri-encoded.js';
export * from './uc-uri-encoded.lexer.js';
Original file line number Diff line number Diff line change
@@ -1,38 +1,20 @@
import { UcdInsetOptions } from '../../../compiler/deserialization/ucd-process-inset.js';
import { UcsInsetOptions } from '../../../compiler/serialization/ucs-process-inset.js';
import { CHURI_MODULE, COMPILER_MODULE } from '../../../impl/module-names.js';
import { UcOmniConstraints } from '../../../schema/uc-constraints.js';

/**
* Enables inset processing as {@link UcPlainTextLexer URI-encoded text}.
* Enables {@link UcPlainTextLexer URI-encoded text} format for schema or inset.
*
* @param options - Lexer options.
* @param options - Formatting options.
*
* @returns Schema constraints.
*/
export function ucFormatURIEncoded(options?: UcURIEncodedOptions): UcOmniConstraints;

export function ucInsetURIEncoded(options?: {
/**
* Whether to decode _plus sign_ (`"+" (U+002B)`) as {@link UC_TOKEN_PREFIX_SPACE space padding}.
*
* @defaultValue `false`
*/
readonly plusAsSpace?: boolean | undefined;
/**
* Whether to emit a raw string rather quoted string.
*
* @defaultValue `false`.
*/
readonly raw?: boolean | undefined;
}): UcOmniConstraints;

export function ucInsetURIEncoded({
export function ucFormatURIEncoded({
plusAsSpace,
raw,
}: {
readonly plusAsSpace?: boolean | undefined;
readonly raw?: boolean | undefined;
} = {}): UcOmniConstraints {
}: UcURIEncodedOptions = {}): UcOmniConstraints {
return {
deserializer: {
use: 'ucdProcessInset',
Expand All @@ -45,11 +27,26 @@ export function ucInsetURIEncoded({
} satisfies UcdInsetOptions,
},
serializer: {
use: 'ucsProcessInset',
use: 'ucsProcessURIEncoded',
from: COMPILER_MODULE,
with: {
format: 'uriEncoded',
} satisfies UcsInsetOptions,
},
};
}

/**
* Options for {@link ucFormatURIEncoded URI-encoded text} formatting.
*/
export interface UcURIEncodedOptions {
/**
* Whether to decode _plus sign_ (`"+" (U+002B)`) as {@link UC_TOKEN_PREFIX_SPACE space padding}.
*
* @defaultValue `false`
*/
readonly plusAsSpace?: boolean | undefined;
/**
* Whether to emit a raw string rather quoted string.
*
* @defaultValue `false`.
*/
readonly raw?: boolean | undefined;
}
10 changes: 5 additions & 5 deletions src/syntax/formats/uri-encoded/uc-uri-encoded.lexer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { readChunks } from '../../../spec/read-chunks.js';
import { scanUcTokens } from '../../scan-uc-tokens.js';
import { UC_TOKEN_APOSTROPHE, UcToken } from '../../uc-token.js';
import { UcURIParamsLexer } from '../uri-params/uc-uri-params.lexer.js';
import { ucInsetURIEncoded } from './uc-inset-uri-encoded.js';
import { ucFormatURIEncoded } from './uc-format-uri-encoded.js';
import { UcURIEncodedLexer } from './uc-uri-encoded.lexer.js';

describe('UcURIEncodedLexer', () => {
Expand Down Expand Up @@ -66,7 +66,7 @@ describe('ucInsetURIEncoded', () => {
models: {
readValue: {
model: ucMap({
a: ucUnknown({ within: { uriParam: ucInsetURIEncoded() } }),
a: ucUnknown({ within: { uriParam: ucFormatURIEncoded() } }),
}),
lexer: ({ emit }) => {
const Lexer = UC_MODULE_CHURI.import(UcURIParamsLexer.name);
Expand Down Expand Up @@ -95,7 +95,7 @@ describe('ucInsetURIEncoded', () => {
models: {
readValue: {
model: ucMap({
a: ucUnknown({ within: { uriParam: ucInsetURIEncoded({ raw: true }) } }),
a: ucUnknown({ within: { uriParam: ucFormatURIEncoded({ raw: true }) } }),
}),
lexer: ({ emit }) => {
const Lexer = UC_MODULE_CHURI.import(UcURIParamsLexer.name);
Expand Down Expand Up @@ -129,7 +129,7 @@ describe('ucInsetURIEncoded', () => {
models: {
readValue: {
model: ucMap({
a: ucUnknown({ within: { uriParam: ucInsetURIEncoded({ plusAsSpace: true }) } }),
a: ucUnknown({ within: { uriParam: ucFormatURIEncoded({ plusAsSpace: true }) } }),
}),
mode: 'async',
lexer: ({ emit }) => {
Expand Down Expand Up @@ -160,7 +160,7 @@ describe('ucInsetURIEncoded', () => {
readValue: {
model: ucMap({
a: ucUnknown({
within: { uriParam: ucInsetURIEncoded({ plusAsSpace: true, raw: true }) },
within: { uriParam: ucFormatURIEncoded({ plusAsSpace: true, raw: true }) },
}),
}),
mode: 'sync',
Expand Down
Loading

0 comments on commit 7b4c17f

Please sign in to comment.