-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: separate rust_types to own import path
- Loading branch information
Showing
6 changed files
with
70 additions
and
68 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
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,14 @@ | ||
Rust Types | ||
========== | ||
|
||
**Why is this needed?** | ||
|
||
This is used by `ContractSpec` and `AssembledTransaction` when parsing values return by contracts. | ||
|
||
Contract methods can be implemented to return simple values, in which case they can also throw errors. This matches JavaScript's most idiomatic workflow, using `try...catch` blocks. | ||
|
||
But Rust also gives the flexibility of returning `Result` types. And Soroban contracts further support this with the `#[contracterror]` macro. Should JavaScript calls to such methods ignore all of that, and just flatten this extra info down to the same `try...catch` flow as other methods? We're not sure. | ||
|
||
For now, we've added this special `rust_types` logic, which exports the `Result` interface and its associated implementations, `Ok` and `Err`. This allows `ContractSpec` and `AssembledTransaction` to work together to duplicate the contract's Rust logic, always returning `Result` types for contract methods that are implemented to do so. | ||
|
||
In the future, if this feels too un-idiomatic for JavaScript, we can always remove this and flatten all JS calls to `try...catch`. Easier to remove this logic later than it would be to add it. |
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,47 @@ | ||
/** | ||
* A minimal implementation of Rust's `Result` type. Used for contract | ||
* methods that return Results, to maintain their distinction from methods | ||
* that simply either return a value or throw. | ||
*/ | ||
export interface Result<T, E extends ErrorMessage> { | ||
unwrap(): T; | ||
unwrapErr(): E; | ||
isOk(): boolean; | ||
isErr(): boolean; | ||
} | ||
|
||
/** | ||
* Error interface containing the error message. Matches Rust's implementation. | ||
* Part of implementing {@link Result}, a minimal implementation of Rust's | ||
* `Result` type. Used for contract methods that return Results, to maintain | ||
* their distinction from methods that simply either return a value or throw. | ||
*/ | ||
export interface ErrorMessage { | ||
message: string; | ||
} | ||
|
||
/** | ||
* Part of implementing {@link Result}, a minimal implementation of Rust's | ||
* `Result` type. Used for contract methods that return Results, to maintain | ||
* their distinction from methods that simply either return a value or throw. | ||
*/ | ||
export class Ok<T> implements Result<T, never> { | ||
constructor(readonly value: T) {} | ||
unwrapErr(): never { throw new Error("No error") } | ||
unwrap() { return this.value } | ||
isOk() { return true } | ||
isErr() { return false } | ||
} | ||
|
||
/** | ||
* Part of implementing {@link Result}, a minimal implementation of Rust's | ||
* `Result` type. Used for contract methods that return Results, to maintain | ||
* their distinction from methods that simply either return a value or throw. | ||
*/ | ||
export class Err<E extends ErrorMessage> implements Result<never, E> { | ||
constructor(readonly error: E) {} | ||
unwrapErr() { return this.error } | ||
unwrap(): never { throw new Error(this.error.message) } | ||
isOk() { return false } | ||
isErr() { return 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