Skip to content
This repository has been archived by the owner on Jan 17, 2024. It is now read-only.

Commit

Permalink
Make clippy happy
Browse files Browse the repository at this point in the history
  • Loading branch information
olanod committed Dec 21, 2022
1 parent 5f0f6e4 commit cc3ae2a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 34 deletions.
14 changes: 7 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl From<(&Type, &PortableRegistry)> for SpecificType {
)
}
}
Def::Variant(v) => Self::Variant(name.into(), v.variants().into(), None),
Def::Variant(v) => Self::Variant(name, v.variants().into(), None),
Def::Sequence(s) => {
let ty = s.type_param();
if matches!(
Expand Down Expand Up @@ -181,7 +181,7 @@ impl SpecificType {
SpecificType::Variant(_, ref mut variants, idx @ None) => {
let i = variants
.iter()
.map(|v| get_field(v))
.map(get_field)
.position(|f| f.as_ref() == selection.as_ref())? as u8;
variants.retain(|v| v.index() == i);
*idx = Some(i);
Expand Down Expand Up @@ -222,26 +222,26 @@ impl<'a> From<&'a SpecificType> for EnumVariant<'a> {
if name == "Option" && vname == "None" {
Self::OptionNone
} else {
Self::Unit(*idx, &vname)
Self::Unit(*idx, vname)
}
} else if is_tuple!(variant) {
if fields.len() == 1 {
let ty = fields.first().map(|f| f.ty().id()).unwrap();
return if name == "Option" && variant.name() == &"Some" {
return if name == "Option" && variant.name() == "Some" {
Self::OptionSome(ty)
} else {
Self::NewType(*idx, &vname, ty)
Self::NewType(*idx, vname, ty)
};
} else {
let fields = fields.iter().map(|f| f.ty().id()).collect();
Self::Tuple(*idx, &vname, fields)
Self::Tuple(*idx, vname, fields)
}
} else {
let fields = fields
.iter()
.map(|f| (f.name().unwrap().deref(), f.ty().id()))
.collect();
Self::Struct(*idx, &vname, fields)
Self::Struct(*idx, vname, fields)
}
}
_ => panic!("Only for enum variants"),
Expand Down
50 changes: 29 additions & 21 deletions src/serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ where
Some(SpecificType::U16) => self.serialize_u16(v as u16)?,
Some(SpecificType::U32) => self.serialize_u32(v as u32)?,
Some(SpecificType::U64) => self.serialize_u64(v as u64)?,
Some(SpecificType::Compact(ty)) => self.serialize_compact(ty, v as u128)?,
Some(SpecificType::Compact(ty)) => self.serialize_compact(ty, v)?,
_ => self.out.put_u128_le(v),
}
Ok(())
Expand Down Expand Up @@ -409,7 +409,7 @@ where
}
}

