Replace unmaintained encoding crate with encoding_rs

This commit is contained in:
messense
2020-11-24 14:22:59 +08:00
committed by Martin Algesten
parent a0b88926fa
commit df1f4bd0e1
3 changed files with 11 additions and 14 deletions

View File

@@ -17,7 +17,7 @@ features = [ "tls", "json", "charset", "cookies", "socks-proxy" ]
[features] [features]
default = ["tls"] default = ["tls"]
json = ["serde", "serde_json"] json = ["serde", "serde_json"]
charset = ["encoding"] charset = ["encoding_rs"]
tls = ["rustls", "webpki", "webpki-roots"] tls = ["rustls", "webpki", "webpki-roots"]
native-certs = ["rustls-native-certs"] native-certs = ["rustls-native-certs"]
cookies = ["cookie", "cookie_store"] cookies = ["cookie", "cookie_store"]
@@ -36,7 +36,7 @@ webpki-roots = { version = "0.21", optional = true }
rustls-native-certs = { version = "0.5", optional = true } rustls-native-certs = { version = "0.5", optional = true }
serde = { version = "1", optional = true } serde = { version = "1", optional = true }
serde_json = { version = "1", optional = true } serde_json = { version = "1", optional = true }
encoding = { version = "0.2", optional = true } encoding_rs = { version = "0.8", optional = true }
cookie_store = { version = "0.12.0", optional = true } cookie_store = { version = "0.12.0", optional = true }
log = "0.4.11" log = "0.4.11"

View File

@@ -5,9 +5,7 @@ use std::io::{self, copy, empty, Cursor, Read, Write};
#[cfg(feature = "charset")] #[cfg(feature = "charset")]
use crate::response::DEFAULT_CHARACTER_SET; use crate::response::DEFAULT_CHARACTER_SET;
#[cfg(feature = "charset")] #[cfg(feature = "charset")]
use encoding::label::encoding_from_whatwg_label; use encoding_rs::Encoding;
#[cfg(feature = "charset")]
use encoding::EncoderTrap;
#[cfg(feature = "json")] #[cfg(feature = "json")]
use super::SerdeValue; use super::SerdeValue;
@@ -80,10 +78,10 @@ impl<'a> Payload<'a> {
Payload::Text(text, _charset) => { Payload::Text(text, _charset) => {
#[cfg(feature = "charset")] #[cfg(feature = "charset")]
let bytes = { let bytes = {
let encoding = encoding_from_whatwg_label(&_charset) let encoding = Encoding::for_label(_charset.as_bytes())
.or_else(|| encoding_from_whatwg_label(DEFAULT_CHARACTER_SET)) .or_else(|| Encoding::for_label(DEFAULT_CHARACTER_SET.as_bytes()))
.unwrap(); .unwrap();
encoding.encode(&text, EncoderTrap::Replace).unwrap() encoding.encode(&text).0
}; };
#[cfg(not(feature = "charset"))] #[cfg(not(feature = "charset"))]
let bytes = text.as_bytes(); let bytes = text.as_bytes();

View File

@@ -14,9 +14,7 @@ use crate::unit::Unit;
use serde::de::DeserializeOwned; use serde::de::DeserializeOwned;
#[cfg(feature = "charset")] #[cfg(feature = "charset")]
use encoding::label::encoding_from_whatwg_label; use encoding_rs::Encoding;
#[cfg(feature = "charset")]
use encoding::DecoderTrap;
pub const DEFAULT_CONTENT_TYPE: &str = "text/plain"; pub const DEFAULT_CONTENT_TYPE: &str = "text/plain";
pub const DEFAULT_CHARACTER_SET: &str = "utf-8"; pub const DEFAULT_CHARACTER_SET: &str = "utf-8";
@@ -327,12 +325,13 @@ impl Response {
pub fn into_string(self) -> io::Result<String> { pub fn into_string(self) -> io::Result<String> {
#[cfg(feature = "charset")] #[cfg(feature = "charset")]
{ {
let encoding = encoding_from_whatwg_label(self.charset()) let encoding = Encoding::for_label(self.charset().as_bytes())
.or_else(|| encoding_from_whatwg_label(DEFAULT_CHARACTER_SET)) .or_else(|| Encoding::for_label(DEFAULT_CHARACTER_SET.as_bytes()))
.unwrap(); .unwrap();
let mut buf: Vec<u8> = vec![]; let mut buf: Vec<u8> = vec![];
self.into_reader().read_to_end(&mut buf)?; self.into_reader().read_to_end(&mut buf)?;
Ok(encoding.decode(&buf, DecoderTrap::Replace).unwrap()) let (text, _, _) = encoding.decode(&buf);
Ok(text.into_owned())
} }
#[cfg(not(feature = "charset"))] #[cfg(not(feature = "charset"))]
{ {