https://github.com/yakiyo/result_rs
Rust's Result implementation in Javascript
https://github.com/yakiyo/result_rs
deno deno-module dnt rust-result
Last synced: 3 months ago
JSON representation
Rust's Result implementation in Javascript
- Host: GitHub
- URL: https://github.com/yakiyo/result_rs
- Owner: Yakiyo
- License: mit
- Created: 2023-02-07T15:03:30.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-04-28T17:04:55.000Z (about 2 years ago)
- Last Synced: 2025-01-28T14:02:35.015Z (4 months ago)
- Topics: deno, deno-module, dnt, rust-result
- Language: TypeScript
- Homepage: https://deno.land/x/result_rs
- Size: 44.9 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# result-rs
Rust's [Result](https://doc.rust-lang.org/std/result/enum.Result.html)
enum ported to Typescript/Javascript## Installation
Import the module to your code:
```ts
import { Result } from 'https://deno.land/x/result_rs/mod.ts';
```It is also published as a [npm](npmjs.com/) module as result_rs.
```bash
$ npm i result_rs
# or using pnpm
$ pnpm add result_rs
# or yarn
$ yarn add result_rs
```Then you can import it in your code:
```ts
// Commonjs require
const { Result } = require('result_rs');
// ES Module import
import { Result } from 'result_rs';
```## Usage
Use the `Result.from` method to create a new instance of Result
```ts
const result = Result.from(someFunctionThatThrows);result.is_ok(); // Returns false if function throwed an error
```The package also exports two utility functions `Ok` and `Err` that are
useful to create Ok or Err values from simple values instead of a
function```ts
import { Err, Ok } from 'https://deno.land/x/result_rs/mod.ts';const ok = Ok('Hello World');
ok.is_ok(); // true;
ok.unwrap(); // 'Hello World'const err = Err('Error value');
err.is_er(); // true;
err.unwrap(); // throws an error
```Passing async functions to the `Result.from` method are handled like
synchronous functions. If you want to resolve the internal value's
promise, you can use the `Result.sync` method on any instance to
resolve the promise internally.```ts
const result = Result.from(async () => 'Hello World');
result.unwrap(); // Promiseawait result.sync();
result.unwrap(); // 'Hello World'
```Documentation of all methods is available
[here](https://deno.land/x/result_rs?doc).## Author
**result-rs** © [Yakiyo](https://github.com/Yakiyo). Authored and
maintained by Yakiyo.Released under [MIT](https://opensource.org/licenses/MIT) License