{"id":26930282,"url":"https://github.com/limitcool/mod","last_synced_at":"2025-04-02T06:18:14.310Z","repository":{"id":243696801,"uuid":"813175660","full_name":"limitcool/mod","owner":"limitcool","description":null,"archived":false,"fork":false,"pushed_at":"2024-06-11T14:12:12.000Z","size":6,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-28T07:16:43.898Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/limitcool.png","metadata":{"files":{"readme":"README-en.md","changelog":null,"contributing":null,"funding":null,"license":null,"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-06-10T15:59:03.000Z","updated_at":"2024-12-19T08:29:38.000Z","dependencies_parsed_at":"2024-06-10T18:47:28.326Z","dependency_job_id":"62b882a1-ff96-478d-9bd3-899518382fd9","html_url":"https://github.com/limitcool/mod","commit_stats":null,"previous_names":["limitcool/mod"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/limitcool%2Fmod","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/limitcool%2Fmod/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/limitcool%2Fmod/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/limitcool%2Fmod/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/limitcool","download_url":"https://codeload.github.com/limitcool/mod/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246763881,"owners_count":20829800,"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":"2025-04-02T06:18:13.801Z","updated_at":"2025-04-02T06:18:14.300Z","avatar_url":"https://github.com/limitcool.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mod\n\nEnglish| [简体中文](README.md)\n\n[![crates.io](https://img.shields.io/crates/v/mod.svg)](https://crates.io/crates/mod)\n[![Docs](https://docs.rs/mod/badge.svg)](https://docs.rs/mod)\n![MSRV](https://img.shields.io/badge/rustc-1.78.0+-ab6000.svg)\n\n## Introduction\n\n`mod` is a Rust project that provides two simple and useful utility functions for converting between `Option` and `Result` types. The project includes two main functions:\n\n1. `option_to_result\u003cT, E\u003e`: Converts an `Option\u003cT\u003e` to a `Result\u003cT, E\u003e`.\n2. `result_to_option\u003cT, E\u003e`: Converts a `Result\u003cT, E\u003e` to an `Option\u003cT\u003e`.\n\n## Installation\n\nYou can add this project to your Rust project by running the following command:\n\n```sh\ncargo add mod\n```\n\n## Usage\n\nFirst, ensure that you include the `mod` crate in your project:\n\n```rust\nuse r#mod::{option_to_result, result_to_option};\n```\n\n### Function Descriptions\n\n#### `option_to_result`\n\nConverts an `Option\u003cT\u003e` to a `Result\u003cT, E\u003e`. If the `Option` is `Some`, it returns the value wrapped in `Ok`; if it is `None`, it returns the specified error.\n\n```rust\npub fn option_to_result\u003cT, E\u003e(option: Option\u003cT\u003e, err: E) -\u003e Result\u003cT, E\u003e\nwhere\n    E: Clone,\n{\n    match option {\n        Some(value) =\u003e Ok(value),\n        None =\u003e Err(err),\n    }\n}\n```\n\n**Example**:\n\n```rust\nlet some_value = Some(42);\nlet none_value: Option\u003ci32\u003e = None;\nlet error = \"Error\";\n\nassert_eq!(option_to_result(some_value, error.to_string()), Ok(42));\nassert_eq!(option_to_result(none_value, error.to_string()), Err(\"Error\".to_string()));\n```\n\n#### `result_to_option`\n\nConverts a `Result\u003cT, E\u003e` to an `Option\u003cT\u003e`. If the `Result` is `Ok`, it returns the value wrapped in `Some`; if it is `Err`, it returns `None`.\n\n```rust\npub fn result_to_option\u003cT, E\u003e(result: Result\u003cT, E\u003e) -\u003e Option\u003cT\u003e\nwhere\n    E: Clone,\n{\n    match result {\n        Ok(value) =\u003e Some(value),\n        Err(_) =\u003e None,\n    }\n}\n```\n\n**Example**:\n\n```rust\nlet ok_result = Ok::\u003ci32, \u0026str\u003e(42);\nlet err_result = Err::\u003ci32, \u0026str\u003e(\"Error\");\n\nassert_eq!(result_to_option(ok_result), Some(42));\nassert_eq!(result_to_option(err_result), None);\n```\n\n## Testing\n\nThis project includes some unit tests to verify the correctness of the functionality. You can run the tests by executing the following command:\n\n```sh\ncargo test\n```\n\nThe test cases are located in the `tests` module and cover the `option_to_result` and `result_to_option` functions:\n\n```rust\n#[cfg(test)]\nmod tests {\n    use super::*;\n\n    #[test]\n    fn test_option_to_result() {\n        let some_value = Some(42);\n        let none_value: Option\u003ci32\u003e = None;\n        let error = \"Error\";\n\n        assert_eq!(option_to_result(some_value, error.to_string()), Ok(42));\n        assert_eq!(option_to_result(none_value, error.to_string()), Err(\"Error\".to_string()));\n    }\n\n    #[test]\n    fn test_result_to_option() {\n        let ok_result = Ok::\u003ci32, \u0026str\u003e(42);\n        let err_result = Err::\u003ci32, \u0026str\u003e(\"Error\");\n\n        assert_eq!(result_to_option(ok_result), Some(42));\n        assert_eq!(result_to_option(err_result), None);\n    }\n}\n```\n\n## License\n\nThis project is licensed under the GPL license. See the LICENSE file for details.\n\n## Contribution\n\nContributions are welcome! Please submit issues or pull requests.\n\n---\n\nThank you for using `mod`! If you have any questions or suggestions, feel free to contact us.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flimitcool%2Fmod","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flimitcool%2Fmod","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flimitcool%2Fmod/lists"}