impl<'a, 'reg, B> Serializer<'reg, B>
impl<'reg, B> Serializer<'reg, B>
where
B: BufMut + Debug,
{
Expand Down Expand Up @@ -453,49 +453,58 @@ where
},
Some(SpecificType::U8) => {
let n = val.parse().map_err(|_| Error::BadInput("u8".into()))?;
Ok(Some(self.out.put_u8(n)))
self.out.put_u8(n);
Ok(Some(()))
}
Some(SpecificType::U16) => {
let n = val.parse().map_err(|_| Error::BadInput("u16".into()))?;
Ok(Some(self.out.put_u16_le(n)))
self.out.put_u16_le(n);
Ok(Some(()))
}
Some(SpecificType::U32) => {
let n = val.parse().map_err(|_| Error::BadInput("u32".into()))?;
Ok(Some(self.out.put_u32_le(n)))
self.out.put_u32_le(n);
Ok(Some(()))
}
Some(SpecificType::U64) => {
let n = val.parse().map_err(|_| Error::BadInput("u64".into()))?;
Ok(Some(self.out.put_u64_le(n)))
self.out.put_u64_le(n);
Ok(Some(()))
}
Some(SpecificType::U128) => {
let n = val.parse().map_err(|_| Error::BadInput("u128".into()))?;
Ok(Some(self.out.put_u128_le(n)))
self.out.put_u128_le(n);
Ok(Some(()))
}
Some(SpecificType::I8) => {
let n = val.parse().map_err(|_| Error::BadInput("i8".into()))?;
Ok(Some(self.out.put_i8(n)))
self.out.put_i8(n);
Ok(Some(()))
}
Some(SpecificType::I16) => {
let n = val.parse().map_err(|_| Error::BadInput("i16".into()))?;
Ok(Some(self.out.put_i16_le(n)))
self.out.put_i16_le(n);
Ok(Some(()))
}
Some(SpecificType::I32) => {
let n = val.parse().map_err(|_| Error::BadInput("i32".into()))?;
Ok(Some(self.out.put_i32_le(n)))
self.out.put_i32_le(n);
Ok(Some(()))
}
Some(SpecificType::I64) => {
let n = val.parse().map_err(|_| Error::BadInput("i64".into()))?;
Ok(Some(self.out.put_i64_le(n)))
self.out.put_i64_le(n);
Ok(Some(()))
}
Some(SpecificType::I128) => {
let n = val.parse().map_err(|_| Error::BadInput("i128".into()))?;
Ok(Some(self.out.put_i128_le(n)))
self.out.put_i128_le(n);
Ok(Some(()))
}
#[cfg(feature = "hex")]
Some(SpecificType::Bytes(_)) => {
if val.starts_with("0x") {
let bytes =
hex::decode(&val[2..]).map_err(|e| Error::BadInput(e.to_string()))?;
if let Some(bytes) = val.strip_prefix("0x") {
let bytes = hex::decode(bytes).map_err(|e| Error::BadInput(e.to_string()))?;
ser::Serializer::serialize_bytes(self, &bytes)?;
Ok(Some(()))
} else {
Expand Down Expand Up @@ -614,8 +623,7 @@ where
let ty = ser.resolve(ty_id);

ser.ty = Some(if let SpecificType::StructNewType(ty_id) = ty {
let ty = ser.resolve(ty_id);
ty
ser.resolve(ty_id)
} else {
ty
});
Expand Down Expand Up @@ -1148,13 +1156,13 @@ mod tests {
#[test]
fn json_mix() -> Result<()> {
#[derive(Debug, Serialize, Encode, TypeInfo)]
struct Foo<'a> {
struct Foo {
a: Vec<String>,
b: (Bar<'a>, Bar<'a>, Bar<'a>),
b: (Bar, Bar, Bar),
}
#[derive(Debug, Serialize, Encode, TypeInfo)]
enum Bar<'a> {
A { thing: &'a str },
enum Bar {
A { thing: &'static str },
B(Baz),
C(BTreeMap<String, bool>, i64),
}
Expand Down
12 changes: 6 additions & 6 deletions src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,9 @@ impl<'reg> core::fmt::Display for Value<'reg> {
}

#[cfg(feature = "json")]
impl<'reg> Into<crate::JsonValue> for Value<'reg> {
fn into(self) -> crate::JsonValue {
serde_json::value::to_value(self).unwrap()
impl<'reg> From<Value<'reg>> for crate::JsonValue {
fn from(val: Value<'reg>) -> Self {
serde_json::value::to_value(val).unwrap()
}
}

Expand Down Expand Up @@ -628,15 +628,15 @@ mod tests {
#[test]
fn serialize_tuple_struct() -> Result<(), Error> {
#[derive(Encode, Serialize, TypeInfo)]
struct Foo<'a>([u8; 4], (bool, Option<()>), Baz<'a>, Baz<'a>);
struct Foo([u8; 4], (bool, Option<()>), Baz, Baz);

#[derive(Encode, Serialize, TypeInfo)]
struct Bar;

#[derive(Encode, Serialize, TypeInfo)]
enum Baz<'a> {
enum Baz {
A(Bar),
B { bb: &'a str },
B { bb: &'static str },
}

let in_value = Foo(
Expand Down

0 comments on commit cc3ae2a

Please sign in to comment.