@yuyu0202/rustlikeresult 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/README.md +53 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.js +87 -0
- package/dist/types/index.d.ts +11 -0
- package/dist/types/index.js +1 -0
- package/package.json +38 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
package/README.md
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# RustLikeResult
|
|
2
|
+
|
|
3
|
+
Rust-like `Result<T, E>` for TypeScript, with sync/async helpers.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install rustlikeresult
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick start
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { Ok, Err, Result } from "rustlikeresult";
|
|
15
|
+
|
|
16
|
+
const success = Ok(123);
|
|
17
|
+
const failure = Err(new Error("boom"));
|
|
18
|
+
|
|
19
|
+
const fromSync = Result.from(() => JSON.parse('{"ok":true}'));
|
|
20
|
+
const fromAsync = await Result.fromAsync(async () => {
|
|
21
|
+
return 42;
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
const value = success.unwrapOr(0);
|
|
25
|
+
|
|
26
|
+
const text = failure.match({
|
|
27
|
+
ok: (v) => `value=${v}`,
|
|
28
|
+
err: (e) => `error=${e.message}`,
|
|
29
|
+
});
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## API
|
|
33
|
+
|
|
34
|
+
- `Ok(value?)`: create a success result.
|
|
35
|
+
- `Err(error)`: create a failure result.
|
|
36
|
+
- `Result.from(fn)`: wrap a sync function and catch thrown errors.
|
|
37
|
+
- `Result.fromAsync(fn)`: wrap an async function and catch rejected/thrown errors.
|
|
38
|
+
- `result.unwrap()`: get value or throw stored error.
|
|
39
|
+
- `result.unwrapOr(defaultValue)`: get value or fallback.
|
|
40
|
+
- `result.isOk()` / `result.isErr()`: type guards.
|
|
41
|
+
- `result.match({ ok, err })`: pattern-match style branching.
|
|
42
|
+
|
|
43
|
+
## Development
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
npm run typecheck
|
|
47
|
+
npm run build
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## License
|
|
51
|
+
|
|
52
|
+
MIT
|
|
53
|
+
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { ResultAsync, Awaitable } from "./types/index";
|
|
2
|
+
export declare function Ok<T>(): Result<void, never>;
|
|
3
|
+
export declare function Ok<T>(value: T): Result<T, never>;
|
|
4
|
+
export declare function Err<E = Error>(error: E): Result<never, E>;
|
|
5
|
+
export declare class Result<T, E = Error> {
|
|
6
|
+
static Ok<T>(): Result<void, never>;
|
|
7
|
+
static Ok<T>(value: T): Result<T, never>;
|
|
8
|
+
static Err<E = Error>(error: E): Result<never, E>;
|
|
9
|
+
static fromAsync<T>(fn: () => Awaitable<T>): ResultAsync<T>;
|
|
10
|
+
static from<T>(fn: () => T): Result<T>;
|
|
11
|
+
readonly success: boolean;
|
|
12
|
+
readonly value?: T;
|
|
13
|
+
readonly error?: E;
|
|
14
|
+
private constructor();
|
|
15
|
+
unwrap(): T;
|
|
16
|
+
unwrapOr(defaultValue: T): T;
|
|
17
|
+
unwrapOrElse(defaultFn: () => T): T;
|
|
18
|
+
ok(): T | undefined;
|
|
19
|
+
err(): E | undefined;
|
|
20
|
+
isOk(): this is Result<T, E> & {
|
|
21
|
+
ok: true;
|
|
22
|
+
value: T;
|
|
23
|
+
};
|
|
24
|
+
isErr(): this is Result<T, E> & {
|
|
25
|
+
ok: false;
|
|
26
|
+
error: E;
|
|
27
|
+
};
|
|
28
|
+
match<U>(handlers: Readonly<{
|
|
29
|
+
ok: (v: T) => U;
|
|
30
|
+
err: (e: E) => U;
|
|
31
|
+
}>): U;
|
|
32
|
+
map<U>(fn: (v: T) => U): Result<U, E>;
|
|
33
|
+
mapErr<F>(fn: (e: E) => F): Result<T, F>;
|
|
34
|
+
andThen<U>(fn: (v: T) => Result<U, E>): Result<U, E>;
|
|
35
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
export function Ok(value) {
|
|
2
|
+
if (arguments.length === 0) {
|
|
3
|
+
return Result.Ok();
|
|
4
|
+
}
|
|
5
|
+
else {
|
|
6
|
+
return Result.Ok(value);
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
export function Err(error) {
|
|
10
|
+
return Result.Err(error);
|
|
11
|
+
}
|
|
12
|
+
export class Result {
|
|
13
|
+
static Ok(value) {
|
|
14
|
+
if (arguments.length === 0) {
|
|
15
|
+
return new Result({ ok: true, value: undefined });
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
return new Result({ ok: true, value: value });
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
static Err(error) {
|
|
22
|
+
return new Result({ ok: false, error });
|
|
23
|
+
}
|
|
24
|
+
static async fromAsync(fn) {
|
|
25
|
+
try {
|
|
26
|
+
return Result.Ok(await fn());
|
|
27
|
+
}
|
|
28
|
+
catch (e) {
|
|
29
|
+
return Result.Err(e instanceof Error ? e : new Error(String(e)));
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
static from(fn) {
|
|
33
|
+
try {
|
|
34
|
+
return Result.Ok(fn());
|
|
35
|
+
}
|
|
36
|
+
catch (e) {
|
|
37
|
+
return Result.Err(e instanceof Error ? e : new Error(String(e)));
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
constructor(payload) {
|
|
41
|
+
this.success = payload.ok;
|
|
42
|
+
if (payload.ok) {
|
|
43
|
+
this.value = payload.value;
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
this.error = payload.error;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
unwrap() {
|
|
50
|
+
if (this.success) {
|
|
51
|
+
return this.value;
|
|
52
|
+
}
|
|
53
|
+
throw this.error;
|
|
54
|
+
}
|
|
55
|
+
unwrapOr(defaultValue) {
|
|
56
|
+
return this.isOk() ? this.value : defaultValue;
|
|
57
|
+
}
|
|
58
|
+
unwrapOrElse(defaultFn) {
|
|
59
|
+
return this.isOk() ? this.value : defaultFn();
|
|
60
|
+
}
|
|
61
|
+
ok() {
|
|
62
|
+
return this.isOk() ? this.value : undefined;
|
|
63
|
+
}
|
|
64
|
+
err() {
|
|
65
|
+
return this.isOk() ? undefined : this.error;
|
|
66
|
+
}
|
|
67
|
+
isOk() {
|
|
68
|
+
return this.success;
|
|
69
|
+
}
|
|
70
|
+
isErr() {
|
|
71
|
+
return !this.success;
|
|
72
|
+
}
|
|
73
|
+
match(handlers) {
|
|
74
|
+
return this.isOk()
|
|
75
|
+
? handlers.ok(this.value)
|
|
76
|
+
: handlers.err(this.error);
|
|
77
|
+
}
|
|
78
|
+
map(fn) {
|
|
79
|
+
return this.isOk() ? Result.Ok(fn(this.value)) : Result.Err(this.error);
|
|
80
|
+
}
|
|
81
|
+
mapErr(fn) {
|
|
82
|
+
return this.isErr() ? Result.Err(fn(this.error)) : Result.Ok(this.value);
|
|
83
|
+
}
|
|
84
|
+
andThen(fn) {
|
|
85
|
+
return this.isOk() ? fn(this.value) : Result.Err(this.error);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Result } from "../index";
|
|
2
|
+
export type Awaitable<T> = T | Promise<T>;
|
|
3
|
+
export type ResultPayload<T, E> = {
|
|
4
|
+
ok: true;
|
|
5
|
+
value: T;
|
|
6
|
+
} | {
|
|
7
|
+
ok: false;
|
|
8
|
+
error: E;
|
|
9
|
+
};
|
|
10
|
+
export type ResultAsync<T, E = Error> = Promise<Result<T, E>>;
|
|
11
|
+
export type AwaitableResult<T, E = Error> = Awaitable<Result<T, E>>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@yuyu0202/rustlikeresult",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Rust-like Result type for TypeScript with sync/async helpers.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"default": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"README.md",
|
|
18
|
+
"LICENSE"
|
|
19
|
+
],
|
|
20
|
+
"sideEffects": false,
|
|
21
|
+
"keywords": [
|
|
22
|
+
"result",
|
|
23
|
+
"rust",
|
|
24
|
+
"typescript",
|
|
25
|
+
"error-handling"
|
|
26
|
+
],
|
|
27
|
+
"license": "MIT",
|
|
28
|
+
"dependencies": {},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"typescript": "^5.5.3",
|
|
31
|
+
"rimraf": "^6.1.3"
|
|
32
|
+
},
|
|
33
|
+
"scripts": {
|
|
34
|
+
"clean": "rimraf dist",
|
|
35
|
+
"build": "tsc -p tsconfig.json",
|
|
36
|
+
"typecheck": "tsc -p tsconfig.json --noEmit"
|
|
37
|
+
}
|
|
38
|
+
}
|