Skip to content

Commit

Permalink
Merge pull request #610 from pintariching/formating-codebase
Browse files Browse the repository at this point in the history
formatting changes
  • Loading branch information
KodrAus committed Aug 22, 2022
2 parents 7188b00 + 4a22668 commit f1d19d4
Show file tree
Hide file tree
Showing 15 changed files with 115 additions and 261 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Expand Up @@ -82,7 +82,7 @@ jobs:
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: 1.46.0
toolchain: 1.57.0
override: true

- name: Version features
Expand Down
9 changes: 0 additions & 9 deletions .rustfmt.toml

This file was deleted.

17 changes: 3 additions & 14 deletions examples/windows_guid.rs
Expand Up @@ -20,12 +20,7 @@ fn guid_to_uuid() {
Data4: [0x86, 0x47, 0x9d, 0xc5, 0x4e, 0x1e, 0xe1, 0xe8],
};

let uuid = Uuid::from_fields(
guid_in.Data1,
guid_in.Data2,
guid_in.Data3,
&guid_in.Data4,
);
let uuid = Uuid::from_fields(guid_in.Data1, guid_in.Data2, guid_in.Data3, &guid_in.Data4);

let guid_out = {
let fields = uuid.as_fields();
Expand Down Expand Up @@ -66,12 +61,7 @@ fn guid_to_uuid_le_encoded() {
Data4: [0x86, 0x47, 0x9d, 0xc5, 0x4e, 0x1e, 0xe1, 0xe8],
};

let uuid = Uuid::from_fields_le(
guid_in.Data1,
guid_in.Data2,
guid_in.Data3,
&guid_in.Data4,
);
let uuid = Uuid::from_fields_le(guid_in.Data1, guid_in.Data2, guid_in.Data3, &guid_in.Data4);

let guid_out = {
let fields = uuid.to_fields_le();
Expand Down Expand Up @@ -107,8 +97,7 @@ fn uuid_from_cocreateguid() {
CoCreateGuid(&mut guid as *mut _);
}

let uuid =
Uuid::from_fields(guid.Data1, guid.Data2, guid.Data3, &guid.Data4);
let uuid = Uuid::from_fields(guid.Data1, guid.Data2, guid.Data3, &guid.Data4);

assert_eq!(Variant::RFC4122, uuid.get_variant());
assert_eq!(Some(Version::Random), uuid.get_version());
Expand Down
51 changes: 24 additions & 27 deletions macros/src/lib.rs
Expand Up @@ -12,7 +12,7 @@ use proc_macro::TokenStream;
use proc_macro2::TokenStream as TokenStream2;
use quote::{quote, quote_spanned};
use std::fmt;
use syn::{LitStr, spanned::Spanned};
use syn::{spanned::Spanned, LitStr};

mod error;
mod parser;
Expand All @@ -22,32 +22,29 @@ mod parser;
pub fn parse_lit(input: TokenStream) -> TokenStream {
build_uuid(input.clone()).unwrap_or_else(|e| {
let msg = e.to_string();
let span =
match e {
Error::UuidParse(lit, error::Error(error::ErrorKind::Char {
character,
index,
})) => {
let mut bytes = character as u32;
let mut width = 0;
while bytes != 0 {
bytes >>= 4;
width += 1;
}
let mut s = proc_macro2::Literal::string("");
s.set_span(lit.span());
s.subspan(index..index + width - 1)
let span = match e {
Error::UuidParse(lit, error::Error(error::ErrorKind::Char { character, index })) => {
let mut bytes = character as u32;
let mut width = 0;
while bytes != 0 {
bytes >>= 4;
width += 1;
}
Error::UuidParse(lit, error::Error(
error::ErrorKind::GroupLength { index, len, .. },
)) => {
let mut s = proc_macro2::Literal::string("");
s.set_span(lit.span());
s.subspan(index..index + len)
}
_ => None,
let mut s = proc_macro2::Literal::string("");
s.set_span(lit.span());
s.subspan(index..index + width - 1)
}
Error::UuidParse(
lit,
error::Error(error::ErrorKind::GroupLength { index, len, .. }),
) => {
let mut s = proc_macro2::Literal::string("");
s.set_span(lit.span());
s.subspan(index..index + len)
}
.unwrap_or_else(|| TokenStream2::from(input).span());
_ => None,
}
.unwrap_or_else(|| TokenStream2::from(input).span());

TokenStream::from(quote_spanned! {span=>
compile_error!(#msg)
Expand Down Expand Up @@ -75,8 +72,8 @@ fn build_uuid(input: TokenStream) -> Result<TokenStream, Error> {
_ => return Err(Error::NonStringLiteral),
};

let bytes = parser::try_parse(&str_lit.value())
.map_err(|e| Error::UuidParse(str_lit, e.into_err()))?;
let bytes =
parser::try_parse(&str_lit.value()).map_err(|e| Error::UuidParse(str_lit, e.into_err()))?;

let tokens = bytes
.iter()
Expand Down
7 changes: 3 additions & 4 deletions macros/src/parser.rs
Expand Up @@ -22,10 +22,9 @@ pub const fn try_parse(input: &str) -> Result<[u8; 16], InvalidUuid> {
// - `UUID` for a regular hyphenated UUID
(36, s)
| (38, [b'{', s @ .., b'}'])
| (
45,
[b'u', b'r', b'n', b':', b'u', b'u', b'i', b'd', b':', s @ ..],
) => parse_hyphenated(s),
| (45, [b'u', b'r', b'n', b':', b'u', b'u', b'i', b'd', b':', s @ ..]) => {
parse_hyphenated(s)
}
// Any other shaped input is immediately invalid
_ => Err(()),
};
Expand Down
18 changes: 4 additions & 14 deletions src/builder.rs
Expand Up @@ -131,12 +131,7 @@ impl Uuid {
/// uuid.hyphenated().to_string(),
/// );
/// ```
pub const fn from_fields_le(
d1: u32,
d2: u16,
d3: u16,
d4: &[u8; 8],
) -> Uuid {
pub const fn from_fields_le(d1: u32, d2: u16, d3: u16, d4: &[u8; 8]) -> Uuid {
Uuid::from_bytes([
d1 as u8,
(d1 >> 8) as u8,
Expand Down Expand Up @@ -415,8 +410,8 @@ impl Uuid {
/// ```
pub const fn from_bytes_le(b: Bytes) -> Uuid {
Uuid([
b[3], b[2], b[1], b[0], b[5], b[4], b[7], b[6], b[8], b[9], b[10],
b[11], b[12], b[13], b[14], b[15],
b[3], b[2], b[1], b[0], b[5], b[4], b[7], b[6], b[8], b[9], b[10], b[11], b[12], b[13],
b[14], b[15],
])
}

Expand Down Expand Up @@ -670,12 +665,7 @@ impl Builder {
/// "a4a3a2a1-b2b1-c2c1-d1d2-d3d4d5d6d7d8"
/// );
/// ```
pub const fn from_fields_le(
d1: u32,
d2: u16,
d3: u16,
d4: &[u8; 8],
) -> Self {
pub const fn from_fields_le(d1: u32, d2: u16, d3: u16, d4: &[u8; 8]) -> Self {
Builder(Uuid::from_fields_le(d1, d2, d3, d4))
}

Expand Down
4 changes: 1 addition & 3 deletions src/external/arbitrary_support.rs
Expand Up @@ -21,9 +21,7 @@ mod tests {

#[test]
fn test_arbitrary() {
let mut bytes = Unstructured::new(&[
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
]);
let mut bytes = Unstructured::new(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);

let uuid = Uuid::arbitrary(&mut bytes).unwrap();

Expand Down
68 changes: 14 additions & 54 deletions src/external/serde_support.rs
Expand Up @@ -21,60 +21,41 @@ use serde::{
};

impl Serialize for Uuid {
fn serialize<S: Serializer>(
&self,
serializer: S,
) -> Result<S::Ok, S::Error> {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
if serializer.is_human_readable() {
serializer.serialize_str(
self.hyphenated().encode_lower(&mut Uuid::encode_buffer()),
)
serializer.serialize_str(self.hyphenated().encode_lower(&mut Uuid::encode_buffer()))
} else {
serializer.serialize_bytes(self.as_bytes())
}
}
}

impl Serialize for Hyphenated {
fn serialize<S: Serializer>(
&self,
serializer: S,
) -> Result<S::Ok, S::Error> {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(self.encode_lower(&mut Uuid::encode_buffer()))
}
}

impl Serialize for Simple {
fn serialize<S: Serializer>(
&self,
serializer: S,
) -> Result<S::Ok, S::Error> {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(self.encode_lower(&mut Uuid::encode_buffer()))
}
}

impl Serialize for Urn {
fn serialize<S: Serializer>(
&self,
serializer: S,
) -> Result<S::Ok, S::Error> {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(self.encode_lower(&mut Uuid::encode_buffer()))
}
}

impl Serialize for Braced {
fn serialize<S: Serializer>(
&self,
serializer: S,
) -> Result<S::Ok, S::Error> {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(self.encode_lower(&mut Uuid::encode_buffer()))
}
}

impl<'de> Deserialize<'de> for Uuid {
fn deserialize<D: Deserializer<'de>>(
deserializer: D,
) -> Result<Self, D::Error> {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
fn de_error<E: de::Error>(e: Error) -> E {
E::custom(format_args!("UUID parsing failed: {}", e))
}
Expand All @@ -85,24 +66,15 @@ impl<'de> Deserialize<'de> for Uuid {
impl<'vi> de::Visitor<'vi> for UuidVisitor {
type Value = Uuid;

fn expecting(
&self,
formatter: &mut fmt::Formatter<'_>,
) -> fmt::Result {
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(formatter, "a UUID string")
}

fn visit_str<E: de::Error>(
self,
value: &str,
) -> Result<Uuid, E> {
fn visit_str<E: de::Error>(self, value: &str) -> Result<Uuid, E> {
value.parse::<Uuid>().map_err(de_error)
}

fn visit_bytes<E: de::Error>(
self,
value: &[u8],
) -> Result<Uuid, E> {
fn visit_bytes<E: de::Error>(self, value: &[u8]) -> Result<Uuid, E> {
Uuid::from_slice(value).map_err(de_error)
}

Expand Down Expand Up @@ -141,17 +113,11 @@ impl<'de> Deserialize<'de> for Uuid {
impl<'vi> de::Visitor<'vi> for UuidBytesVisitor {
type Value = Uuid;

fn expecting(
&self,
formatter: &mut fmt::Formatter<'_>,
) -> fmt::Result {
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(formatter, "bytes")
}

fn visit_bytes<E: de::Error>(
self,
value: &[u8],
) -> Result<Uuid, E> {
fn visit_bytes<E: de::Error>(self, value: &[u8]) -> Result<Uuid, E> {
Uuid::from_slice(value).map_err(de_error)
}
}
Expand All @@ -169,10 +135,7 @@ pub mod compact {
/// Serialize from a [`Uuid`] as a `[u8; 16]`
///
/// [`Uuid`]: ../../struct.Uuid.html
pub fn serialize<S>(
u: &crate::Uuid,
serializer: S,
) -> Result<S::Ok, S::Error>
pub fn serialize<S>(u: &crate::Uuid, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
Expand Down Expand Up @@ -292,10 +255,7 @@ mod serde_tests {
let uuid_bytes = b"F9168C5E-CEB2-4F";
let u = Uuid::from_slice(uuid_bytes).unwrap();

serde_test::assert_de_tokens(
&u.readable(),
&[serde_test::Token::Bytes(uuid_bytes)],
);
serde_test::assert_de_tokens(&u.readable(), &[serde_test::Token::Bytes(uuid_bytes)]);
}

#[test]
Expand Down

0 comments on commit f1d19d4

Please sign in to comment.