Skip to content

chore: clippy #124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions pbjson-build/src/descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,8 @@ impl TypePath {

/// Performs a prefix match, returning the length of the match in path segments if any
pub fn prefix_match(&self, prefix: &str) -> Option<usize> {
let prefix = match prefix.strip_prefix('.') {
Some(prefix) => prefix,
None => panic!("prefix must start with a '.'"),
let Some(prefix) = prefix.strip_prefix('.') else {
panic!("prefix must start with a '.'");
};

if prefix.is_empty() {
Expand Down Expand Up @@ -183,11 +182,11 @@ impl DescriptorSet {
let path = TypePath::new(package);

for descriptor in file.message_type {
self.register_message(&path, descriptor, syntax)
self.register_message(&path, descriptor, syntax);
}

for descriptor in file.enum_type {
self.register_enum(&path, descriptor)
self.register_enum(&path, descriptor);
}
}

Expand All @@ -200,11 +199,11 @@ impl DescriptorSet {
let child_path = path.child(name);

for child_descriptor in descriptor.enum_type {
self.register_enum(&child_path, child_descriptor)
self.register_enum(&child_path, child_descriptor);
}

for child_descriptor in descriptor.nested_type {
self.register_message(&child_path, child_descriptor, syntax)
self.register_message(&child_path, child_descriptor, syntax);
}

self.register_descriptor(
Expand Down
43 changes: 19 additions & 24 deletions pbjson-build/src/generator/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,25 +85,25 @@ fn write_field_empty_predicate<W: Write>(
(_, FieldModifier::Required) => unreachable!(),
(_, FieldModifier::Repeated)
| (FieldType::Map(_, _), _)
| (FieldType::Scalar(ScalarType::String), FieldModifier::UseDefault)
| (FieldType::Scalar(ScalarType::Bytes), FieldModifier::UseDefault) => {
| (FieldType::Scalar(ScalarType::String | ScalarType::Bytes), FieldModifier::UseDefault) => {
write!(writer, "!self.{}.is_empty()", member.rust_field_name())
}
(_, FieldModifier::Optional) | (FieldType::Message(_), _) => {
write!(writer, "self.{}.is_some()", member.rust_field_name())
}
(FieldType::Scalar(ScalarType::F64), FieldModifier::UseDefault)
| (FieldType::Scalar(ScalarType::F32), FieldModifier::UseDefault) => {
(FieldType::Scalar(ScalarType::F64 | ScalarType::F32), FieldModifier::UseDefault) => {
write!(writer, "self.{} != 0.", member.rust_field_name())
}
(FieldType::Scalar(ScalarType::Bool), FieldModifier::UseDefault) => {
write!(writer, "self.{}", member.rust_field_name())
}
(FieldType::Enum(_), FieldModifier::UseDefault)
| (FieldType::Scalar(ScalarType::I64), FieldModifier::UseDefault)
| (FieldType::Scalar(ScalarType::I32), FieldModifier::UseDefault)
| (FieldType::Scalar(ScalarType::U32), FieldModifier::UseDefault)
| (FieldType::Scalar(ScalarType::U64), FieldModifier::UseDefault) => {
(
FieldType::Enum(_)
| FieldType::Scalar(
ScalarType::I64 | ScalarType::I32 | ScalarType::U32 | ScalarType::U64,
),
FieldModifier::UseDefault,
) => {
write!(writer, "self.{} != 0", member.rust_field_name())
}
}
Expand Down Expand Up @@ -280,9 +280,7 @@ fn write_serialize_variable<W: Write>(
FieldType::Map(_, value_type)
if matches!(
value_type.as_ref(),
FieldType::Scalar(ScalarType::I64)
| FieldType::Scalar(ScalarType::U64)
| FieldType::Scalar(ScalarType::Bytes)
FieldType::Scalar(ScalarType::I64 | ScalarType::U64 | ScalarType::Bytes)
| FieldType::Enum(_)
) =>
{
Expand All @@ -294,7 +292,7 @@ fn write_serialize_variable<W: Write>(
)?;

match value_type.as_ref() {
FieldType::Scalar(ScalarType::I64) | FieldType::Scalar(ScalarType::U64) => {
FieldType::Scalar(ScalarType::I64 | ScalarType::U64) => {
writeln!(
writer,
"{}.map(|(k, v)| (k, v.to_string())).collect();",
Expand Down Expand Up @@ -631,7 +629,7 @@ fn write_deserialize_message<W: Write>(
field = field.rust_field_name()
)?;
}
_ => {
FieldModifier::Optional => {
writeln!(
writer,
"{indent}{field}: {field}__,",
Expand Down Expand Up @@ -1005,16 +1003,13 @@ fn write_encode_scalar_field<W: Write>(
field_modifier: FieldModifier,
writer: &mut W,
) -> Result<()> {
let deserializer = match override_deserializer(scalar) {
Some(deserializer) => deserializer,
None => {
return match field_modifier {
FieldModifier::Optional => {
write!(writer, "map_.next_value()?")
}
_ => write!(writer, "Some(map_.next_value()?)"),
};
}
let Some(deserializer) = override_deserializer(scalar) else {
return match field_modifier {
FieldModifier::Optional => {
write!(writer, "map_.next_value()?")
}
_ => write!(writer, "Some(map_.next_value()?)"),
};
};

writeln!(writer)?;
Expand Down
2 changes: 1 addition & 1 deletion pbjson-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ impl Builder {
&self.btree_map_paths,
self.emit_fields,
self.preserve_proto_field_names,
)?
)?;
}
}
}
Expand Down
13 changes: 6 additions & 7 deletions pbjson-build/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub enum ScalarType {
}

impl ScalarType {
pub fn rust_type(&self) -> &'static str {
pub fn rust_type(self) -> &'static str {
match self {
Self::F64 => "f64",
Self::F32 => "f32",
Expand All @@ -40,7 +40,7 @@ impl ScalarType {
}
}

pub fn is_numeric(&self) -> bool {
pub fn is_numeric(self) -> bool {
matches!(
self,
Self::F64 | Self::F32 | Self::I32 | Self::I64 | Self::U32 | Self::U64
Expand All @@ -65,7 +65,7 @@ pub enum FieldModifier {
}

impl FieldModifier {
pub fn is_required(&self) -> bool {
pub fn is_required(self) -> bool {
matches!(self, Self::Required)
}
}
Expand Down Expand Up @@ -167,7 +167,7 @@ pub fn resolve_message(
let name = descriptor.name.clone().expect("oneof with no name");
let path = message.path.child(TypeName::new(&name));

one_ofs.push(OneOf { name, path, fields })
one_ofs.push(OneOf { name, path, fields });
}
}

Expand Down Expand Up @@ -254,9 +254,8 @@ fn resolve_type(descriptors: &DescriptorSet, type_name: &str) -> FieldType {
assert_eq!("key", key.name());
assert_eq!("value", value.name());

let key_type = match field_type(descriptors, key) {
FieldType::Scalar(scalar) => scalar,
_ => panic!("non scalar map key"),
let FieldType::Scalar(key_type) = field_type(descriptors, key) else {
panic!("non scalar map key");
};
let value_type = field_type(descriptors, value);
FieldType::Map(key_type, Box::new(value_type))
Expand Down
2 changes: 1 addition & 1 deletion pbjson-build/src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl<'a> Resolver<'a> {

let mut ret = String::new();
for _ in 0..super_count {
ret.push_str("super::")
ret.push_str("super::");
}

(ret, shared_prefix)
Expand Down
Loading