Skip to content

Commit

Permalink
Remove rfc
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Jan 18, 2024
1 parent 07b6159 commit 2a83cd5
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 106 deletions.
91 changes: 1 addition & 90 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ regex = { version = "1.10.2" }
reqwest = { version = "0.11.23", default-features = false, features = ["json", "gzip", "brotli", "stream", "rustls-tls"] }
reqwest-middleware = { version = "0.2.4" }
reqwest-retry = { version = "0.3.0" }
rfc2047-decoder = { version = "1.0.2" }
rmp-serde = { version = "1.1.2" }
rustc-hash = { version = "1.1.0" }
seahash = { version = "4.1.0" }
Expand Down
1 change: 0 additions & 1 deletion crates/pypi-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ chrono = { workspace = true, features = ["serde"] }
mailparse = { workspace = true }
once_cell = { workspace = true }
regex = { workspace = true }
rfc2047-decoder = { workspace = true }
serde = { workspace = true }
thiserror = { workspace = true }
tracing = { workspace = true }
Expand Down
62 changes: 48 additions & 14 deletions crates/pypi-types/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,24 +75,16 @@ pub enum Error {
impl Metadata21 {
/// Parse distribution metadata from metadata bytes
pub fn parse(content: &[u8]) -> Result<Self, Error> {
// HACK: trick mailparse to parse as UTF-8 instead of ASCII
let mut mail = b"Content-Type: text/plain; charset=utf-8\n".to_vec();
mail.extend_from_slice(content);

let msg = mailparse::parse_mail(&mail)?;
let msg = mailparse::parse_mail(&content)?;
let headers = msg.get_headers();

let get_first_value = |name| {
headers.get_first_header(name).and_then(|header| {
match rfc2047_decoder::decode(header.get_value_raw()) {
Ok(value) => {
if value == "UNKNOWN" {
None
} else {
Some(value)
}
}
Err(_) => None,
let value = header.get_value();
if value == "UNKNOWN" {
None
} else {
Some(value)
}
})
};
Expand Down Expand Up @@ -141,3 +133,45 @@ impl Metadata21 {
})
}
}

impl FromStr for Metadata21 {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Metadata21::parse(s.as_bytes())
}
}

#[cfg(test)]
mod tests {
use std::str::FromStr;

use pep440_rs::Version;
use puffin_normalize::PackageName;

use crate::Error;

use super::Metadata21;

#[test]
fn test_parse_from_str() {
let s = "Metadata-Version: 1.0";
let meta: Result<Metadata21, Error> = s.parse();
assert!(matches!(meta, Err(Error::FieldNotFound("Name"))));

let s = "Metadata-Version: 1.0\nName: asdf";
let meta = Metadata21::parse(s.as_bytes());
assert!(matches!(meta, Err(Error::FieldNotFound("Version"))));

let s = "Metadata-Version: 1.0\nName: asdf\nVersion: 1.0";
let meta = Metadata21::parse(s.as_bytes()).unwrap();
assert_eq!(meta.metadata_version, "1.0");
assert_eq!(meta.name, PackageName::from_str("asdf").unwrap());
assert_eq!(meta.version, Version::new([1, 0]));

let s = "Metadata-Version: 1.0\nName: asdf\nVersion: 1.0\nAuthor: 中文\n\n一个 Python 包";
let meta = Metadata21::parse(s.as_bytes()).unwrap();
assert_eq!(meta.metadata_version, "2.0");
assert_eq!(meta.name, PackageName::from_str("asdf").unwrap());
assert_eq!(meta.version, Version::new([1, 0]));
}
}
1 change: 1 addition & 0 deletions requirements.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
black

0 comments on commit 2a83cd5

Please sign in to comment.