Skip to content

Commit 1e5cb7f

Browse files
authored
Merge pull request #5 from meilisearch/fix-bug-with-empty-vec
Rewrite everything
2 parents 37bc85f + b73bcbb commit 1e5cb7f

File tree

9 files changed

+812
-981
lines changed

9 files changed

+812
-981
lines changed

Cargo.toml

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
[package]
22
name = "yaup"
3-
version = "0.2.1"
4-
authors = ["boxdot <[email protected]>, tamo <[email protected]>"]
3+
version = "0.3.0"
4+
authors = ["tamo <[email protected]>"]
55
license = "MIT/Apache-2.0"
66
description = "URL parameters serialization"
7-
repository = "https://github.com/irevoire/yaup"
8-
keywords = ["serde", "serialization", "url"]
7+
repository = "https://github.com/meilisearch/yaup"
8+
keywords = ["serde", "serialization", "url", "meilisearch"]
99
categories = ["encoding"]
1010
readme = "README.md"
1111
include = ["Cargo.toml", "src/**/*.rs", "README.md", "LICENSE-APACHE", "LICENSE-MIT"]
1212
edition = "2021"
1313

1414
[dependencies]
1515
serde = "1"
16-
url = "2"
16+
form_urlencoded = "1.2.1"
17+
thiserror = "1.0.61"
1718

1819
[dev-dependencies]
20+
insta = "1.39.0"
21+
maplit = "1.0.2"
1922
serde = { version = "1", features = ["derive"] }

README.md

+19-11
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
# Yaup - Yet Another URL Params crate
22

3-
This is a fork of [`serde_url_params`](https://github.com/boxdot/serde-url-params-rs).
4-
I updated the way of serializing arrays:
5-
Serializing `{ "food": ["baguette", "with", "cheese"] }`
6-
- With `serde_url_params` returns `food=baguette&food=with&food=cheese`.
7-
- With `yaup` it returns `food=baguette,with,cheese`.
3+
Serialize your structures as query parameters.
4+
I made this crate because I didn't find anything that matched the structure of the query parameters used in Meilisearch.
85

9-
And I got rids of the serialization of embedded structures.
6+
Specificities of this query parameters format:
7+
- The crate writes the initial `?` if there are parameters to send.
8+
- You can only serialize structures that follow a "key-value" shape, like structures, `HashMap`, `BTreeMap`, etc.
9+
- Sequences (arrays, vectors, tuples, etc) are comma-separated. `{ doggo: vec!["kefir", "echo"] }` serialize as `?doggo=kefir,echo`.
10+
- Empty and `null` values are not ignored. `{ doggo: Vec::new(), catto: None }` serialize as `?doggo=&catto=null`.
11+
- Return an error if you try to serialize a structure with multiple levels of key-value structures (i.e., an object containing a `HashMap` for example).
1012

1113
## Example
1214

1315
```rust
14-
#[derive(Debug, Serialize)]
16+
#[derive(Debug, serde::Serialize)]
1517
enum Filter { New, Registered, Blocked }
1618

17-
#[derive(Debug, Serialize)]
19+
#[derive(Debug, serde::Serialize)]
1820
struct Params {
1921
cursor: Option<usize>,
2022
per_page: Option<usize>,
@@ -30,16 +32,22 @@ let params = Params {
3032
};
3133
assert_eq!(
3234
yaup::to_string(&params).unwrap(),
33-
"cursor=42&username=boxdot&filter=New,Blocked"
35+
"?cursor=42&per_page=null&username=tamo&filter=New,Blocked"
3436
);
3537
```
38+
## Thanks
39+
40+
This was originally a fork of [`serde_url_params`](https://github.com/boxdot/serde-url-params-rs) which is still maintained.
41+
Thanks, `boxdot`, for the initial code.
42+
43+
Everything has been rewritten from scratch for the v0.3.0.
3644

3745
## License
3846

3947
* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
40-
http://www.apache.org/licenses/LICENSE-2.0)
48+
<http://www.apache.org/licenses/LICENSE-2.0>)
4149
* MIT License ([LICENSE-MIT](LICENSE-MIT) or
42-
http://opensource.org/licenses/MIT)
50+
<http://opensource.org/licenses/MIT>)
4351

4452
### Contribution
4553

src/error.rs

+20-33
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,25 @@
11
//! When serializing to URL parameters fails.
22
3-
use serde::ser;
4-
use std::fmt;
5-
6-
#[derive(Debug)]
3+
#[derive(Debug, thiserror::Error)]
74
/// Represents all possible errors that can occur when serializing into URL
85
/// parameters.
96
pub enum Error {
107
/// External error caused by e.g. utf8 string conversion or io.
8+
#[error(transparent)]
119
Extern(Box<dyn std::error::Error + Send + Sync>),
12-
/// Error when tried to serialize an unsupported type.
13-
Unsupported(String),
14-
/// Custom error caused by any error while serializing a type.
10+
/// Error when trying to serialize a value without any key.
11+
#[error("Tried to serialize a {0} at the top level. Only key-value shapes are supported at the top level of a query parameter.")]
12+
UnsupportedAtTopLevel(&'static str),
13+
/// Error when trying to serialize a key-value in place of a simple value.
14+
#[error("Tried to serialize a {0} in place of a value. Only simple values are supported on the right-hand side of a parameter.")]
15+
UnsupportedNestedStruct(&'static str),
16+
/// Custom user defined error
17+
#[error("{0}")]
1518
Custom(String),
1619
}
1720

1821
/// Alias for `Result` with error type `serde_url_params::Error`.
19-
pub type Result<T> = std::result::Result<T, Error>;
20-
21-
impl Error {
22-
/// Creates a new error when a type is not supported for serializing into
23-
/// URL parameters.
24-
pub fn unsupported<T: fmt::Display>(msg: T) -> Self {
25-
Error::Unsupported(format!("{}", msg))
26-
}
27-
}
28-
29-
impl fmt::Display for Error {
30-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
31-
match *self {
32-
Error::Extern(ref err) => fmt::Display::fmt(err, f),
33-
Error::Unsupported(ref msg) | Error::Custom(ref msg) => fmt::Display::fmt(msg, f),
34-
}
35-
}
36-
}
37-
38-
impl std::error::Error for Error {}
39-
40-
impl ser::Error for Error {
41-
fn custom<T: fmt::Display>(msg: T) -> Error {
42-
Error::Custom(msg.to_string())
43-
}
44-
}
22+
pub type Result<T, E = Error> = std::result::Result<T, E>;
4523

4624
impl From<std::io::Error> for Error {
4725
fn from(err: std::io::Error) -> Self {
@@ -54,3 +32,12 @@ impl From<std::string::FromUtf8Error> for Error {
5432
Error::Extern(Box::new(err))
5533
}
5634
}
35+
36+
impl serde::ser::Error for Error {
37+
fn custom<T>(msg: T) -> Self
38+
where
39+
T: std::fmt::Display,
40+
{
41+
Error::Custom(msg.to_string())
42+
}
43+
}

0 commit comments

Comments
 (0)