{"id":15668862,"url":"https://github.com/ghaerdi/rustify","last_synced_at":"2026-03-06T14:34:14.364Z","repository":{"id":235588745,"uuid":"753416912","full_name":"ghaerdi/rustify","owner":"ghaerdi","description":"Rust-like error handling for TypeScript with Result, Ok, and Err types.","archived":false,"fork":false,"pushed_at":"2025-10-23T14:08:04.000Z","size":72,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-23T15:05:04.597Z","etag":null,"topics":["error-handling","result","result-type","rust","rustify","ts-result","type-safe","typescirpt"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ghaerdi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2024-02-06T04:29:07.000Z","updated_at":"2025-07-03T20:12:27.000Z","dependencies_parsed_at":"2024-04-23T23:06:23.418Z","dependency_job_id":"7994aed5-fd05-4ff0-9e6d-00fb1704ed54","html_url":"https://github.com/ghaerdi/rustify","commit_stats":null,"previous_names":["ghaerdi/rustify"],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/ghaerdi/rustify","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ghaerdi%2Frustify","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ghaerdi%2Frustify/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ghaerdi%2Frustify/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ghaerdi%2Frustify/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ghaerdi","download_url":"https://codeload.github.com/ghaerdi/rustify/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ghaerdi%2Frustify/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30180728,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-06T12:39:21.703Z","status":"ssl_error","status_checked_at":"2026-03-06T12:36:09.819Z","response_time":250,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["error-handling","result","result-type","rust","rustify","ts-result","type-safe","typescirpt"],"created_at":"2024-10-03T14:20:22.499Z","updated_at":"2026-03-06T14:34:14.156Z","avatar_url":"https://github.com/ghaerdi.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# rustify\n\n[![npm version](https://img.shields.io/npm/v/@ghaerdi/rustify.svg)](https://www.npmjs.com/package/@ghaerdi/rustify)\n[![JSR version](https://jsr.io/badges/@ghaerdi/rustify)](https://jsr.io/@ghaerdi/rustify)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n\u003cbr\u003e\nA TypeScript library implementing Rust-like error handling with `Result`, `Ok`, and `Err` types, promoting type-safe and explicit error management.\n\n## Why rustify?\n\nJavaScript/TypeScript error handling often relies on `try...catch` blocks or nullable return types, which can be verbose or hide potential errors. `rustify` brings the `Result` type, a pattern widely used in Rust, to TypeScript. This allows you to:\n\n* **Handle errors explicitly:** Functions return a `Result` which is either `Ok(value)` for success or `Err(error)` for failure.\n* **Improve type safety:** The types `T` (success) and `E` (error) are tracked by the type system.\n* **Chain operations safely:** Methods like `andThen` and `orElse` allow elegant chaining.\n* **Perform exhaustive checks:** The `match` method ensures you handle both `Ok` and `Err` cases explicitly.\n* **Easily wrap unsafe functions:** `Result.from` provides a simple way to convert functions that might throw errors into functions that return a `Result`.\n* **Destructure results easily:** Use `asTuple()` for Go-style `[err, val]` destructuring, or `asObject()` if you prefer `{ error, value }` destructuring.\n\n## Installation\n\nYou can install `rustify` using your favorite package manager or directly from jsr.\n\n**npm:**\n\n```bash\nnpm install @ghaerdi/rustify\n# or\nyarn add @ghaerdi/rustify\n# or\npnpm add @ghaerdi/rustify\n```\n\n**jsr:**\n\n```bash\nnpx jsr add @ghaerdi/rustify\n# or\nbunx jsr add @ghaerdi/rustify\n# or\ndeno add @ghaerdi/rustify\n```\n\n## Basic Usage\n\nImport `Ok`, `Err`, and `Result` from the library.\n\n```typescript\nimport { Result, Ok, Err } from \"@ghaerdi/rustify\";\n\n// --- Creating a function that returns a Result ---\n\n// Example: A function that performs division but returns Err for division by zero\nfunction divide(numerator: number, denominator: number): Result\u003cnumber, string\u003e {\n  if (denominator === 0) {\n    return Err(\"Cannot divide by zero\"); // Failure case\n  }\n  const result = numerator / denominator;\n  return Ok(result); // Success case\n}\n\n// --- Using the function and handling the Result ---\n\nconst result = divide(10, 2); // Try change 2 to 0 for an Err case\n\n// Use 'match' to exhaustively handle both Ok and Err cases.\n// This is often the clearest way to ensure all possibilities are handled.\nconst message = result.match({\n  Ok: (value) =\u003e {\n    // This runs only if result is Ok\n    console.log(`Match Ok: Division successful, value is ${value}`);\n    return `Result: ${value}`;\n  },\n  Err: (errorMessage) =\u003e {\n    // This runs only if result is Err\n    console.error(\"Match Err:\", errorMessage);\n    return `Error: ${errorMessage}`;\n  }\n});\n\nconsole.log(message);\n\n// Other methods like Result.from, isOk, map, andThen, unwrapOrElse, asTuple etc.\n// allow for wrapping functions, specific checks, transformations, and handling patterns.\n// See the API Overview section for more details.\n```\n\n## Core Concepts\n\n* **`Result\u003cT, E\u003e`:** The main type, representing either success (`Ok\u003cT\u003e`) or failure (`Err\u003cE\u003e`).\n* **`Ok\u003cT\u003e`:** Represents a successful result containing a value of type `T`. Created using the `Ok(value)` function.\n    * If `T` is iterable (like an Array or String), the `Ok` instance itself becomes iterable.\n* **`Err\u003cE\u003e`:** Represents a failure containing an error value of type `E`. Created using the `Err(error)` function.\n\n## API Overview\n\nThe `Result` type provides numerous methods for handling and transformation:\n\n* **Checking:**\n    * `isOk()`: Returns `true` if `Ok`.\n    * `isErr()`: Returns `true` if `Err`.\n    * `isOkAnd(fn)`: Returns `true` if `Ok` and the value satisfies `fn`.\n    * `isErrAnd(fn)`: Returns `true` if `Err` and the error satisfies `fn`.\n* **Extracting Values:**\n    * `ok()`: Returns the `Ok` value or `undefined`.\n    * `err()`: Returns the `Err` value or `undefined`.\n    * `unwrap()`: Returns the `Ok` value, throws if `Err`. **Use with caution.**\n    * `unwrapErr()`: Returns the `Err` value, throws if `Ok`.\n    * `expect(message)`: Returns `Ok` value, throws `message` if `Err`.\n    * `expectErr(message)`: Returns `Err` value, throws `message` if `Ok`.\n    * `unwrapOr(defaultValue)`: Returns `Ok` value or `defaultValue` if `Err`.\n    * `unwrapOrElse(fn)`: Returns `Ok` value or computes default using `fn(errorValue)` if `Err`.\n* **Mapping \u0026 Transformation:**\n    * `map(fn)`: Maps `Ok\u003cT\u003e` to `Ok\u003cU\u003e`. Leaves `Err` untouched.\n    * `mapErr(fn)`: Maps `Err\u003cE\u003e` to `Err\u003cF\u003e`. Leaves `Ok` untouched.\n    * `mapOr(defaultValue, fn)`: Applies `fn` to `Ok` value, returns `defaultValue` if `Err`.\n    * `mapOrElse(defaultFn, fn)`: Applies `fn` to `Ok` value, applies `defaultFn` to `Err` value.\n* **Chaining \u0026 Side Effects:**\n    * `and(res)`: Returns `res` if `Ok`, else returns self (`Err`).\n    * `andThen(fn)`: Calls `fn(okValue)` if `Ok`, returns the resulting `Result`.\n    * `or(res)`: Returns `res` if `Err`, else returns self (`Ok`).\n    * `orElse(fn)`: Calls `fn(errValue)` if `Err`, returns the resulting `Result`.\n    * `inspect(fn)`: Calls `fn(okValue)` if `Ok`, returns original `Result`.\n    * `inspectErr(fn)`: Calls `fn(errValue)` if `Err`, returns original `Result`.\n* **Pattern Matching:**\n    * `match(matcher)`: Executes `matcher.Ok(value)` or `matcher.Err(error)`, returning the result.\n* **Cloning:**\n    * `cloned()`: Returns a new `Result` with a deep clone of the `Ok` value (using `structuredClone`). `Err` values are not cloned.\n* **Destructuring / Representation:**\n    * `asTuple()`: Represents the Result's state as a tuple `[error, value]`. Returns `[undefined, T]` for `Ok(T)` and `[E, undefined]` for `Err(E)`.\n    * `asObject()`: Represents the Result's state as an object `{ error, value }`. Returns `{ error: undefined, value: T }` for `Ok(T)` and `{ error: E, value: undefined }` for `Err(E)`.\n* **Utilities (Static Methods on `Result`):**\n    * `Result.from(fn, errorTransform?)`: Wraps a sync function `fn` that might throw. Executes `fn`. Returns `Ok(result)` or `Err(error)`.\n    * `Result.fromAsync(fn, errorTransform?)`: Wraps an async function `fn` returning a Promise. Returns `Promise\u003cResult\u003e`. Handles resolution/rejection.\n    * `Result.isResult(value)`: Type guard, returns `true` if `value` is `Ok` or `Err`.\n    * `wrapInResult(fn, errorTransform?)`: **[Deprecated]** Use `Result.from(() =\u003e fn(...args))` instead.\n## Development\n\nThis project uses Bun.\n\n* **Install Dependencies:**\n    ```bash\n    bun install\n    ```\n* **Type Checking:**\n    ```bash\n    bun run check --watch\n    ```\n* **Run Tests:**\n    ```bash\n    bun test --watch\n    ```\n\n## Contributing\n\nContributions welcome! Please submit issues and pull requests.\n\n1.  Fork the repository.\n2.  Create your feature branch.\n3.  Commit your changes.\n4.  Push to the branch.\n5.  Open a Pull Request.\n\n## License\n\nMIT License - see the LICENSE file for details.\n\n## Links\n\n* [GitHub Repository](https://github.com/ghaerdi/rustify)\n* [Issue Tracker](https://github.com/ghaerdi/rustify/issues)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fghaerdi%2Frustify","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fghaerdi%2Frustify","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fghaerdi%2Frustify/lists"}