-
Notifications
You must be signed in to change notification settings - Fork 758
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5cb29e7
commit 66b3282
Showing
9 changed files
with
192 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { CairoCustomEnum, CairoOption, CairoResult } from '../utils/calldata/enum'; | ||
|
||
export type CairoEnum = CairoCustomEnum | CairoOption<Object> | CairoResult<Object, Object>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
export type CairoEnumRaw = { | ||
[key: string]: any; | ||
}; | ||
|
||
/** | ||
* Class to handle Cairo custom Enum | ||
* @param enumContent object containing the variants and its content. Example : | ||
* {Success: 234, Warning: undefined, Error: undefined}. | ||
* Only one variant with a value, object, array. | ||
* @returns an instance representing a Cairo custom Enum. | ||
* @example ```typescript | ||
* const myCairoEnum = new CairoCustomEnum( {Success: undefined, Warning: "0x7f32ea", Error: undefined}) | ||
* ``` | ||
*/ | ||
export class CairoCustomEnum { | ||
/** | ||
* direct readonly access to variants of the Cairo Custom Enum. | ||
* @returns a value of type any | ||
* @example ```typescript | ||
* const successValue = myCairoEnum.variant.Success; | ||
*/ | ||
readonly variant: CairoEnumRaw; | ||
|
||
constructor(enumContent: CairoEnumRaw) { | ||
// TODO : add checks of validity of enumContent | ||
this.variant = enumContent; | ||
} | ||
|
||
/** | ||
* | ||
* @returns the content of the valid variant of a Cairo custom Enum. | ||
*/ | ||
public unwrap(): any { | ||
const variants = Object.entries(this.variant); | ||
const activeVariant = variants.find((item) => typeof item[1] !== 'undefined'); | ||
if (typeof activeVariant === 'undefined') { | ||
return undefined; | ||
} | ||
return activeVariant[1]; | ||
} | ||
|
||
// TODO : add function 'activeVariant' -> string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
export enum CairoOptionVariant { | ||
Some = 0, | ||
None = 1, | ||
} | ||
|
||
/** | ||
* Class to handle Cairo Option | ||
* @param variant CairoOptionVariant.Some or CairoOptionVariant.None | ||
* @param someContent value of type T. | ||
* @returns an instance representing a Cairo Option. | ||
* @example ```typescript | ||
* const myOption = new CairoOption<BigNumberish>(CairoOptionVariant.Some, "0x54dda8"); | ||
* ``` | ||
*/ | ||
export class CairoOption<T> { | ||
readonly Some?: T; | ||
|
||
readonly None?: boolean; | ||
|
||
constructor(variant: CairoOptionVariant, someContent?: T) { | ||
if (variant === CairoOptionVariant.Some) { | ||
if (typeof someContent === 'undefined') { | ||
throw new Error( | ||
'The creation of a Cairo Option with "Some" variant needs a content as input.' | ||
); | ||
} | ||
this.Some = someContent; | ||
} | ||
this.None = true; | ||
} | ||
|
||
/** | ||
* | ||
* @returns the content of the valid variant of a Cairo custom Enum. | ||
* If None, returns 'undefined'. | ||
*/ | ||
public unwrap(): T | undefined { | ||
if (this.None) { | ||
return undefined; | ||
} | ||
return this.Some; | ||
} | ||
|
||
/** | ||
* | ||
* @returns true if the valid variant is 'isSome'. | ||
*/ | ||
public isSome(): boolean { | ||
return !(typeof this.Some === 'undefined'); | ||
} | ||
|
||
/** | ||
* | ||
* @returns true if the valid variant is 'isNone'. | ||
*/ | ||
public isNone(): boolean { | ||
return this.None === true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
export enum CairoResultVariant { | ||
Ok = 0, | ||
Err = 1, | ||
} | ||
|
||
/** | ||
* Class to handle Cairo Result | ||
* @param variant CairoResultVariant.Ok or CairoResultVariant.Err | ||
* @param resultContent value of type T or U. | ||
* @returns an instance representing a Cairo Result. | ||
* @example ```typescript | ||
* const myOption = new CairoResult<BigNumberish,CustomError>(CairoResultVariant.Ok, "0x54dda8"); | ||
* ``` | ||
*/ | ||
export class CairoResult<T, U> { | ||
readonly Ok?: T; | ||
|
||
readonly Err?: U; | ||
|
||
constructor(variant: CairoResultVariant, resultContent: T | U) { | ||
if (variant === CairoResultVariant.Ok) { | ||
this.Ok = resultContent as T; | ||
} | ||
this.Err = resultContent as U; | ||
} | ||
|
||
/** | ||
* | ||
* @returns the content of the valid variant of a Cairo Result. | ||
*/ | ||
public unwrap(): T | U { | ||
if (typeof this.Ok !== 'undefined') { | ||
return this.Ok; | ||
} | ||
if (typeof this.Err !== 'undefined') { | ||
return this.Err; | ||
} | ||
throw new Error('Both Result.Ok and .Err are undefined. Not authorized.'); | ||
} | ||
|
||
/** | ||
* | ||
* @returns true if the valid variant is 'Ok'. | ||
*/ | ||
public isOk(): boolean { | ||
return !(typeof this.Ok === 'undefined'); | ||
} | ||
|
||
/** | ||
* | ||
* @returns true if the valid variant is 'isErr'. | ||
*/ | ||
public isErr(): boolean { | ||
return !(typeof this.Err === 'undefined'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './CairoCustomEnum'; | ||
export * from './CairoOption'; | ||
export * from './CairoResult'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters