Skip to content

Commit bb24f85

Browse files
authored
Add XDR schema types
feat: implement XDR (External Data Representation Standard) schema interface
2 parents 97e3ca2 + 8d6682e commit bb24f85

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

src/xdr/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* XDR (External Data Representation Standard) module
3+
*
4+
* This module provides TypeScript type definitions for XDR schemas
5+
* based on RFC 4506 specification.
6+
*/
7+
8+
export * from './types';

src/xdr/types.ts

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/**
2+
* XDR (External Data Representation Standard) schema type definitions
3+
* based on RFC 4506 specification.
4+
* Specification: https://datatracker.ietf.org/doc/html/rfc4506
5+
*/
6+
export type XdrSchema = XdrPrimitiveSchema | XdrWidePrimitiveSchema | XdrCompositeSchema;
7+
8+
// Primitive type schemas
9+
10+
export type XdrPrimitiveSchema =
11+
| XdrVoidSchema
12+
| XdrIntSchema
13+
| XdrUnsignedIntSchema
14+
| XdrEnumSchema
15+
| XdrBooleanSchema
16+
| XdrHyperSchema
17+
| XdrUnsignedHyperSchema
18+
| XdrFloatSchema
19+
| XdrDoubleSchema
20+
| XdrQuadrupleSchema;
21+
22+
export type XdrVoidSchema = XdrBaseSchema<'void'>;
23+
export type XdrIntSchema = XdrBaseSchema<'int'>;
24+
export type XdrUnsignedIntSchema = XdrBaseSchema<'unsigned_int'>;
25+
export interface XdrEnumSchema extends XdrBaseSchema<'enum'> {
26+
values: Record<string, number>;
27+
}
28+
export type XdrBooleanSchema = XdrBaseSchema<'boolean'>;
29+
export type XdrHyperSchema = XdrBaseSchema<'hyper'>;
30+
export type XdrUnsignedHyperSchema = XdrBaseSchema<'unsigned_hyper'>;
31+
export type XdrFloatSchema = XdrBaseSchema<'float'>;
32+
export type XdrDoubleSchema = XdrBaseSchema<'double'>;
33+
export type XdrQuadrupleSchema = XdrBaseSchema<'quadruple'>;
34+
35+
// Wide primitive type schemas
36+
37+
export type XdrWidePrimitiveSchema = XdrOpaqueSchema | XdrVarlenOpaqueSchema | XdrStringSchema;
38+
39+
export interface XdrOpaqueSchema extends XdrBaseSchema<'opaque'> {
40+
size: number;
41+
}
42+
43+
export interface XdrVarlenOpaqueSchema extends XdrBaseSchema<'vopaque'> {
44+
size?: number;
45+
}
46+
47+
export interface XdrStringSchema extends XdrBaseSchema<'string'> {
48+
size?: number;
49+
}
50+
51+
// Composite type schemas
52+
53+
export type XdrCompositeSchema = XdrArraySchema | XdrVarlenArraySchema | XdrStructSchema | XdrUnionSchema;
54+
55+
export interface XdrArraySchema extends XdrBaseSchema<'array'> {
56+
/** Schema of array elements */
57+
elements: XdrSchema;
58+
/** Fixed number of elements */
59+
size: number;
60+
}
61+
62+
export interface XdrVarlenArraySchema extends XdrBaseSchema<'varray'> {
63+
/** Schema of array elements */
64+
elements: XdrSchema;
65+
/** Optional maximum length constraint */
66+
size?: number;
67+
}
68+
69+
/**
70+
* The components of the structure are encoded in the order of their
71+
* declaration in the structure. Each component's size is a multiple of
72+
* four bytes, though the components may be different sizes.
73+
*/
74+
export interface XdrStructSchema extends XdrBaseSchema<'struct'> {
75+
/** Array of field definitions */
76+
fields: [schema: XdrSchema, name: string][];
77+
}
78+
79+
/**
80+
* A discriminated union is a type composed of a discriminant followed
81+
* by a type selected from a set of prearranged types according to the
82+
* value of the discriminant. The type of discriminant is either "int",
83+
* "unsigned int", or an enumerated type, such as "bool". The component
84+
* types are called "arms" of the union and are preceded by the value of
85+
* the discriminant that implies their encoding.
86+
*/
87+
export interface XdrUnionSchema extends XdrBaseSchema<'union'> {
88+
type: 'union';
89+
arms: [discriminant: number | string | boolean, schema: XdrSchema][];
90+
default?: XdrSchema;
91+
}
92+
93+
// Base schema
94+
95+
export interface XdrBaseSchema<Type extends string> {
96+
/** The schema type */
97+
type: Type;
98+
}

0 commit comments

Comments
 (0)