{"id":18668697,"url":"https://github.com/franeklubi/rustic","last_synced_at":"2025-07-24T12:38:30.157Z","repository":{"id":42464861,"uuid":"391355832","full_name":"franeklubi/rustic","owner":"franeklubi","description":"rustic is a TypeScript library providing emulation of Rust's Option and Result types (and some useful wrappers for common js functions as well!)","archived":false,"fork":false,"pushed_at":"2022-07-25T10:45:26.000Z","size":162,"stargazers_count":142,"open_issues_count":3,"forks_count":2,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-06-29T17:48:32.436Z","etag":null,"topics":["library","rust","rust-option","rust-result","typescript","typescript-library"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/rustic","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/franeklubi.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}},"created_at":"2021-07-31T12:53:28.000Z","updated_at":"2025-06-15T15:51:09.000Z","dependencies_parsed_at":"2022-08-25T18:21:34.273Z","dependency_job_id":null,"html_url":"https://github.com/franeklubi/rustic","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/franeklubi/rustic","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/franeklubi%2Frustic","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/franeklubi%2Frustic/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/franeklubi%2Frustic/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/franeklubi%2Frustic/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/franeklubi","download_url":"https://codeload.github.com/franeklubi/rustic/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/franeklubi%2Frustic/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266844490,"owners_count":23993965,"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","status":"online","status_checked_at":"2025-07-24T02:00:09.469Z","response_time":99,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":["library","rust","rust-option","rust-result","typescript","typescript-library"],"created_at":"2024-11-07T08:44:37.796Z","updated_at":"2025-07-24T12:38:30.071Z","avatar_url":"https://github.com/franeklubi.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"# rustic\n\n`rustic` is a TypeScript library providing emulation of `Rust`'s `Option` and `Result` types (and some useful wrappers for common js functions as well!).\n\nThe repo has 100% test code coverage, so you can be sure it will never fail! Even more in your production code™. (The not fail part is obviously a lie. Test your code.)\n\n---\n* [Installation](#installation)\n* [Usage](#usage)\n\t* [Result](#result)\n\t* [Option](#option)\n* [Wrappers](#wrappers)\n\t* [catchResult](#catchResult)\n\t* [parseJson](#parsejson)\n---\n\n# Installation\n\nJust install as any other package:\n```sh\n$ npm i rustic\n```\n\n# Usage\n\n## Result\n\n1. Let's suppose we you have a fallible function that'll return an error for random number lower than 5:\n\n```ts\nimport { Result, Err, Ok, isOk } from 'rustic';\n\nfunction fallible(): Result\u003cnumber, string\u003e {\n\tconst zeroToTen: number = Math.random() * 10;\n\n\t// Using Err and Ok helper as a shorthand to produce Result\n\tif (zeroToTen \u003c 5) {\n\t\treturn Err(\"Lower than 5\");\n\t} else {\n\t\treturn Ok(zeroToTen);\n\t}\n}\n\nconst res = fallible();\n\n// Using isOk helper will do this for You, but You can also\n// access the `__kind` field and compare it with `ResultKind` enum directly\nif (isOk(res)) {\n\t// Typescript infers res.data's type as `number`\n\tconsole.log('Successful num sq:', res.data * res.data);\t// Successful num sq: \u003cnumber\u003e\n} else {\n\tconsole.log('Error:', res.data);\t// 'Error: Lower than 5'\n}\n```\n\n2. Suppose you want to map the Result of a fallible function:\n\n```ts\nimport { Result, equip, ResultEquipped } from 'rustic';\n\nfunction fallible(): Result\u003cnumber, string\u003e { ... }\n\nconst res: Result\u003cnumber, string\u003e = fallible();\n\n// Call `equip` with the Result of fallible function\nconst equipped: ResultEquipped\u003cnumber, string\u003e = equip(res);\n\n// Use as You would Rust's Result\nconst squared: number = equipped.map(n =\u003e n * n).expect('Squared n');\n\n// Using unwrap can cause a panic: `panicked at 'Squared n: \"\u003cerr message\u003e\"'`\n\n// You can access the underlying Result\u003cnumber, string\u003e using the `.inner` getter:\n// `equipped.inner`;\n\nconsole.log('Squared', squared);\n```\n\n## Option\n\n1. Option follows the same methods as Result does:\n\n```ts\nimport { Option } from 'rustic';\n\nfunction returnsOption(): Option\u003cnumber\u003e { ... }\n\nconst res: Option\u003cnumber\u003e = returnsOption();\n\nif (res == null) {\n\tconsole.log('Returned null');\n} else {\n\t// Typescript can infer for sure, that the res is of type `number`.\n\tconsole.log('Returned num:', res * 2);\n}\n```\n\n2. Call `equip` with the optional variable to gain access to the full functionality:\n\n```ts\nimport { Option, equip, OptionEquipped } from 'rustic';\n\nfunction returnsOption(): Option\u003cnumber\u003e { ... }\n\nconst res: OptionEquipped\u003cnumber\u003e = equip(returnsOption());\n\nconst squared = res.map(n =\u003e n * n);\n\n// Unwrap can lead to panics. You can still access the underlying Option\u003cnumber\u003e\n// by using `.inner`: `squared.inner`\nconsole.log('Sqared num:', squared.unwrap());\n```\n\n# Wrappers\n\n## catchResult\n```ts\nimport { catchResult } from 'rustic';\n\nfunction throwsError(): void { throw new Error('1234') }\n\nfunction doesNotThrowError(): number { return 5 }\n\nconst res1 = catchResult(throwsError);\t// Err('Error: 1234')\nconst res2 = catchResult(doesNotThrowError);\t// Ok(5)\n```\n\n## parseJson\n```ts\nimport { parseJson, Result } from 'rustic';\n\nconst parsed1: Result\u003cnumber, string\u003e = parseJson('5');\t// Ok(5)\n\nconst parsed2: Result\u003cnumber, string\u003e = parseJson('{');\t// Err('...')\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffraneklubi%2Frustic","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffraneklubi%2Frustic","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffraneklubi%2Frustic/lists"}