Skip to content
Merged
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
10 changes: 3 additions & 7 deletions prost-reflect-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,10 @@ impl Builder {
let descriptor = DescriptorPool::decode(buf.as_ref()).expect("Invalid file descriptor");

let pool_attribute = if let Some(descriptor_pool) = &self.descriptor_pool_expr {
format!(
r#"#[prost_reflect(descriptor_pool = "{}")]"#,
descriptor_pool,
)
format!(r#"#[prost_reflect(descriptor_pool = "{descriptor_pool}")]"#,)
} else if let Some(file_descriptor_set_bytes) = &self.file_descriptor_set_bytes_expr {
format!(
r#"#[prost_reflect(file_descriptor_set_bytes = "{}")]"#,
file_descriptor_set_bytes,
r#"#[prost_reflect(file_descriptor_set_bytes = "{file_descriptor_set_bytes}")]"#,
)
} else {
return Err(io::Error::other(
Expand All @@ -173,7 +169,7 @@ impl Builder {
.type_attribute(full_name, "#[derive(::prost_reflect::ReflectMessage)]")
.type_attribute(
full_name,
format!(r#"#[prost_reflect(message_name = "{}")]"#, full_name,),
format!(r#"#[prost_reflect(message_name = "{full_name}")]"#),
)
.type_attribute(full_name, &pool_attribute);
}
Expand Down
4 changes: 2 additions & 2 deletions prost-reflect-conformance-tests/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ fn main() -> Result<()> {
}

let version = git_describe(&src_dir)?;
let protobuf_dir = &out_dir.join(format!("protobuf-{}", version));
let protobuf_dir = &out_dir.join(format!("protobuf-{version}"));

if !protobuf_dir.exists() {
let build_dir = &out_dir.join(format!("build-protobuf-{}", version));
let build_dir = &out_dir.join(format!("build-protobuf-{version}"));
fs::create_dir_all(build_dir).expect("failed to create build directory");

let tempdir = tempfile::Builder::new()
Expand Down
2 changes: 1 addition & 1 deletion prost-reflect-conformance-tests/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fn main() -> io::Result<()> {

let result = match ConformanceRequest::decode(&*bytes) {
Ok(request) => handle_request(request),
Err(error) => conformance_response::Result::ParseError(format!("{:?}", error)),
Err(error) => conformance_response::Result::ParseError(format!("{error:?}")),
};

let response = ConformanceResponse {
Expand Down
2 changes: 1 addition & 1 deletion prost-reflect-tests/src/arbitrary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub fn mask() -> impl Strategy<Value = FieldMask> {
let mut parts = parts.into_iter();
let mut path = parts.next().unwrap();
for part in parts {
write!(path, ".{}", part).unwrap();
write!(path, ".{part}").unwrap();
}
path
})
Expand Down
48 changes: 39 additions & 9 deletions prost-reflect-tests/src/desc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,35 +243,35 @@ fn test_debug_impls() {
let _ = format!("{:?}", test_file_descriptor());

for service in test_file_descriptor().services() {
let _ = format!("{:?}", service);
let _ = format!("{service:?}");
for method in service.methods() {
let _ = format!("{:?}", method);
let _ = format!("{method:?}");
}
}

for file in test_file_descriptor().files() {
let _ = format!("{:?}", file);
let _ = format!("{file:?}");
}

for message in test_file_descriptor().all_messages() {
let _ = format!("{:?}", message);
let _ = format!("{message:?}");
for field in message.fields() {
let _ = format!("{:?}", field);
let _ = format!("{field:?}");
}
for oneof in message.oneofs() {
let _ = format!("{:?}", oneof);
let _ = format!("{oneof:?}");
}
}

for enum_ in test_file_descriptor().all_enums() {
let _ = format!("{:?}", enum_);
let _ = format!("{enum_:?}");
for value in enum_.values() {
let _ = format!("{:?}", value);
let _ = format!("{value:?}");
}
}

for extension in test_file_descriptor().all_extensions() {
let _ = format!("{:?}", extension);
let _ = format!("{extension:?}");
}
}

Expand Down Expand Up @@ -785,3 +785,33 @@ fn message_take_fields() {
assert_eq!(dynamic_message.fields_mut().count(), 0);
assert_eq!(dynamic_message.extensions_mut().count(), 0);
}

#[test]
fn oneof_not_synthetic() {
let message_desc = test_file_descriptor()
.get_message_by_name("test.MessageWithOneof")
.unwrap();

assert_eq!(message_desc.oneofs().len(), 1);

let oneof_desc = message_desc.oneofs().next().unwrap();
assert_eq!(oneof_desc.name(), "test_oneof");
assert!(!oneof_desc.is_synthetic());
}

#[test]
fn proto3_optional_field() {
let message_desc = test_file_descriptor()
.get_message_by_name("test.MessageWithOptionalEnum")
.unwrap();
let field_desc = message_desc.get_field_by_name("optional_enum").unwrap();
let oneof_desc = field_desc.containing_oneof().unwrap();

assert!(field_desc.supports_presence());
assert_eq!(oneof_desc.name(), "_optional_enum");
assert!(oneof_desc.is_synthetic());
assert!(oneof_desc.fields().eq([field_desc.clone()]));

assert_eq!(message_desc.oneofs().len(), 1);
assert!(message_desc.oneofs().eq([oneof_desc.clone()]));
}
8 changes: 8 additions & 0 deletions prost-reflect-tests/src/test.proto
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ message MessageWithOneof {
}
}

message MessageWithOptionalEnum {
enum OptionalEnum {
DEFAULT = 0;
}

optional OptionalEnum optional_enum = 1;
}

message Point {
int32 latitude = 1;
int32 longitude = 2;
Expand Down
12 changes: 11 additions & 1 deletion prost-reflect/src/descriptor/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1610,6 +1610,16 @@ impl OneofDescriptor {
})
}

/// Returns `true` if this is a synthetic oneof generated to support proto3 optional semantics.
///
/// If `true`, then [`fields`](OneofDescriptor::fields) will yield exactly one field, for which [`FieldDescriptorProto::proto3_optional`] returns true.
pub fn is_synthetic(&self) -> bool {
self.fields().len() == 1
&& self
.fields()
.all(|f| f.field_descriptor_proto().proto3_optional())
}

fn inner(&self) -> &OneofDescriptorInner {
&self.message.inner().oneofs[self.index as usize]
}
Expand Down Expand Up @@ -1882,7 +1892,7 @@ fn join_name<'a>(namespace: &str, name: &'a str) -> Cow<'a, str> {
if namespace.is_empty() {
Cow::Borrowed(name)
} else {
Cow::Owned(format!("{}.{}", namespace, name))
Cow::Owned(format!("{namespace}.{name}"))
}
}

Expand Down
7 changes: 3 additions & 4 deletions prost-reflect/src/descriptor/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,7 @@ impl<'a, 'b> ResolveNameResult<'a, 'b> {
join_path(found_path1, found_path2),
),
help: Some(format!(
"'{}' is defined in '{}', which is not imported by '{}'",
name, dep_name, root_name
"'{name}' is defined in '{dep_name}', which is not imported by '{root_name}'"
)),
name: name.into_owned(),
})
Expand Down Expand Up @@ -341,7 +340,7 @@ fn resolve_relative_name<'a, 'b>(
for candidate_parent in resolve_relative_candidate_parents(scope) {
let candidate = match candidate_parent {
"" => Cow::Borrowed(relative_first_part),
_ => Cow::Owned(format!("{}.{}", candidate_parent, relative_first_part)),
_ => Cow::Owned(format!("{candidate_parent}.{relative_first_part}")),
};

if relative_first_part.len() == relative_name.len() {
Expand All @@ -358,7 +357,7 @@ fn resolve_relative_name<'a, 'b>(
Some(def) if def.kind.is_parent() => {
let candidate_full = match candidate_parent {
"" => Cow::Borrowed(relative_name),
_ => Cow::Owned(format!("{}.{}", candidate_parent, relative_name)),
_ => Cow::Owned(format!("{candidate_parent}.{relative_name}")),
};

let res =
Expand Down
8 changes: 4 additions & 4 deletions prost-reflect/src/descriptor/build/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ fn set_file_option(file: &mut FileDescriptorProto, path: &[i32], encoded: &[u8])
let value = &mut service.method[path[3] as usize];
value.options = Some(Options::decode(encoded).unwrap());
}
p => panic!("unknown path element {}", p),
p => panic!("unknown path element {p}"),
}
}
tag::file::EXTENSION => {
Expand All @@ -753,7 +753,7 @@ fn set_file_option(file: &mut FileDescriptorProto, path: &[i32], encoded: &[u8])
let field = &mut file.extension[path[1] as usize];
field.options = Some(Options::decode(encoded).unwrap());
}
p => panic!("unknown path element {}", p),
p => panic!("unknown path element {p}"),
}
}

Expand Down Expand Up @@ -795,7 +795,7 @@ fn set_message_option(message: &mut DescriptorProto, path: &[i32], encoded: &[u8
let field = &mut message.extension[path[1] as usize];
field.options = Some(Options::decode(encoded).unwrap());
}
p => panic!("unknown path element {}", p),
p => panic!("unknown path element {p}"),
}
}

Expand All @@ -808,6 +808,6 @@ fn set_enum_option(enum_: &mut EnumDescriptorProto, path: &[i32], encoded: &[u8]
let value = &mut enum_.value[path[1] as usize];
value.options = Some(Options::decode(encoded).unwrap());
}
p => panic!("unknown path element {}", p),
p => panic!("unknown path element {p}"),
}
}
16 changes: 8 additions & 8 deletions prost-reflect/src/descriptor/build/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ impl Visitor for ResolveVisitor<'_> {
id: Identity::new(file, path, full_name, extension.name()),
parent: parent_message,
number: extension.number() as u32,
json_name: format!("[{}]", full_name).into(),
json_name: format!("[{full_name}]").into(),
extendee: extendee.unwrap_or(MessageIndex::MAX),
kind: kind.unwrap_or(KindIndex::Double),
is_packed,
Expand Down Expand Up @@ -653,7 +653,7 @@ impl ResolveVisitor<'_> {
Err(_) => {
self.errors.push(DescriptorErrorKind::InvalidFieldDefault {
value: default_value.to_owned(),
kind: format!("{:?}", kind),
kind: format!("{kind:?}"),
found: Label::new(
&self.pool.files,
"found here",
Expand Down Expand Up @@ -753,7 +753,7 @@ impl ResolveVisitor<'_> {
&mut self.pool.files[file as usize].raw,
path,
tag,
format!(".{}", type_name),
format!(".{type_name}"),
ty,
);
Some(def)
Expand Down Expand Up @@ -899,7 +899,7 @@ fn set_type_name(
match tag {
tag::method::INPUT_TYPE => method.input_type = Some(type_name),
tag::method::OUTPUT_TYPE => method.output_type = Some(type_name),
p => panic!("unknown path element {}", p),
p => panic!("unknown path element {p}"),
}
}
tag::file::MESSAGE_TYPE | tag::file::EXTENSION => {
Expand All @@ -912,10 +912,10 @@ fn set_type_name(
}
}
tag::field::EXTENDEE => field.extendee = Some(type_name),
p => panic!("unknown path element {}", p),
p => panic!("unknown path element {p}"),
}
}
p => panic!("unknown path element {}", p),
p => panic!("unknown path element {p}"),
}
}

Expand All @@ -932,7 +932,7 @@ fn find_file_field_proto_mut<'a>(
debug_assert_eq!(path.len(), 2);
&mut file.extension[path[1] as usize]
}
p => panic!("unknown path element {}", p),
p => panic!("unknown path element {p}"),
}
}

Expand All @@ -953,7 +953,7 @@ fn find_message_field_proto<'a>(
let nested_message = &mut message.nested_type[path[1] as usize];
find_message_field_proto(nested_message, &path[2..])
}
p => panic!("unknown path element {}", p),
p => panic!("unknown path element {p}"),
}
}

Expand Down
Loading