Skip to content

Commit

Permalink
fix accept header parser
Browse files Browse the repository at this point in the history
  • Loading branch information
sunli829 committed Aug 18, 2023
1 parent cc5d458 commit 98b6f3a
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 67 deletions.
106 changes: 48 additions & 58 deletions poem-grpc-build/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@ impl Config {
///
/// ```rust
/// # let mut config = poem_grpc_build::Config::new();
/// config.codec("::poem_grpc::codec::ProstCodec");
/// config.codec("::poem_grpc::codec::JsonCodec");
/// config
/// .codec("::poem_grpc::codec::ProstCodec")
/// .codec("::poem_grpc::codec::JsonCodec");
/// ```
pub fn codec(mut self, path: impl Into<String>) -> Self {
self.grpc_config.codec_list.push(path.into());
Expand Down Expand Up @@ -107,29 +108,23 @@ impl Config {
/// ```rust
/// # let mut config = poem_grpc_build::Config::new();
/// // Match a specific field in a message type.
/// config.btree_map(&[".my_messages.MyMessageType.my_map_field"]);
///
/// // Match all map fields in a message type.
/// config.btree_map(&[".my_messages.MyMessageType"]);
///
/// // Match all map fields in a package.
/// config.btree_map(&[".my_messages"]);
///
/// // Match all map fields. Specially useful in `no_std` contexts.
/// config.btree_map(&["."]);
///
/// // Match all map fields in a nested message.
/// config.btree_map(&[".my_messages.MyMessageType.MyNestedMessageType"]);
///
/// // Match all fields named 'my_map_field'.
/// config.btree_map(&["my_map_field"]);
///
/// // Match all fields named 'my_map_field' in messages named 'MyMessageType', regardless of
/// // package or nesting.
/// config.btree_map(&["MyMessageType.my_map_field"]);
///
/// // Match all fields named 'my_map_field', and all fields in the 'foo.bar' package.
/// config.btree_map(&["my_map_field", ".foo.bar"]);
/// config
/// .btree_map(&[".my_messages.MyMessageType.my_map_field"])
/// // Match all map fields in a message type.
/// .btree_map(&[".my_messages.MyMessageType"])
/// // Match all map fields in a package.
/// .btree_map(&[".my_messages"])
/// // Match all map fields. Specially useful in `no_std` contexts.
/// .btree_map(&["."])
/// // Match all map fields in a nested message.
/// .btree_map(&[".my_messages.MyMessageType.MyNestedMessageType"])
/// // Match all fields named 'my_map_field'.
/// .btree_map(&["my_map_field"])
/// // Match all fields named 'my_map_field' in messages named 'MyMessageType', regardless of
/// // package or nesting.
/// .btree_map(&["MyMessageType.my_map_field"])
/// // Match all fields named 'my_map_field', and all fields in the 'foo.bar' package.
/// .btree_map(&["my_map_field", ".foo.bar"]);
/// ```
///
/// [1]: https://doc.rust-lang.org/std/collections/struct.BTreeMap.html
Expand Down Expand Up @@ -166,29 +161,23 @@ impl Config {
/// ```rust
/// # let mut config = poem_grpc_build::Config::new();
/// // Match a specific field in a message type.
/// config.bytes(&[".my_messages.MyMessageType.my_bytes_field"]);
///
/// // Match all bytes fields in a message type.
/// config.bytes(&[".my_messages.MyMessageType"]);
///
/// // Match all bytes fields in a package.
/// config.bytes(&[".my_messages"]);
///
/// // Match all bytes fields. Specially useful in `no_std` contexts.
/// config.bytes(&["."]);
///
/// // Match all bytes fields in a nested message.
/// config.bytes(&[".my_messages.MyMessageType.MyNestedMessageType"]);
///
/// // Match all fields named 'my_bytes_field'.
/// config.bytes(&["my_bytes_field"]);
///
/// // Match all fields named 'my_bytes_field' in messages named 'MyMessageType', regardless of
/// // package or nesting.
/// config.bytes(&["MyMessageType.my_bytes_field"]);
///
/// // Match all fields named 'my_bytes_field', and all fields in the 'foo.bar' package.
/// config.bytes(&["my_bytes_field", ".foo.bar"]);
/// config
/// .bytes(&[".my_messages.MyMessageType.my_bytes_field"])
/// // Match all bytes fields in a message type.
/// .bytes(&[".my_messages.MyMessageType"])
/// // Match all bytes fields in a package.
/// .bytes(&[".my_messages"])
/// // Match all bytes fields. Specially useful in `no_std` contexts.
/// .bytes(&["."])
/// // Match all bytes fields in a nested message.
/// .bytes(&[".my_messages.MyMessageType.MyNestedMessageType"])
/// // Match all fields named 'my_bytes_field'.
/// .bytes(&["my_bytes_field"])
/// // Match all fields named 'my_bytes_field' in messages named 'MyMessageType', regardless of
/// // package or nesting.
/// .bytes(&["MyMessageType.my_bytes_field"])
/// // Match all fields named 'my_bytes_field', and all fields in the 'foo.bar' package.
/// .bytes(&["my_bytes_field", ".foo.bar"]);
/// ```
///
/// [1]: https://docs.rs/bytes/latest/bytes/struct.Bytes.html
Expand Down Expand Up @@ -227,16 +216,17 @@ impl Config {
/// ```rust
/// # let mut config = poem_grpc_build::Config::new();
/// // Nothing around uses floats, so we can derive real `Eq` in addition to `PartialEq`.
/// config.type_attribute(".", "#[derive(Eq)]");
/// // Some messages want to be serializable with serde as well.
/// config.type_attribute(
/// "my_messages.MyMessageType",
/// "#[derive(Serialize)] #[serde(rename-all = \"snake_case\")]",
/// );
/// config.type_attribute(
/// "my_messages.MyMessageType.MyNestedMessageType",
/// "#[derive(Serialize)] #[serde(rename-all = \"snake_case\")]",
/// );
/// config
/// .type_attribute(".", "#[derive(Eq)]")
/// // Some messages want to be serializable with serde as well.
/// .type_attribute(
/// "my_messages.MyMessageType",
/// "#[derive(Serialize)] #[serde(rename-all = \"snake_case\")]",
/// )
/// .type_attribute(
/// "my_messages.MyMessageType.MyNestedMessageType",
/// "#[derive(Serialize)] #[serde(rename-all = \"snake_case\")]",
/// );
/// ```
///
/// # Oneof fields
Expand Down
18 changes: 9 additions & 9 deletions poem/src/web/accept.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ fn parse_accept(headers: &HeaderMap) -> Vec<Mime> {
.iter()
.filter_map(|hval| hval.to_str().ok())
.flat_map(|s| s.split(',').map(str::trim))
.filter_map(|v| {
let (e, q) = match v.split_once(";q=") {
Some((e, q)) => (e, (q.parse::<f32>().ok()? * 1000.0) as i32),
None => (v, 1000),
};
let mime: Mime = e.parse().ok()?;
.filter_map(|item| {
let mime: Mime = item.parse().ok()?;
let q = mime
.get_param("q")
.and_then(|value| Some((value.as_str().parse::<f32>().ok()? * 1000.0) as i32))
.unwrap_or(1000);
Some((mime, q))
})
.collect::<Vec<_>>();
Expand Down Expand Up @@ -55,9 +55,9 @@ mod tests {
&[
Mime::from_str("text/html").unwrap(),
Mime::from_str("application/xhtml+xml").unwrap(),
Mime::from_str("application/xml").unwrap(),
Mime::from_str("text/yaml").unwrap(),
Mime::from_str("*/*").unwrap()
Mime::from_str("application/xml;q=0.9").unwrap(),
Mime::from_str("text/yaml;q=0.5").unwrap(),
Mime::from_str("*/*;q=0.1").unwrap()
]
);
}
Expand Down

0 comments on commit 98b6f3a

Please sign in to comment.