Skip to main content
Version: 11.x

Class: CairoTypeResult

Defined in: src/utils/cairoDataTypes/cairoTypeResult.ts:32

A Cairo core::result::Result::<T, E> : either an outcome or an error, each with its own type.

On the wire it is an enum with two branches — 0 then the Ok value, 1 then the Err value. Unlike an option, both branches carry something, so a Result always has content; what cannot be guessed is which of the two types that content is, which is why building one from raw data asks for the variant.

This is the internal shape. What a caller writes and reads back is CairoResult, and handing one here says the branch by itself.

Example

const type = 'core::result::Result::<core::integer::u8, core::integer::u16>';
new CairoTypeResult(8, type, cairoTypeStrategy, CairoResultVariant.Ok).toApiRequest();
// ["0", "8"]
new CairoTypeResult(8, type, cairoTypeStrategy, CairoResultVariant.Err).toApiRequest();
// ["1", "8"]

Constructors

Constructor

new CairoTypeResult(content, resultCairoType, parsingStrategy, variant?, subType?): CairoTypeResult

Defined in: src/utils/cairoDataTypes/cairoTypeResult.ts:121

Build a result, from a value a caller passed or from the felts of a response.

A CairoResult says its own branch, so variant must be left out with one — as with the response iterator, and as with a CairoTypeResult being copied. Anywhere else it is required, since the two branches carry different types and the value alone does not say which.

subType is what makes a result of results work. Handed a CairoResult, this constructor unwraps it and calls itself with what was inside, which for a nested result is another CairoResult; the flag stops that second call from unwrapping again and losing a level.

Parameters

content

unknown

the value, a CairoResult, an instance already built, or the response iterator. Never undefined : both branches carry something.

resultCairoType

string

the abi type, core::result::Result::<T, E>

parsingStrategy

AllowArray<CairoTypeStrategy>

how to build the value

variant?

number

which branch, when the content does not say

subType?

boolean = false

true when called from the unwrapping of a nested CairoResult, which is the only caller that should set it

Returns

CairoTypeResult

Throws

when the content is missing, the type is not a result, or the variant is missing, absurd, or given where the content already says it

Example

const type = 'core::result::Result::<core::integer::u8, core::integer::u16>';
new CairoTypeResult(8, type, cairoTypeStrategy, CairoResultVariant.Ok).toApiRequest();
// ["0", "8"]
new CairoTypeResult(new CairoResult(CairoResultVariant.Err, 8), type, cairoTypeStrategy).toApiRequest();
// ["1", "8"] the CairoResult says the branch
new CairoTypeResult(['0x0', '0x64'].values(), type, cairoTypeStrategy).toApiRequest();
// ["0", "100"] read off a response

Properties

dynamicSelector

static dynamicSelector: "CairoTypeResult"

Defined in: src/utils/cairoDataTypes/cairoTypeResult.ts:41

The name this class is registered under in a strategy's dynamicSelectors.

Example

const result = CairoTypeResult.dynamicSelector;
// result = "CairoTypeResult"

dynamicSelector

readonly dynamicSelector: "CairoTypeResult" = CairoTypeResult.dynamicSelector

Defined in: src/utils/cairoDataTypes/cairoTypeResult.ts:53

The selector on the instance, which is how a composite holding it knows what built it.

Example

const type = 'core::result::Result::<core::integer::u8, core::integer::u16>';
const result = new CairoTypeResult(8, type, cairoTypeStrategy, CairoResultVariant.Ok)
.dynamicSelector;
// result = "CairoTypeResult"

content

readonly content: CairoType

Defined in: src/utils/cairoDataTypes/cairoTypeResult.ts:65

The value this result carries, on whichever branch it is.

Example

const type = 'core::result::Result::<core::integer::u8, core::integer::u16>';
const result = new CairoTypeResult(8, type, cairoTypeStrategy, CairoResultVariant.Ok)
.content.toApiRequest();
// result = ["8"]

resultCairoType

readonly resultCairoType: string

Defined in: src/utils/cairoDataTypes/cairoTypeResult.ts:77

The abi type this result was built for.

Example

const type = 'core::result::Result::<core::integer::u8, core::integer::u16>';
const result = new CairoTypeResult(8, type, cairoTypeStrategy, CairoResultVariant.Ok)
.resultCairoType;
// result = "core::result::Result::<core::integer::u8, core::integer::u16>"

