{"id":13502901,"url":"https://github.com/teenjuna/prae","last_synced_at":"2025-03-29T12:33:11.739Z","repository":{"id":43189710,"uuid":"380144403","full_name":"teenjuna/prae","owner":"teenjuna","description":"prae is a crate that aims to provide a better way to define types that require validation.","archived":false,"fork":false,"pushed_at":"2023-01-31T14:29:18.000Z","size":85,"stargazers_count":129,"open_issues_count":6,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-04-24T18:54:39.711Z","etag":null,"topics":["macros","rust","rust-library","validation"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"unlicense","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/teenjuna.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-06-25T06:22:32.000Z","updated_at":"2024-03-18T14:08:29.000Z","dependencies_parsed_at":"2023-02-16T20:01:29.092Z","dependency_job_id":null,"html_url":"https://github.com/teenjuna/prae","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/teenjuna%2Fprae","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/teenjuna%2Fprae/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/teenjuna%2Fprae/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/teenjuna%2Fprae/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/teenjuna","download_url":"https://codeload.github.com/teenjuna/prae/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246058267,"owners_count":20717061,"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":["macros","rust","rust-library","validation"],"created_at":"2024-07-31T22:02:29.218Z","updated_at":"2025-03-29T12:33:11.416Z","avatar_url":"https://github.com/teenjuna.png","language":"Rust","funding_links":[],"categories":["Rust"],"sub_categories":[],"readme":"# prae\n\n[![crates.io version](https://shields.io/crates/v/prae)](https://crates.io/crates/prae)\n[![docs.rs](https://docs.rs/prae/badge.svg)](https://docs.rs/prae)\n[![crates.io license](https://shields.io/crates/l/prae)](https://crates.io/crates/prae)\n\n`prae` is a crate that aims to provide a better way to define types that\nrequire validation.\n\nThe main concept of the library is the [`Wrapper`](crate::Wrapper) trait.\nThis trait describes a\n[`Newtype`](https://rust-unofficial.github.io/patterns/patterns/behavioural/newtype.html)\nwrapper struct that contains some inner value and provides methods to\nconstruct, read and mutate it.\n\nThe easiest way to create a type that implements [`Wrapper`](crate::Wrapper)\nis to use [`define!`](crate::define) and [`extend!`](crate::extend) macros.\n\n## Example\nSuppose you want to create a type `Username`. You want this type to be a\n`String`, and you don't want it to be empty. Traditionally, you would create\na wrapper struct with getter and setter functions, like this simplified\nexample:\n```rust\n#[derive(Debug)]\npub struct Username(String);\n\nimpl Username {\n    pub fn new(username: \u0026str) -\u003e Result\u003cSelf, \u0026'static str\u003e {\n        let username = username.trim().to_owned();\n        if username.is_empty() {\n            Err(\"value is invalid\")\n        } else {\n            Ok(Self(username))\n        }\n    }\n\n    pub fn get(\u0026self) -\u003e \u0026str {\n        \u0026self.0\n    }\n\n    pub fn set(\u0026mut self, username: \u0026str) -\u003e Result\u003c(), \u0026'static str\u003e {\n        let username = username.trim().to_owned();\n        if username.is_empty() {\n            Err(\"value is invalid\")\n        } else {\n            self.0 = username;\n            Ok(())\n        }\n   }\n}\n\nlet username = Username::new(\" my username \").unwrap();\nassert_eq!(username.get(), \"my username\");\n\nlet err = Username::new(\"  \").unwrap_err();\nassert_eq!(err, \"value is invalid\");\n```\n\nUsing `prae`, you will do it like this:\n```rust\nuse prae::Wrapper;\n\nprae::define! {\n    #[derive(Debug)]\n    pub Username: String;\n    adjust |username| *username = username.trim().to_owned();\n    ensure |username| !username.is_empty();\n}\n\nlet username = Username::new(\" my username \").unwrap();\nassert_eq!(username.get(), \"my username\");\n\nlet err = Username::new(\"  \").unwrap_err();\nassert_eq!(err.original, \"value is invalid\");\nassert_eq!(err.value, \"\");\n```\n\nFuthermore, `prae` allows you to use custom errors and extend your types.\nSee docs for [`define!`](crate::define) and [`extend!`](crate::define) for\nmore information and examples.\n\n## Compilation speed\n\nThe macros provided by this crate are declarative, therefore make almost\nzero impact on the compilation speed.\n\n## Performarnce impact\n\nIf you find yourself in a situation where the internal adjustment and\nvalidation of your type becomes a performance bottleneck (for example, you\nperform a heavy validation and mutate your type in a hot loop) - try\n`_unprocessed` variants of [`Wrapper`] methods. They won't call\n[`Wrapper::PROCESS`]. However, I strongly advise you to call\n[`Wrapper::verify`] after such operations.\n\n## Feature flags\n\n `prae` provides additional features:\n\n Name | Description\n ---|---\n `serde` | Adds the [`impl_serde`] plugin.\n\n## Credits\nThis crate was highly inspired by the\n[tightness](https://github.com/PabloMansanet/tightness) crate. It's basically\njust a fork of tightness with a slightly different philosophy.\nSee [this](https://github.com/PabloMansanet/tightness/issues/2) issue for details.\n\nLicense: Unlicense\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fteenjuna%2Fprae","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fteenjuna%2Fprae","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fteenjuna%2Fprae/lists"}