Skip to content

Commit 00c9ee0

Browse files
committed
Feat: Added a FromStr implementation on every 'String type'. So now (e.g.) an ID can be parsed from a &str
1 parent 2f4b905 commit 00c9ee0

3 files changed

Lines changed: 37 additions & 4 deletions

File tree

lambda-appsync/src/aws_scalars/email.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ impl From<&str> for AWSEmail {
1818
Self(value.to_lowercase())
1919
}
2020
}
21+
impl core::str::FromStr for AWSEmail {
22+
type Err = core::convert::Infallible;
23+
fn from_str(s: &str) -> Result<Self, Self::Err> {
24+
Ok(Self::from(s))
25+
}
26+
}
2127
#[cfg(test)]
2228
mod tests {
2329
use super::*;

lambda-appsync/src/aws_scalars/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ macro_rules! impl_new_string {
3030
Self(value.to_owned())
3131
}
3232
}
33+
impl core::str::FromStr for $name {
34+
type Err = core::convert::Infallible;
35+
fn from_str(s: &str) -> Result<Self, Self::Err> {
36+
Ok(Self::from(s))
37+
}
38+
}
3339
};
3440
(into $name:ident) => {
3541
impl From<$name> for String {

lambda-appsync/src/id.rs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::ops::Deref;
2-
31
use serde::{Deserialize, Serialize};
42

53
/// A custom UUID-based identifier type for AppSync GraphQL objects.
@@ -53,7 +51,29 @@ impl TryFrom<String> for ID {
5351
/// - The string contains invalid characters
5452
/// - The string is not of the correct length
5553
fn try_from(value: String) -> Result<Self, Self::Error> {
56-
Ok(ID(uuid::Uuid::parse_str(&value)?))
54+
value.parse()
55+
}
56+
}
57+
58+
impl core::str::FromStr for ID {
59+
type Err = uuid::Error;
60+
61+
/// Attempts to create an ID from a string representation of a UUID.
62+
///
63+
/// # Example
64+
/// ```
65+
/// use lambda_appsync::ID;
66+
///
67+
/// let id: ID = "123e4567-e89b-12d3-a456-426614174000".parse().unwrap();
68+
/// ```
69+
///
70+
/// # Errors
71+
/// Returns a `uuid::Error` if:
72+
/// - The string is not a valid UUID format
73+
/// - The string contains invalid characters
74+
/// - The string is not of the correct length
75+
fn from_str(s: &str) -> Result<Self, Self::Err> {
76+
Ok(ID(uuid::Uuid::parse_str(s)?))
5777
}
5878
}
5979
impl core::fmt::Display for ID {
@@ -66,7 +86,7 @@ impl From<ID> for String {
6686
value.to_string()
6787
}
6888
}
69-
impl Deref for ID {
89+
impl core::ops::Deref for ID {
7090
type Target = uuid::Uuid;
7191

7292
fn deref(&self) -> &Self::Target {
@@ -76,6 +96,7 @@ impl Deref for ID {
7696
#[cfg(test)]
7797
mod tests {
7898
use super::*;
99+
use core::ops::Deref;
79100

80101
#[test]
81102
fn test_id_creation() {

0 commit comments

Comments
 (0)