isVariantOk

readonly isVariantOk: boolean

Defined in: src/utils/cairoDataTypes/cairoTypeResult.ts:89

Which of the two branches this result is on.

Example

const type = 'core::result::Result::<core::integer::u8, core::integer::u16>';
const result = new CairoTypeResult(8, type, cairoTypeStrategy, CairoResultVariant.Err)
.isVariantOk;
// result = false

Methods

getVariantTypes()

static getVariantTypes(type): string[]

Defined in: src/utils/cairoDataTypes/cairoTypeResult.ts:250

The two types a result declares, in the order [Ok, Err].

The two are separated by a comma inside the angle brackets, which is exactly how a tuple writes its members — so the tuple splitter reads them, wrapped in parentheses for the occasion, rather than a second comma-scanner being written here.

Parameters

type

string

the abi type to read

Returns

string[]

the Ok type then the Err type

Throws

when the type carries no bracketed pair, or does not hold exactly two types

Example

const result = CairoTypeResult.getVariantTypes(
'core::result::Result::<core::integer::u8, core::integer::u16>'
);
// result = ["core::integer::u8", "core::integer::u16"]

validate()

static validate(_input, type, variant): void

Defined in: src/utils/cairoDataTypes/cairoTypeResult.ts:282

Throw unless this is a result type and this variant is one of its two branches.

The value is not read here : what it is worth is the business of the branch type, and is checked when that value is built.

Parameters

_input

unknown

the value, which this does not read

type

string

the abi type it is meant for

variant

VariantType | undefined

the branch, when one was given

Returns

void

Throws

when the type is not a result, or the variant is neither 0 nor 1

Example

const type = 'core::result::Result::<core::integer::u8, core::integer::u16>';
CairoTypeResult.validate(200, type, CairoResultVariant.Err); // passes
CairoTypeResult.validate(200, type, 3);
// throws Error("In Cairo Result, only 0 or 1 variants are authorized.")

is()

static is(input, type, variant): boolean

Defined in: src/utils/cairoDataTypes/cairoTypeResult.ts:313

Can this be read as a result of this type, on this branch?

The non-throwing form of CairoTypeResult.validate.

Parameters

input

unknown

the value to test

type

string

the abi type it is meant for

variant

VariantType

the branch

Returns

boolean

true when the type and the variant both fit

Example

const type = 'core::result::Result::<core::integer::u8, core::integer::u16>';
const result = CairoTypeResult.is(200, type, CairoResultVariant.Ok);
// result = true
const result2 = CairoTypeResult.is(200, 'wrong', 3);
// result2 = false

isAbiType()

static isAbiType(type): boolean

Defined in: src/utils/cairoDataTypes/cairoTypeResult.ts:336

Is this abi type a result?

Parameters

type

string

the abi type to test

Returns

boolean

true for core::result::Result::<T, E>

Example

const result = CairoTypeResult.isAbiType(
'core::result::Result::<core::integer::u8, core::integer::u16>'
);
// result = true
const result2 = CairoTypeResult.isAbiType('core::integer::u16');
// result2 = false

toApiRequest()

toApiRequest(): string[]

Defined in: src/utils/cairoDataTypes/cairoTypeResult.ts:352

Serialize to the felts a contract call carries.

The branch comes first, then the value — always, since both branches carry one.

Returns

string[]

the branch then the value, flagged as compiled

Example

const type = 'core::result::Result::<core::integer::u8, core::integer::u16>';
new CairoTypeResult(8, type, cairoTypeStrategy, CairoResultVariant.Err).toApiRequest();
// ["1", "8"]

decompose()

decompose(strategyDecompose): CairoResult<any, any>

Defined in: src/utils/cairoDataTypes/cairoTypeResult.ts:371

Read the result back as the CairoResult a caller reads.

Parameters

strategyDecompose

AllowArray<CairoTypeStrategy>

how to read the value back

Returns

CairoResult<any, any>

the result, on the branch it was built with

Throws

when no strategy can read the value back

Example

const type = 'core::result::Result::<core::integer::u8, core::integer::u16>';
const result = new CairoTypeResult(3, type, cairoTypeStrategy, CairoResultVariant.Ok);
const value = result.decompose(cairoTypeStrategy).unwrap();
// value = 3n