@@ -9,12 +9,12 @@ import {
99 Bytes ,
1010 Enum ,
1111 Struct ,
12- _void ,
1312 createCodec ,
1413 createDecoder ,
1514 enhanceCodec ,
16- str as scaleStr ,
15+ str ,
1716 u8 ,
17+ _void ,
1818 type Codec ,
1919} from "scale-ts" ;
2020import {
@@ -123,49 +123,23 @@ export function TaggedUnion<O extends TaggedUnionCodecs>(
123123 return Enum ( inner ) as unknown as Codec < TaggedUnionValue < O > > ;
124124}
125125
126- /**
127- * Wire codec for Rust `CallError<D>`, projected to the public domain error `D`.
128- *
129- * Generated TypeScript APIs expose only the domain error union in
130- * `ResultAsync<Ok, D>`. The Rust host still wraps that value in
131- * `CallError::Domain` on the wire so framework errors can share the response
132- * channel. Encoding always emits `Domain`; decoding returns the inner domain
133- * value and throws for framework-level failures that have no public `D` shape.
134- */
135- export function CallError < D > ( domain : Codec < D > ) : Codec < D > {
136- type WireCallError =
137- | { tag : "Domain" ; value : D }
138- | { tag : "Denied" ; value ?: undefined }
139- | { tag : "Unsupported" ; value ?: undefined }
140- | { tag : "MalformedFrame" ; value : { reason : string } }
141- | { tag : "HostFailure" ; value : { reason : string } } ;
126+ /** Public TS value for Rust's derived `CallError<D>` enum. */
127+ export type CallErrorValue < D > =
128+ | { tag : "Domain" ; value : D }
129+ | { tag : "Denied" ; value ?: undefined }
130+ | { tag : "Unsupported" ; value ?: undefined }
131+ | { tag : "MalformedFrame" ; value : { reason : string } }
132+ | { tag : "HostFailure" ; value : { reason : string } } ;
142133
143- const wire = Enum ( {
134+ /** SCALE codec for Rust's derived `CallError<D>` enum. */
135+ export function CallError < D > ( domain : Codec < D > ) : Codec < CallErrorValue < D > > {
136+ return TaggedUnion ( {
144137 Domain : domain ,
145138 Denied : _void ,
146139 Unsupported : _void ,
147- MalformedFrame : Struct ( { reason : scaleStr } ) ,
148- HostFailure : Struct ( { reason : scaleStr } ) ,
149- } ) as unknown as Codec < WireCallError > ;
150-
151- return enhanceCodec (
152- wire ,
153- ( value : D ) : WireCallError => ( { tag : "Domain" , value } ) ,
154- ( value : WireCallError ) : D => {
155- switch ( value . tag ) {
156- case "Domain" :
157- return value . value ;
158- case "Denied" :
159- throw new Error ( "Host denied the request" ) ;
160- case "Unsupported" :
161- throw new Error ( "Host does not support this request" ) ;
162- case "MalformedFrame" :
163- throw new Error ( `Malformed request frame: ${ value . value . reason } ` ) ;
164- case "HostFailure" :
165- throw new Error ( `Host failure: ${ value . value . reason } ` ) ;
166- }
167- } ,
168- ) ;
140+ MalformedFrame : Struct ( { reason : str } ) ,
141+ HostFailure : Struct ( { reason : str } ) ,
142+ } ) as Codec < CallErrorValue < D > > ;
169143}
170144
171145type TaggedUnionCodecs = {
0 commit comments