Skip to content

Commit

Permalink
Merge pull request #806 from tysen/add-fromstr-impls-to-fmt
Browse files Browse the repository at this point in the history
Add FromStr impls to the fmt structs
  • Loading branch information
KodrAus authored Feb 20, 2025
2 parents d8871b3 + 5b0ca42 commit 6bd7bc7
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
44 changes: 43 additions & 1 deletion src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@

//! Adapters for alternative string formats.
use core::str::FromStr;

use crate::{
std::{borrow::Borrow, fmt, ptr, str},
Uuid, Variant,
Error, Uuid, Variant,
};

#[cfg(feature = "std")]
Expand Down Expand Up @@ -829,6 +831,46 @@ impl Urn {
}
}

impl FromStr for Hyphenated {
type Err = Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
crate::parser::parse_hyphenated(s.as_bytes())
.map(|b| Hyphenated(Uuid(b)))
.map_err(|invalid| invalid.into_err())
}
}

impl FromStr for Simple {
type Err = Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
crate::parser::parse_simple(s.as_bytes())
.map(|b| Simple(Uuid(b)))
.map_err(|invalid| invalid.into_err())
}
}

impl FromStr for Urn {
type Err = Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
crate::parser::parse_urn(s.as_bytes())
.map(|b| Urn(Uuid(b)))
.map_err(|invalid| invalid.into_err())
}
}

impl FromStr for Braced {
type Err = Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
crate::parser::parse_braced(s.as_bytes())
.map(|b| Braced(Uuid(b)))
.map_err(|invalid| invalid.into_err())
}
}

macro_rules! impl_fmt_traits {
($($T:ident<$($a:lifetime),*>),+) => {$(
impl<$($a),*> fmt::Display for $T<$($a),*> {
Expand Down
2 changes: 1 addition & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ pub(crate) const fn parse_simple(s: &[u8]) -> Result<[u8; 16], InvalidUuid> {
}

#[inline]
const fn parse_hyphenated(s: &[u8]) -> Result<[u8; 16], InvalidUuid> {
pub(crate) const fn parse_hyphenated(s: &[u8]) -> Result<[u8; 16], InvalidUuid> {
// This length check here removes all other bounds
// checks in this function
if s.len() != 36 {
Expand Down
8 changes: 6 additions & 2 deletions src/rng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,11 @@ mod imp {

use super::*;

#[cfg(all(not(feature = "js"), not(feature = "rng-getrandom"), not(feature = "rng-rand")))]
#[cfg(all(
not(feature = "js"),
not(feature = "rng-getrandom"),
not(feature = "rng-rand")
))]
compile_error!("to use `uuid` on `wasm32-unknown-unknown`, specify a source of randomness using one of the `js`, `rng-getrandom`, or `rng-rand` features");

// Using `rand`
Expand Down Expand Up @@ -249,7 +253,7 @@ mod imp {
*/

use wasm_bindgen::{prelude::wasm_bindgen, JsValue};

#[cfg(target_feature = "atomics")]
use core::convert::TryInto;

Expand Down

0 comments on commit 6bd7bc7

Please sign in to comment.