| Crates.io | simple-newtype |
| lib.rs | simple-newtype |
| version | 0.1.2 |
| created_at | 2025-11-16 09:00:14.07201+00 |
| updated_at | 2025-11-16 09:12:14.978263+00 |
| description | Simple newtype macros for application development. |
| homepage | |
| repository | https://github.com/ecociel/newtype-rs |
| max_upload_size | |
| id | 1935357 |
| size | 19,859 |
A very simple newtype macro for Rust applications
use simple_newtype::{simple_string_newtype, ParseError};
use std::str::FromStr;
use uuid::Uuid;
simple_string_newtype!(UserId(String));
impl UserId {
pub fn new() -> Self {
UserId(Uuid::new_v4().into())
}
}
impl FromStr for UserId {
type Err = ParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match uuid::Uuid::parse_str(s) {
Ok(uuid) => Ok(UserId(uuid.into())),
Err(_) => Err(ParseError::invalid_syntax(
"UserId",
s,
"not a valid user ID",
)),
}
}
}
fn main() {
let user_id = UserId::new();
println!("Hello {}", user_id);
let not_a_uuid = String::from("not a valid uuid");
match UserId::try_from(not_a_uuid) {
Ok(uid) => println!("Hello {}", uid),
Err(e) => println!("Not a valid user id: {:?}", e),
}
}