fn parse_encoding(encoding: &str) -> anyhow::Result<&'static Encoding> {
    let normalized_encoding = encoding.to_lowercase().replace(
        |c: char| (!c.is_alphanumeric() && c != '_' && c != '-' && c != ':'),
        "",
    );

    match normalized_encoding.as_str() {
        "u8" | "utf" => return Ok(encoding_rs::UTF_8),
        "u16" => return Ok(encoding_rs::UTF_16LE),
        _ => (),
    }

    for encoding in &[
        &normalized_encoding,
        &normalized_encoding.replace(&['-', '_'][..], ""),
        &normalized_encoding.replace('_', "-"),
        &normalized_encoding.replace('-', "_"),
    ] {
        if let Some(encoding) = Encoding::for_label(encoding.as_bytes()) {
            return Ok(encoding);
        }
    }

    {
        let mut encoding = normalized_encoding.replace(&['-', '_'][..], "");
        if let Some(first_digit_index) = encoding.find(|c: char| c.is_digit(10)) {
            encoding.insert(first_digit_index, '-');
            if let Some(encoding) = Encoding::for_label(encoding.as_bytes()) {
                return Ok(encoding);
            }
        }
    }

    Err(anyhow::anyhow!(
        "{} is not a supported encoding, please refer to https://encoding.spec.whatwg.org/#names-and-labels \
         for supported encodings",
        encoding
    ))
}
// HTTPie recognizes some encoding names that encoding_rs doesn't e.g utf16 has to spelled as utf-16.
// There are also some encodings which encoding_rs doesn't support but HTTPie does e.g utf-7.
// See https://github.com/ducaale/xh/pull/184#pullrequestreview-787528027
HTTPie recognizes some encoding names that encoding_rs doesn't e.g utf16 has to spelled as utf-16.
There are also some encodings which encoding_rs doesn't support but HTTPie does e.g utf-7.
HTTPie recognizes some encoding names that encoding_rs doesn ' t e . g utf16 has to spelled as utf - 16 . There are also some encodings which encoding_rs doesn ' t support but HTTPie does e . g utf - 7 .
fn parse_encoding(encoding: &str) -> anyhow::Result<&'static Encoding> {
    let normalized_encoding = encoding.to_lowercase().replace(
        |c: char| (!c.is_alphanumeric() && c != '_' && c != '-' && c != ':'),
        "",
    );
    match normalized_encoding.as_str() {
        "u8" | "utf" => return Ok(encoding_rs::UTF_8),
        "u16" => return Ok(encoding_rs::UTF_16LE),
        _ => (),
    }
    for encoding in &[
        &normalized_encoding,
        &normalized_encoding.replace(&['-', '_'][..], ""),
        &normalized_encoding.replace('_', "-"),
        &normalized_encoding.replace('-', "_"),
    ] {
        if let Some(encoding) = Encoding::for_label(encoding.as_bytes()) {
            return Ok(encoding);
        }
    }
    {
        let mut encoding = normalized_encoding.replace(&['-', '_'][..], "");
        if let Some(first_digit_index) = encoding.find(|c: char| c.is_digit(10)) {
            encoding.insert(first_digit_index, '-');
            if let Some(encoding) = Encoding::for_label(encoding.as_bytes()) {
                return Ok(encoding);
            }
        }
    }
    Err(anyhow::anyhow!(
        "{} is not a supported encoding, please refer to https://encoding.spec.whatwg.org/#names-and-labels \
         for supported encodings",
        encoding
    ))
}
fn parse_encoding ( encoding : & str ) -> anyhow :: Result < & ' static Encoding > { let normalized_encoding = encoding . to_lowercase ( ) . replace ( | c : char | ( ! c . is_alphanumeric ( ) && c != '_' && c != '-' && c != ':' ) , " " , ) ; match normalized_encoding . as_str ( ) { " " | " " => return Ok ( encoding_rs :: UTF_8 ) , " " => return Ok ( encoding_rs :: UTF_16LE ) , _ => ( ) , } for encoding in & [ & normalized_encoding , & normalized_encoding . replace ( & [ '-' , '_' ] [ .. ] , " " ) , & normalized_encoding . replace ( '_' , " " ) , & normalized_encoding . replace ( '-' , " " ) , ] { if let Some ( encoding ) = Encoding :: for_label ( encoding . as_bytes ( ) ) { return Ok ( encoding ) ; } } { let mut encoding = normalized_encoding . replace ( & [ '-' , '_' ] [ .. ] , " " ) ; if let Some ( first_digit_index ) = encoding . find ( | c : char | c . is_digit ( 10 ) ) { encoding . insert ( first_digit_index , '-' ) ; if let Some ( encoding ) = Encoding :: for_label ( encoding . as_bytes ( ) ) { return Ok ( encoding ) ; } } } Err ( anyhow :: anyhow ! ( " \
 " encoding ) ) }
HTTPie recognizes some encoding names that encoding_rs doesn't e.g utf16 has to spelled as utf-16.
HTTPie recognizes some encoding names that encoding_rs doesn ' t e . g utf16 has to spelled as utf - 16 .
encoding
&str
encoding
&str