@@ -78,14 +78,23 @@ pub struct Metadata {
7878impl Metadata {
7979 /// Parse distribution metadata from metadata bytes
8080 pub fn parse ( content : & [ u8 ] ) -> Result < Self , Error > {
81- let msg = mailparse:: parse_mail ( content) ?;
81+ // HACK: trick mailparse to parse as UTF-8 instead of ASCII
82+ let mut mail = b"Content-Type: text/plain; charset=utf-8\n " . to_vec ( ) ;
83+ mail. extend_from_slice ( content) ;
84+
85+ let msg = mailparse:: parse_mail ( & mail) ?;
8286 let headers = msg. get_headers ( ) ;
8387 let get_first_value = |name| {
84- headers. get_first_value ( name) . and_then ( |value| {
85- if value == "UNKNOWN" {
86- None
87- } else {
88- Some ( value)
88+ headers. get_first_header ( name) . and_then ( |header| {
89+ match rfc2047_decoder:: decode ( header. get_value_raw ( ) ) {
90+ Ok ( value) => {
91+ if value == "UNKNOWN" {
92+ None
93+ } else {
94+ Some ( value)
95+ }
96+ }
97+ Err ( _) => None ,
8998 }
9099 } )
91100 } ;
@@ -199,5 +208,10 @@ mod tests {
199208 let s = "Metadata-Version: 1.0\n Name: asdf\n Version: 1.0\n \n a Python package" ;
200209 let meta: Metadata = s. parse ( ) . unwrap ( ) ;
201210 assert_eq ! ( meta. description. as_deref( ) , Some ( "a Python package" ) ) ;
211+
212+ let s = "Metadata-Version: 1.0\n Name: asdf\n Version: 1.0\n Author: 中文\n \n 一个 Python 包" ;
213+ let meta: Metadata = s. parse ( ) . unwrap ( ) ;
214+ assert_eq ! ( meta. author. as_deref( ) , Some ( "中文" ) ) ;
215+ assert_eq ! ( meta. description. as_deref( ) , Some ( "一个 Python 包" ) ) ;
202216 }
203217}
0 commit comments