{"id":17668734,"url":"https://github.com/lukasjhan/rust-variant-ts","last_synced_at":"2026-01-02T09:07:17.216Z","repository":{"id":256243420,"uuid":"854706255","full_name":"lukasjhan/rust-variant-ts","owner":"lukasjhan","description":"Typescript implementation of Rust's variant, result, and option","archived":false,"fork":false,"pushed_at":"2024-09-09T16:40:44.000Z","size":29,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-09-09T20:32:22.045Z","etag":null,"topics":[],"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/lukasjhan.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}},"created_at":"2024-09-09T16:31:10.000Z","updated_at":"2024-09-09T16:41:02.000Z","dependencies_parsed_at":"2024-09-09T20:32:25.029Z","dependency_job_id":null,"html_url":"https://github.com/lukasjhan/rust-variant-ts","commit_stats":null,"previous_names":["lukasjhan/rust-variant-ts"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lukasjhan%2Frust-variant-ts","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lukasjhan%2Frust-variant-ts/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lukasjhan%2Frust-variant-ts/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lukasjhan%2Frust-variant-ts/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lukasjhan","download_url":"https://codeload.github.com/lukasjhan/rust-variant-ts/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221250269,"owners_count":16785107,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":[],"created_at":"2024-10-23T23:41:59.365Z","updated_at":"2026-01-02T09:07:17.188Z","avatar_url":"https://github.com/lukasjhan.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# rust-variant\n\n[![npm version](https://badge.fury.io/js/rust-variant.svg)](https://badge.fury.io/js/rust-variant)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n\nA TypeScript implementation of Rust's Variant, Result, and Option types. This library provides a robust way to handle algebraic data types, error handling, and optional values in TypeScript, inspired by Rust's powerful type system.\n\n## Features\n\n- **Variant Type**: A TypeScript implementation of Rust's enum-like variants.\n- **Result Type**: For error handling, similar to Rust's `Result\u003cT, E\u003e`.\n- **Option Type**: For handling optional values, similar to Rust's `Option\u003cT\u003e`.\n- **Pattern Matching**: Use the `match` method for elegant pattern matching.\n- **Functional Methods**: Includes methods like `map`, `flatMap`, and more for composable operations.\n\n## Installation\n\nInstall the package using npm:\n\n```bash\nnpm install rust-variant\n```\n\nOr using yarn:\n\n```bash\nyarn add rust-variant\n```\n\nOr using pnpm:\n\n```bash\npnpm add rust-variant\n```\n\n## Usage\n\n### Importing\n\n```typescript\nimport { VariantType } from 'rust-variant';\n```\n\n### Result Type\n\nUse `Result` for operations that might fail:\n\n```typescript\nfunction divide(a: number, b: number): VariantType.Result\u003cnumber, string\u003e {\n  if (b === 0) {\n    return VariantType.Result.err('Division by zero');\n  }\n  return VariantType.Result.ok(a / b);\n}\n\nconst result = divide(10, 2);\nresult.match({\n  Ok: ({ value }) =\u003e console.log(`Result: ${value}`),\n  Err: ({ error }) =\u003e console.log(`Error: ${error}`),\n});\n```\n\n### Option Type\n\nUse `Option` for values that might not exist:\n\n```typescript\nfunction findEven(numbers: number[]): VariantType.Option\u003cnumber\u003e {\n  const even = numbers.find((n) =\u003e n % 2 === 0);\n  return even !== undefined\n    ? VariantType.Option.some(even)\n    : VariantType.Option.none();\n}\n\nconst numbers = [1, 3, 5, 7, 8, 9];\nconst evenNumber = findEven(numbers);\n\nevenNumber.match({\n  Some: ({ value }) =\u003e console.log(`Found even number: ${value}`),\n  None: () =\u003e console.log('No even number found'),\n});\n```\n\n### Custom Variant Types\n\nCreate your own variant types:\n\n```typescript\ntype ShapeTag = 'Circle' | 'Rectangle' | 'Triangle';\n\ntype ShapeShape = {\n  Circle: { radius: number };\n  Rectangle: { width: number; height: number };\n  Triangle: { base: number; height: number };\n};\n\nabstract class Shape extends VariantType.Variant\u003cShapeTag, ShapeShape\u003e {\n  static circle(radius: number): Shape {\n    return new Circle(radius);\n  }\n\n  static rectangle(width: number, height: number): Shape {\n    return new Rectangle(width, height);\n  }\n\n  static triangle(base: number, height: number): Shape {\n    return new Triangle(base, height);\n  }\n}\n\n// Usage\nconst shape = Shape.circle(5);\nshape.match({\n  Circle: ({ radius }) =\u003e console.log(`Circle with radius ${radius}`),\n  Rectangle: ({ width, height }) =\u003e\n    console.log(`Rectangle with width ${width} and height ${height}`),\n  Triangle: ({ base, height }) =\u003e\n    console.log(`Triangle with base ${base} and height ${height}`),\n});\n```\n\n## API Reference\n\n### Result\u003cT, E\u003e\n\n- `ok\u003cT, E\u003e(value: T): Result\u003cT, E\u003e`\n- `err\u003cT, E\u003e(error: E): Result\u003cT, E\u003e`\n- `match\u003cR\u003e(handlers: { Ok: (value: T) =\u003e R, Err: (error: E) =\u003e R }): R`\n\n### Option\u003cT\u003e\n\n- `some\u003cT\u003e(value: T): Option\u003cT\u003e`\n- `none\u003cT\u003e(): Option\u003cT\u003e`\n- `match\u003cR\u003e(handlers: { Some: (value: T) =\u003e R, None: () =\u003e R }): R`\n- `isSome(): boolean`\n- `isNone(): boolean`\n- `unwrap(): T`\n- `unwrapOr(defaultValue: T): T`\n- `map\u003cU\u003e(f: (value: T) =\u003e U): Option\u003cU\u003e`\n- `flatMap\u003cU\u003e(f: (value: T) =\u003e Option\u003cU\u003e): Option\u003cU\u003e`\n\n### Variant\u003cTag, Shape\u003e\n\n- `match\u003cR\u003e(handlers: { [K in Tag]: (value: Shape[K]) =\u003e R }): R`\n\n## Contributing\n\nContributions are welcome! Please feel free to submit a Pull Request.\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.\n\n## Acknowledgments\n\n- Inspired by Rust's powerful type system and error handling patterns.\n- Thanks to the TypeScript community for providing the tools to make this possible.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flukasjhan%2Frust-variant-ts","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flukasjhan%2Frust-variant-ts","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flukasjhan%2Frust-variant-ts/lists"}