Skip to content

Commit

Permalink
Remove rust-encoding support
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonSapin committed Jul 15, 2019
1 parent 47e2286 commit b567a51
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 235 deletions.
7 changes: 0 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@ serde_json = "1.0"

bencher = "0.1"

[features]
query_encoding = ["encoding"]

[dependencies]
encoding = {version = "0.2", optional = true}
idna = { version = "0.1.0", path = "./idna" }
matches = "0.1"
percent-encoding = { version = "1.0.0", path = "./percent_encoding" }
Expand All @@ -49,6 +45,3 @@ serde = {version = "1.0", optional = true}
[[bench]]
name = "parse_url"
harness = false

[package.metadata.docs.rs]
features = ["query_encoding"]
127 changes: 0 additions & 127 deletions src/encoding.rs

This file was deleted.

80 changes: 12 additions & 68 deletions src/form_urlencoded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@
//! Converts between a string (such as an URL’s query string)
//! and a sequence of (name, value) pairs.
use encoding::{decode_utf8_lossy, EncodingOverride};
use percent_encoding::{percent_decode, percent_encode_byte};
use query_encoding::{self, decode_utf8_lossy, EncodingOverride};
use std::borrow::{Borrow, Cow};
use std::fmt;
use std::str;

/// Convert a byte string in the `application/x-www-form-urlencoded` syntax
Expand All @@ -31,7 +30,7 @@ pub fn parse(input: &[u8]) -> Parse {
Parse { input: input }
}
/// The return type of `parse()`.
#[derive(Copy, Clone, Debug)]
#[derive(Copy, Clone)]
pub struct Parse<'a> {
input: &'a [u8],
}
Expand Down Expand Up @@ -91,7 +90,6 @@ impl<'a> Parse<'a> {
}

/// Like `Parse`, but yields pairs of `String` instead of pairs of `Cow<str>`.
#[derive(Debug)]
pub struct ParseIntoOwned<'a> {
inner: Parse<'a>,
}
Expand Down Expand Up @@ -161,20 +159,10 @@ impl<'a> Iterator for ByteSerialize<'a> {

/// The [`application/x-www-form-urlencoded` serializer](
/// https://url.spec.whatwg.org/#concept-urlencoded-serializer).
#[derive(Debug)]
pub struct Serializer<T: Target> {
pub struct Serializer<'a, T: Target> {
target: Option<T>,
start_position: usize,
encoding: EncodingOverride,
custom_encoding: Option<SilentDebug<Box<dyn FnMut(&str) -> Cow<[u8]>>>>,
}

struct SilentDebug<T>(T);

impl<T> fmt::Debug for SilentDebug<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("…")
}
encoding: EncodingOverride<'a>,
}

pub trait Target {
Expand Down Expand Up @@ -227,7 +215,7 @@ impl<'a> Target for ::UrlQuery<'a> {
type Finished = &'a mut ::Url;
}

impl<T: Target> Serializer<T> {
impl<'a, T: Target> Serializer<'a, T> {
/// Create a new `application/x-www-form-urlencoded` serializer for the given target.
///
/// If the target is non-empty,
Expand All @@ -246,8 +234,7 @@ impl<T: Target> Serializer<T> {
Serializer {
target: Some(target),
start_position: start_position,
encoding: EncodingOverride::utf8(),
custom_encoding: None,
encoding: None,
}
}

Expand All @@ -260,18 +247,8 @@ impl<T: Target> Serializer<T> {
}

/// Set the character encoding to be used for names and values before percent-encoding.
#[cfg(feature = "query_encoding")]
pub fn encoding_override(&mut self, new: Option<::encoding::EncodingRef>) -> &mut Self {
self.encoding = EncodingOverride::from_opt_encoding(new).to_output_encoding();
self
}

/// Set the character encoding to be used for names and values before percent-encoding.
pub fn custom_encoding_override<F>(&mut self, encode: F) -> &mut Self
where
F: FnMut(&str) -> Cow<[u8]> + 'static,
{
self.custom_encoding = Some(SilentDebug(Box::new(encode)));
pub fn encoding_override(&mut self, new: EncodingOverride<'a>) -> &mut Self {
self.encoding = new;
self
}

Expand All @@ -283,7 +260,6 @@ impl<T: Target> Serializer<T> {
string(&mut self.target),
self.start_position,
self.encoding,
&mut self.custom_encoding,
name,
value,
);
Expand Down Expand Up @@ -312,7 +288,6 @@ impl<T: Target> Serializer<T> {
string,
self.start_position,
self.encoding,
&mut self.custom_encoding,
k.as_ref(),
v.as_ref(),
);
Expand All @@ -321,26 +296,6 @@ impl<T: Target> Serializer<T> {
self
}

/// Add a name/value pair whose name is `_charset_`
/// and whose value is the character encoding’s name.
/// (See the `encoding_override()` method.)
///
/// Panics if called after `.finish()`.
#[cfg(feature = "query_encoding")]
pub fn append_charset(&mut self) -> &mut Self {
assert!(
self.custom_encoding.is_none(),
"Cannot use both custom_encoding_override() and append_charset()"
);
{
let string = string(&mut self.target);
append_separator_if_needed(string, self.start_position);
string.push_str("_charset_=");
string.push_str(self.encoding.name());
}
self
}

/// If this serializer was constructed with a string, take and return that string.
///
/// ```rust
Expand Down Expand Up @@ -378,26 +333,15 @@ fn append_pair(
string: &mut String,
start_position: usize,
encoding: EncodingOverride,
custom_encoding: &mut Option<SilentDebug<Box<dyn FnMut(&str) -> Cow<[u8]>>>>,
name: &str,
value: &str,
) {
append_separator_if_needed(string, start_position);
append_encoded(name, string, encoding, custom_encoding);
append_encoded(name, string, encoding);
string.push('=');
append_encoded(value, string, encoding, custom_encoding);
append_encoded(value, string, encoding);
}

fn append_encoded(
s: &str,
string: &mut String,
encoding: EncodingOverride,
custom_encoding: &mut Option<SilentDebug<Box<dyn FnMut(&str) -> Cow<[u8]>>>>,
) {
let bytes = if let Some(SilentDebug(ref mut custom)) = *custom_encoding {
custom(s)
} else {
encoding.encode(s.into())
};
string.extend(byte_serialize(&bytes));
fn append_encoded(s: &str, string: &mut String, encoding: EncodingOverride) {
string.extend(byte_serialize(&query_encoding::encode(encoding, s.into())))
}
Loading

0 comments on commit b567a51

Please sign in to comment.