-
Notifications
You must be signed in to change notification settings - Fork 533
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
chore(prost_build): introduce a typeset FullyQualifiedName #1191
Open
gibbz00
wants to merge
2
commits into
tokio-rs:master
Choose a base branch
from
gibbz00:fqn
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,48 +23,49 @@ pub struct ExternPaths { | |
} | ||
|
||
impl ExternPaths { | ||
pub fn new(paths: &[(String, String)], prost_types: bool) -> Result<ExternPaths, String> { | ||
pub fn new<'a>( | ||
paths: impl IntoIterator<Item = (&'a str, &'a str)> + 'a, | ||
prost_types: bool, | ||
) -> Result<ExternPaths, String> { | ||
let mut extern_paths = ExternPaths { | ||
extern_paths: HashMap::new(), | ||
}; | ||
|
||
for (proto_path, rust_path) in paths { | ||
extern_paths.insert(proto_path.clone(), rust_path.clone())?; | ||
extern_paths.insert(proto_path, rust_path)?; | ||
} | ||
|
||
if prost_types { | ||
extern_paths.insert(".google.protobuf".to_string(), "::prost_types".to_string())?; | ||
extern_paths.insert(".google.protobuf.BoolValue".to_string(), "bool".to_string())?; | ||
extern_paths.insert(".google.protobuf", "::prost_types")?; | ||
extern_paths.insert(".google.protobuf.BoolValue", "bool")?; | ||
extern_paths.insert( | ||
".google.protobuf.BytesValue".to_string(), | ||
"::prost::alloc::vec::Vec<u8>".to_string(), | ||
".google.protobuf.BytesValue", | ||
"::prost::alloc::vec::Vec<u8>", | ||
)?; | ||
extern_paths.insert(".google.protobuf.DoubleValue", "f64")?; | ||
extern_paths.insert(".google.protobuf.Empty", "()")?; | ||
extern_paths.insert(".google.protobuf.FloatValue", "f32")?; | ||
extern_paths.insert(".google.protobuf.Int32Value", "i32")?; | ||
extern_paths.insert(".google.protobuf.Int64Value", "i64")?; | ||
extern_paths.insert( | ||
".google.protobuf.DoubleValue".to_string(), | ||
"f64".to_string(), | ||
)?; | ||
extern_paths.insert(".google.protobuf.Empty".to_string(), "()".to_string())?; | ||
extern_paths.insert(".google.protobuf.FloatValue".to_string(), "f32".to_string())?; | ||
extern_paths.insert(".google.protobuf.Int32Value".to_string(), "i32".to_string())?; | ||
extern_paths.insert(".google.protobuf.Int64Value".to_string(), "i64".to_string())?; | ||
extern_paths.insert( | ||
".google.protobuf.StringValue".to_string(), | ||
"::prost::alloc::string::String".to_string(), | ||
)?; | ||
extern_paths.insert( | ||
".google.protobuf.UInt32Value".to_string(), | ||
"u32".to_string(), | ||
)?; | ||
extern_paths.insert( | ||
".google.protobuf.UInt64Value".to_string(), | ||
"u64".to_string(), | ||
".google.protobuf.StringValue", | ||
"::prost::alloc::string::String", | ||
)?; | ||
extern_paths.insert(".google.protobuf.UInt32Value", "u32")?; | ||
extern_paths.insert(".google.protobuf.UInt64Value", "u64")?; | ||
} | ||
|
||
Ok(extern_paths) | ||
} | ||
|
||
fn insert(&mut self, proto_path: String, rust_path: String) -> Result<(), String> { | ||
fn insert( | ||
&mut self, | ||
proto_path: impl Into<String>, | ||
rust_path: impl Into<String>, | ||
Comment on lines
+67
to
+68
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, this is a great improvement. |
||
) -> Result<(), String> { | ||
let proto_path = proto_path.into(); | ||
let rust_path = rust_path.into(); | ||
|
||
validate_proto_path(&proto_path)?; | ||
match self.extern_paths.entry(proto_path) { | ||
hash_map::Entry::Occupied(occupied) => { | ||
|
@@ -124,12 +125,12 @@ mod tests { | |
#[test] | ||
fn test_extern_paths() { | ||
let paths = ExternPaths::new( | ||
&[ | ||
(".foo".to_string(), "::foo1".to_string()), | ||
(".foo.bar".to_string(), "::foo2".to_string()), | ||
(".foo.baz".to_string(), "::foo3".to_string()), | ||
(".foo.Fuzz".to_string(), "::foo4::Fuzz".to_string()), | ||
(".a.b.c.d.e.f".to_string(), "::abc::def".to_string()), | ||
[ | ||
(".foo", "::foo1"), | ||
(".foo.bar", "::foo2"), | ||
(".foo.baz", "::foo3"), | ||
(".foo.Fuzz", "::foo4::Fuzz"), | ||
(".a.b.c.d.e.f", "::abc::def"), | ||
], | ||
false, | ||
) | ||
|
@@ -157,7 +158,7 @@ mod tests { | |
|
||
#[test] | ||
fn test_well_known_types() { | ||
let paths = ExternPaths::new(&[], true).unwrap(); | ||
let paths = ExternPaths::new([], true).unwrap(); | ||
|
||
let case = |proto_ident: &str, resolved_ident: &str| { | ||
assert_eq!(paths.resolve_ident(proto_ident).unwrap(), resolved_ident); | ||
|
@@ -170,8 +171,8 @@ mod tests { | |
|
||
#[test] | ||
fn test_error_fully_qualified() { | ||
let paths = [("foo".to_string(), "bar".to_string())]; | ||
let err = ExternPaths::new(&paths, false).unwrap_err(); | ||
let paths = [("foo", "bar")]; | ||
let err = ExternPaths::new(paths, false).unwrap_err(); | ||
assert_eq!( | ||
err.to_string(), | ||
"Protobuf paths must be fully qualified (begin with a leading '.'): foo" | ||
|
@@ -180,8 +181,8 @@ mod tests { | |
|
||
#[test] | ||
fn test_error_invalid_path() { | ||
let paths = [(".foo.".to_string(), "bar".to_string())]; | ||
let err = ExternPaths::new(&paths, false).unwrap_err(); | ||
let paths = [(".foo.", "bar")]; | ||
let err = ExternPaths::new(paths, false).unwrap_err(); | ||
assert_eq!( | ||
err.to_string(), | ||
"invalid fully-qualified Protobuf path: .foo." | ||
|
@@ -190,11 +191,8 @@ mod tests { | |
|
||
#[test] | ||
fn test_error_duplicate() { | ||
let paths = [ | ||
(".foo".to_string(), "bar".to_string()), | ||
(".foo".to_string(), "bar".to_string()), | ||
]; | ||
let err = ExternPaths::new(&paths, false).unwrap_err(); | ||
let paths = [(".foo", "bar"), (".foo", "bar")]; | ||
let err = ExternPaths::new(paths, false).unwrap_err(); | ||
assert_eq!(err.to_string(), "duplicate extern Protobuf path: .foo") | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be honest, if the goal is to simplify, then that failed here. The previous version was simpler.
I think
Config::extern_paths
should change type to better match the internals ofExternPaths
. Ifextern_paths
is aHashMap
, then it can be passed toExternPaths::new
and used without doing a manual copy.