wreq 1.3.2.3 → 1.3.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Cargo.lock +24 -3
- data/Cargo.toml +5 -3
- data/Rakefile +9 -3
- data/VERSION +1 -1
- data/crates/README.md +1 -0
- data/crates/serde-magnus/Cargo.toml +29 -0
- data/crates/serde-magnus/LICENSE +19 -0
- data/crates/serde-magnus/README.md +147 -0
- data/{src/serde → crates/serde-magnus/src}/de/array_deserializer.rs +2 -2
- data/{src/serde → crates/serde-magnus/src}/de/array_enumerator.rs +1 -1
- data/{src/serde → crates/serde-magnus/src}/de/deserializer.rs +25 -31
- data/{src/serde → crates/serde-magnus/src}/de/enum_deserializer.rs +3 -3
- data/{src/serde → crates/serde-magnus/src}/de/hash_deserializer.rs +2 -2
- data/crates/serde-magnus/src/de/mod.rs +554 -0
- data/{src/serde → crates/serde-magnus/src}/de/number_deserializer.rs +2 -2
- data/{src/serde → crates/serde-magnus/src}/de/variant_deserializer.rs +11 -20
- data/crates/serde-magnus/src/error.rs +84 -0
- data/crates/serde-magnus/src/lib.rs +137 -0
- data/crates/serde-magnus/src/options.rs +151 -0
- data/crates/serde-magnus/src/ser/enums.rs +11 -0
- data/{src/serde → crates/serde-magnus/src}/ser/map_serializer.rs +2 -2
- data/crates/serde-magnus/src/ser/mod.rs +428 -0
- data/crates/serde-magnus/src/ser/seq_serializer.rs +68 -0
- data/crates/serde-magnus/src/ser/serializer.rs +220 -0
- data/{src/serde → crates/serde-magnus/src}/ser/struct_serializer.rs +4 -4
- data/crates/serde-magnus/src/ser/struct_variant_serializer.rs +45 -0
- data/crates/serde-magnus/src/ser/tuple_variant_serializer.rs +38 -0
- data/{src/serde → crates/serde-magnus/src}/tests.rs +19 -68
- data/src/client/body/json.rs +2 -1
- data/src/client/resp.rs +1 -1
- data/src/error.rs +3 -23
- data/src/lib.rs +0 -1
- data/src/options.rs +19 -17
- metadata +24 -20
- data/src/serde/de.rs +0 -33
- data/src/serde/error.rs +0 -146
- data/src/serde/ser/enums.rs +0 -14
- data/src/serde/ser/seq_serializer.rs +0 -71
- data/src/serde/ser/serializer.rs +0 -194
- data/src/serde/ser/struct_variant_serializer.rs +0 -44
- data/src/serde/ser/tuple_variant_serializer.rs +0 -41
- data/src/serde/ser.rs +0 -18
- data/src/serde.rs +0 -202
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
use std::ffi::c_long;
|
|
2
|
+
|
|
3
|
+
use magnus::{IntoValue, RHash, Ruby, Value};
|
|
4
|
+
use serde::Serialize;
|
|
5
|
+
|
|
6
|
+
use super::{
|
|
7
|
+
MapSerializer, SeqSerializer, StructSerializer, StructVariantSerializer,
|
|
8
|
+
TupleVariantSerializer, enums::nest,
|
|
9
|
+
};
|
|
10
|
+
use crate::error::Error;
|
|
11
|
+
|
|
12
|
+
pub(super) struct Serializer<'r> {
|
|
13
|
+
ruby: &'r Ruby,
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
impl<'r> Serializer<'r> {
|
|
17
|
+
pub fn new(ruby: &'r Ruby) -> Self {
|
|
18
|
+
Self { ruby }
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
impl<'r> serde::Serializer for Serializer<'r> {
|
|
23
|
+
type Ok = Value;
|
|
24
|
+
type Error = Error;
|
|
25
|
+
|
|
26
|
+
type SerializeSeq = SeqSerializer<'r>;
|
|
27
|
+
type SerializeTuple = SeqSerializer<'r>;
|
|
28
|
+
type SerializeTupleStruct = SeqSerializer<'r>;
|
|
29
|
+
type SerializeTupleVariant = TupleVariantSerializer<'r>;
|
|
30
|
+
type SerializeMap = MapSerializer<'r>;
|
|
31
|
+
type SerializeStruct = StructSerializer<'r>;
|
|
32
|
+
type SerializeStructVariant = StructVariantSerializer<'r>;
|
|
33
|
+
|
|
34
|
+
fn serialize_bool(self, value: bool) -> Result<Self::Ok, Self::Error> {
|
|
35
|
+
Ok(value.into_value_with(self.ruby))
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
fn serialize_i8(self, value: i8) -> Result<Self::Ok, Self::Error> {
|
|
39
|
+
Ok(value.into_value_with(self.ruby))
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
fn serialize_i16(self, value: i16) -> Result<Self::Ok, Self::Error> {
|
|
43
|
+
Ok(value.into_value_with(self.ruby))
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
fn serialize_i32(self, value: i32) -> Result<Self::Ok, Self::Error> {
|
|
47
|
+
Ok(value.into_value_with(self.ruby))
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
fn serialize_i64(self, value: i64) -> Result<Self::Ok, Self::Error> {
|
|
51
|
+
Ok(value.into_value_with(self.ruby))
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
fn serialize_i128(self, value: i128) -> Result<Self::Ok, Self::Error> {
|
|
55
|
+
Ok(value.into_value_with(self.ruby))
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
fn serialize_u8(self, value: u8) -> Result<Self::Ok, Self::Error> {
|
|
59
|
+
Ok(value.into_value_with(self.ruby))
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
fn serialize_u16(self, value: u16) -> Result<Self::Ok, Self::Error> {
|
|
63
|
+
Ok(value.into_value_with(self.ruby))
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
fn serialize_u32(self, value: u32) -> Result<Self::Ok, Self::Error> {
|
|
67
|
+
Ok(value.into_value_with(self.ruby))
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
fn serialize_u64(self, value: u64) -> Result<Self::Ok, Self::Error> {
|
|
71
|
+
Ok(value.into_value_with(self.ruby))
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
fn serialize_u128(self, value: u128) -> Result<Self::Ok, Self::Error> {
|
|
75
|
+
Ok(value.into_value_with(self.ruby))
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
fn serialize_f32(self, value: f32) -> Result<Self::Ok, Self::Error> {
|
|
79
|
+
Ok(value.into_value_with(self.ruby))
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
fn serialize_f64(self, value: f64) -> Result<Self::Ok, Self::Error> {
|
|
83
|
+
Ok(value.into_value_with(self.ruby))
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
fn serialize_char(self, value: char) -> Result<Self::Ok, Self::Error> {
|
|
87
|
+
Ok(value.into_value_with(self.ruby))
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
fn serialize_str(self, value: &str) -> Result<Self::Ok, Self::Error> {
|
|
91
|
+
Ok(value.into_value_with(self.ruby))
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
fn serialize_bytes(self, value: &[u8]) -> Result<Self::Ok, Self::Error> {
|
|
95
|
+
Ok(self.ruby.str_from_slice(value).into_value_with(self.ruby))
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
fn serialize_none(self) -> Result<Self::Ok, Self::Error> {
|
|
99
|
+
Ok(().into_value_with(self.ruby))
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
fn serialize_some<Value>(self, value: &Value) -> Result<Self::Ok, Self::Error>
|
|
103
|
+
where
|
|
104
|
+
Value: Serialize + ?Sized,
|
|
105
|
+
{
|
|
106
|
+
value.serialize(self)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
|
|
110
|
+
Ok(().into_value_with(self.ruby))
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
fn serialize_unit_struct(self, _name: &'static str) -> Result<Self::Ok, Self::Error> {
|
|
114
|
+
Ok(().into_value_with(self.ruby))
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
fn serialize_unit_variant(
|
|
118
|
+
self,
|
|
119
|
+
_name: &'static str,
|
|
120
|
+
_index: u32,
|
|
121
|
+
variant: &'static str,
|
|
122
|
+
) -> Result<Self::Ok, Self::Error> {
|
|
123
|
+
Ok(variant.into_value_with(self.ruby))
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
fn serialize_newtype_struct<Value>(
|
|
127
|
+
self,
|
|
128
|
+
_name: &'static str,
|
|
129
|
+
value: &Value,
|
|
130
|
+
) -> Result<Self::Ok, Self::Error>
|
|
131
|
+
where
|
|
132
|
+
Value: Serialize + ?Sized,
|
|
133
|
+
{
|
|
134
|
+
value.serialize(self)
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
fn serialize_newtype_variant<Value>(
|
|
138
|
+
self,
|
|
139
|
+
_name: &'static str,
|
|
140
|
+
_index: u32,
|
|
141
|
+
variant: &'static str,
|
|
142
|
+
value: &Value,
|
|
143
|
+
) -> Result<Self::Ok, Self::Error>
|
|
144
|
+
where
|
|
145
|
+
Value: Serialize + ?Sized,
|
|
146
|
+
{
|
|
147
|
+
nest(self.ruby, variant, value.serialize(self)?)
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
|
|
151
|
+
Ok(SeqSerializer::new(
|
|
152
|
+
self.ruby,
|
|
153
|
+
self.ruby.ary_new_capa(len.unwrap_or(0)),
|
|
154
|
+
))
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, Self::Error> {
|
|
158
|
+
self.serialize_seq(Some(len))
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
fn serialize_tuple_struct(
|
|
162
|
+
self,
|
|
163
|
+
_name: &'static str,
|
|
164
|
+
len: usize,
|
|
165
|
+
) -> Result<Self::SerializeTupleStruct, Self::Error> {
|
|
166
|
+
self.serialize_seq(Some(len))
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
fn serialize_tuple_variant(
|
|
170
|
+
self,
|
|
171
|
+
_name: &'static str,
|
|
172
|
+
_index: u32,
|
|
173
|
+
variant: &'static str,
|
|
174
|
+
len: usize,
|
|
175
|
+
) -> Result<Self::SerializeTupleVariant, Self::Error> {
|
|
176
|
+
Ok(TupleVariantSerializer::new(
|
|
177
|
+
self.ruby,
|
|
178
|
+
variant,
|
|
179
|
+
self.ruby.ary_new_capa(len),
|
|
180
|
+
))
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
|
|
184
|
+
Ok(MapSerializer::new(
|
|
185
|
+
self.ruby,
|
|
186
|
+
hash_with_capacity(self.ruby, len.unwrap_or(0)),
|
|
187
|
+
))
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
fn serialize_struct(
|
|
191
|
+
self,
|
|
192
|
+
name: &'static str,
|
|
193
|
+
len: usize,
|
|
194
|
+
) -> Result<Self::SerializeStruct, Self::Error> {
|
|
195
|
+
Ok(StructSerializer::new(self.ruby, name, len))
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
fn serialize_struct_variant(
|
|
199
|
+
self,
|
|
200
|
+
_name: &'static str,
|
|
201
|
+
_index: u32,
|
|
202
|
+
variant: &'static str,
|
|
203
|
+
len: usize,
|
|
204
|
+
) -> Result<Self::SerializeStructVariant, Self::Error> {
|
|
205
|
+
Ok(StructVariantSerializer::new(
|
|
206
|
+
self.ruby,
|
|
207
|
+
variant,
|
|
208
|
+
hash_with_capacity(self.ruby, len),
|
|
209
|
+
))
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
pub(super) fn hash_with_capacity(ruby: &Ruby, capacity: usize) -> RHash {
|
|
214
|
+
// Magnus passes the capacity to Ruby as a C long, which can be narrower than usize.
|
|
215
|
+
if c_long::try_from(capacity).is_ok() {
|
|
216
|
+
ruby.hash_new_capa(capacity)
|
|
217
|
+
} else {
|
|
218
|
+
ruby.hash_new()
|
|
219
|
+
}
|
|
220
|
+
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
use ::serde::{Serialize, ser::SerializeStruct};
|
|
2
1
|
use magnus::{Integer, IntoValue, RHash, RString, Ruby, Value, value::ReprValue};
|
|
2
|
+
use serde::{Serialize, ser::SerializeStruct};
|
|
3
3
|
|
|
4
|
-
use super::Serializer;
|
|
5
|
-
use crate::
|
|
4
|
+
use super::serializer::{Serializer, hash_with_capacity};
|
|
5
|
+
use crate::{JSON_NUMBER_TOKEN, error::Error};
|
|
6
6
|
|
|
7
7
|
/// Serde struct serializer, including `serde_json` arbitrary-precision numbers.
|
|
8
8
|
pub(super) enum StructSerializer<'ruby> {
|
|
@@ -24,7 +24,7 @@ impl<'ruby> StructSerializer<'ruby> {
|
|
|
24
24
|
} else {
|
|
25
25
|
Self::Map {
|
|
26
26
|
ruby,
|
|
27
|
-
hash: ruby
|
|
27
|
+
hash: hash_with_capacity(ruby, len),
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
30
|
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
use super::{Serializer, enums::nest};
|
|
2
|
+
use crate::error::Error;
|
|
3
|
+
use magnus::{RHash, Ruby, Value};
|
|
4
|
+
use serde::{Serialize, ser::SerializeStructVariant};
|
|
5
|
+
|
|
6
|
+
pub(super) struct StructVariantSerializer<'r> {
|
|
7
|
+
ruby: &'r Ruby,
|
|
8
|
+
variant: &'static str,
|
|
9
|
+
hash: RHash,
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
impl<'r> StructVariantSerializer<'r> {
|
|
13
|
+
pub fn new(ruby: &'r Ruby, variant: &'static str, hash: RHash) -> StructVariantSerializer<'r> {
|
|
14
|
+
StructVariantSerializer {
|
|
15
|
+
ruby,
|
|
16
|
+
variant,
|
|
17
|
+
hash,
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
impl<'r> SerializeStructVariant for StructVariantSerializer<'r> {
|
|
23
|
+
type Ok = Value;
|
|
24
|
+
type Error = Error;
|
|
25
|
+
|
|
26
|
+
fn serialize_field<Value>(
|
|
27
|
+
&mut self,
|
|
28
|
+
name: &'static str,
|
|
29
|
+
value: &Value,
|
|
30
|
+
) -> Result<(), Self::Error>
|
|
31
|
+
where
|
|
32
|
+
Value: Serialize + ?Sized,
|
|
33
|
+
{
|
|
34
|
+
self.hash
|
|
35
|
+
.aset(
|
|
36
|
+
self.ruby.to_symbol(name),
|
|
37
|
+
value.serialize(Serializer::new(self.ruby))?,
|
|
38
|
+
)
|
|
39
|
+
.map_err(Into::into)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
fn end(self) -> Result<Self::Ok, Self::Error> {
|
|
43
|
+
nest(self.ruby, self.variant, self.hash)
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
use super::{Serializer, enums::nest};
|
|
2
|
+
use crate::error::Error;
|
|
3
|
+
use magnus::{RArray, Ruby, Value};
|
|
4
|
+
use serde::{Serialize, ser::SerializeTupleVariant};
|
|
5
|
+
|
|
6
|
+
pub(super) struct TupleVariantSerializer<'r> {
|
|
7
|
+
ruby: &'r Ruby,
|
|
8
|
+
variant: &'static str,
|
|
9
|
+
array: RArray,
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
impl<'r> TupleVariantSerializer<'r> {
|
|
13
|
+
pub fn new(ruby: &'r Ruby, variant: &'static str, array: RArray) -> TupleVariantSerializer<'r> {
|
|
14
|
+
TupleVariantSerializer {
|
|
15
|
+
ruby,
|
|
16
|
+
variant,
|
|
17
|
+
array,
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
impl<'r> SerializeTupleVariant for TupleVariantSerializer<'r> {
|
|
23
|
+
type Ok = Value;
|
|
24
|
+
type Error = Error;
|
|
25
|
+
|
|
26
|
+
fn serialize_field<Field>(&mut self, field: &Field) -> Result<(), Self::Error>
|
|
27
|
+
where
|
|
28
|
+
Field: Serialize + ?Sized,
|
|
29
|
+
{
|
|
30
|
+
self.array
|
|
31
|
+
.push(field.serialize(Serializer::new(self.ruby))?)
|
|
32
|
+
.map_err(Into::into)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
fn end(self) -> Result<Self::Ok, self::Error> {
|
|
36
|
+
nest(self.ruby, self.variant, self.array)
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
use std::{collections::BTreeMap, fmt};
|
|
2
2
|
|
|
3
|
-
use ::serde::{Deserialize, Serialize, de::Visitor};
|
|
4
3
|
use magnus::{RArray, RHash, RString, Ruby, Value, encoding::EncodingCapable, value::ReprValue};
|
|
4
|
+
use serde::{Deserialize, Serialize};
|
|
5
|
+
use serde_bytes::ByteBuf;
|
|
5
6
|
|
|
6
|
-
use super::{
|
|
7
|
+
use super::{deserialize, deserialize_json, serialize};
|
|
7
8
|
|
|
8
9
|
#[derive(Debug, Deserialize, PartialEq, Serialize)]
|
|
9
10
|
struct Record {
|
|
@@ -30,62 +31,13 @@ enum State {
|
|
|
30
31
|
Failed { message: String },
|
|
31
32
|
}
|
|
32
33
|
|
|
33
|
-
/// Byte sequence that exercises Serde's owned byte-buffer entry points.
|
|
34
|
-
#[derive(Debug, PartialEq)]
|
|
35
|
-
struct ByteBuffer(Vec<u8>);
|
|
36
|
-
|
|
37
|
-
impl Serialize for ByteBuffer {
|
|
38
|
-
fn serialize<Serializer>(
|
|
39
|
-
&self,
|
|
40
|
-
serializer: Serializer,
|
|
41
|
-
) -> Result<Serializer::Ok, Serializer::Error>
|
|
42
|
-
where
|
|
43
|
-
Serializer: ::serde::Serializer,
|
|
44
|
-
{
|
|
45
|
-
serializer.serialize_bytes(&self.0)
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
impl<'de> Deserialize<'de> for ByteBuffer {
|
|
50
|
-
fn deserialize<Deserializer>(deserializer: Deserializer) -> Result<Self, Deserializer::Error>
|
|
51
|
-
where
|
|
52
|
-
Deserializer: ::serde::Deserializer<'de>,
|
|
53
|
-
{
|
|
54
|
-
struct ByteBufferVisitor;
|
|
55
|
-
|
|
56
|
-
impl<'de> Visitor<'de> for ByteBufferVisitor {
|
|
57
|
-
type Value = ByteBuffer;
|
|
58
|
-
|
|
59
|
-
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
60
|
-
formatter.write_str("an owned byte buffer")
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
fn visit_bytes<Error>(self, value: &[u8]) -> Result<Self::Value, Error>
|
|
64
|
-
where
|
|
65
|
-
Error: ::serde::de::Error,
|
|
66
|
-
{
|
|
67
|
-
Ok(ByteBuffer(value.to_vec()))
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
fn visit_byte_buf<Error>(self, value: Vec<u8>) -> Result<Self::Value, Error>
|
|
71
|
-
where
|
|
72
|
-
Error: ::serde::de::Error,
|
|
73
|
-
{
|
|
74
|
-
Ok(ByteBuffer(value))
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
deserializer.deserialize_byte_buf(ByteBufferVisitor)
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
34
|
/// Assert that a supported Serde value survives conversion through Ruby.
|
|
83
35
|
fn assert_ruby_round_trip<Input>(ruby: &Ruby, input: Input) -> Result<(), magnus::Error>
|
|
84
36
|
where
|
|
85
37
|
Input: fmt::Debug + PartialEq + Serialize + for<'de> Deserialize<'de>,
|
|
86
38
|
{
|
|
87
39
|
let value: Value = serialize(ruby, &input)?;
|
|
88
|
-
let output: Input =
|
|
40
|
+
let output: Input = deserialize(ruby, value)?;
|
|
89
41
|
assert_eq!(input, output);
|
|
90
42
|
Ok(())
|
|
91
43
|
}
|
|
@@ -110,22 +62,20 @@ fn assert_scalar_conversions(ruby: &Ruby) -> Result<(), magnus::Error> {
|
|
|
110
62
|
let character: RString = serialize(ruby, &'\u{2603}')?;
|
|
111
63
|
assert_eq!("\u{2603}", character.to_string()?);
|
|
112
64
|
assert!(character.enc_get() == ruby.utf8_encindex());
|
|
113
|
-
let output: char =
|
|
65
|
+
let output: char = deserialize(ruby, character)?;
|
|
114
66
|
assert_eq!('\u{2603}', output);
|
|
115
67
|
|
|
116
68
|
let string: RString = serialize(ruby, &"Hello, world!")?;
|
|
117
69
|
assert_eq!("Hello, world!", string.to_string()?);
|
|
118
70
|
assert!(string.enc_get() == ruby.utf8_encindex());
|
|
119
|
-
assert_eq!(
|
|
120
|
-
"Hello, world!",
|
|
121
|
-
deserialize_ruby::<_, String>(ruby, string)?
|
|
122
|
-
);
|
|
71
|
+
assert_eq!("Hello, world!", deserialize::<_, String>(ruby, string)?);
|
|
123
72
|
|
|
124
|
-
let bytes =
|
|
73
|
+
let bytes = ByteBuf::from(b"\0binary\xff".as_slice());
|
|
125
74
|
let value: RString = serialize(ruby, &bytes)?;
|
|
126
|
-
assert_eq!(bytes.
|
|
75
|
+
assert_eq!(bytes.as_ref(), value.to_bytes().as_ref());
|
|
127
76
|
assert!(value.enc_get() == ruby.ascii8bit_encindex());
|
|
128
|
-
|
|
77
|
+
let output: ByteBuf = deserialize(ruby, value)?;
|
|
78
|
+
assert_eq!(bytes, output);
|
|
129
79
|
|
|
130
80
|
let none: Value = serialize(ruby, &Option::<u64>::None)?;
|
|
131
81
|
assert!(none.is_nil());
|
|
@@ -157,7 +107,7 @@ fn assert_composite_conversions(ruby: &Ruby) -> Result<(), magnus::Error> {
|
|
|
157
107
|
let value: RHash = serialize(ruby, &record)?;
|
|
158
108
|
let count: u64 = value.aref(ruby.to_symbol("count"))?;
|
|
159
109
|
assert_eq!(record.count, count);
|
|
160
|
-
assert_eq!(record,
|
|
110
|
+
assert_eq!(record, deserialize(ruby, value)?);
|
|
161
111
|
|
|
162
112
|
let map = BTreeMap::from([("first".to_owned(), 1_u64), ("second".to_owned(), 2)]);
|
|
163
113
|
assert_ruby_round_trip(ruby, map)?;
|
|
@@ -190,43 +140,44 @@ fn assert_integer_conversions(ruby: &Ruby) -> Result<(), magnus::Error> {
|
|
|
190
140
|
let value: Value = serialize(ruby, &i128::MIN)?;
|
|
191
141
|
let decimal: String = value.funcall("to_s", ())?;
|
|
192
142
|
assert_eq!(i128::MIN.to_string(), decimal);
|
|
193
|
-
assert_eq!(i128::MIN,
|
|
143
|
+
assert_eq!(i128::MIN, deserialize::<_, i128>(ruby, value)?);
|
|
194
144
|
assert_eq!(i128::MIN, deserialize_json::<_, i128>(ruby, value)?);
|
|
195
145
|
|
|
196
146
|
let value: Value = serialize(ruby, &u128::MAX)?;
|
|
197
147
|
let decimal: String = value.funcall("to_s", ())?;
|
|
198
148
|
assert_eq!(u128::MAX.to_string(), decimal);
|
|
199
|
-
assert_eq!(u128::MAX,
|
|
149
|
+
assert_eq!(u128::MAX, deserialize::<_, u128>(ruby, value)?);
|
|
200
150
|
assert_eq!(u128::MAX, deserialize_json::<_, u128>(ruby, value)?);
|
|
201
151
|
|
|
202
152
|
let value: Value = serialize(ruby, &u64::MAX)?;
|
|
203
|
-
assert_eq!(u64::MAX,
|
|
153
|
+
assert_eq!(u64::MAX, deserialize::<_, u64>(ruby, value)?);
|
|
204
154
|
assert_eq!(u64::MAX, deserialize_json::<_, u64>(ruby, value)?);
|
|
205
155
|
Ok(())
|
|
206
156
|
}
|
|
207
157
|
|
|
208
158
|
/// Verify unsupported borrowed values and malformed enum shapes return errors.
|
|
209
159
|
fn assert_conversion_errors(ruby: &Ruby) -> Result<(), magnus::Error> {
|
|
210
|
-
let error =
|
|
160
|
+
let error = deserialize::<_, &str>(ruby, ruby.str_new("borrowed"))
|
|
211
161
|
.expect_err("borrowed strings must not outlive their Ruby value");
|
|
212
162
|
assert_type_error(error, "expected a borrowed string");
|
|
213
163
|
|
|
214
|
-
let error =
|
|
164
|
+
let error = deserialize::<_, &[u8]>(ruby, ruby.str_new("borrowed"))
|
|
215
165
|
.expect_err("borrowed byte slices must not outlive their Ruby value");
|
|
216
166
|
assert_type_error(error, "can't deserialize into byte slice");
|
|
217
167
|
|
|
218
168
|
let variants = ruby.hash_new();
|
|
219
169
|
variants.aset("Ready", ruby.qnil())?;
|
|
220
170
|
variants.aset("Count", 1)?;
|
|
221
|
-
let error =
|
|
171
|
+
let error = deserialize::<_, State>(ruby, variants)
|
|
222
172
|
.expect_err("an enum hash must contain exactly one variant");
|
|
223
173
|
assert_type_error(error, "Hash of length 2");
|
|
224
174
|
Ok(())
|
|
225
175
|
}
|
|
226
176
|
|
|
227
177
|
#[test]
|
|
178
|
+
#[allow(unsafe_code)]
|
|
228
179
|
fn retains_ruby_serde_conversion_surface() -> Result<(), magnus::Error> {
|
|
229
|
-
// SAFETY: this
|
|
180
|
+
// SAFETY: this process initializes the embedded Ruby VM once on this thread.
|
|
230
181
|
let ruby = unsafe { magnus::embed::init() };
|
|
231
182
|
|
|
232
183
|
assert_scalar_conversions(&ruby)?;
|
data/src/client/body/json.rs
CHANGED
|
@@ -9,8 +9,9 @@
|
|
|
9
9
|
|
|
10
10
|
use ::serde::{Deserialize, Serialize};
|
|
11
11
|
use magnus::{Error, Ruby, TryConvert, Value};
|
|
12
|
+
use serde_magnus::deserialize_json;
|
|
12
13
|
|
|
13
|
-
use crate::
|
|
14
|
+
use crate::error::json_serialization_error;
|
|
14
15
|
|
|
15
16
|
/// An owned JSON tree shared by request and response conversion.
|
|
16
17
|
///
|
data/src/client/resp.rs
CHANGED
|
@@ -262,7 +262,7 @@ impl Response {
|
|
|
262
262
|
let response = rb_self.response(ruby, false)?;
|
|
263
263
|
let json =
|
|
264
264
|
rt::block_on(ruby, response.json::<Json>())?.map_err(|err| wreq_error(ruby, err))?;
|
|
265
|
-
|
|
265
|
+
serde_magnus::serialize(ruby, &json)
|
|
266
266
|
}
|
|
267
267
|
|
|
268
268
|
/// Yield response body chunks to the given Ruby block.
|
data/src/error.rs
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
use std::{
|
|
2
|
-
cell::{BorrowError, BorrowMutError},
|
|
3
|
-
fmt,
|
|
4
|
-
};
|
|
1
|
+
use std::cell::{BorrowError, BorrowMutError};
|
|
5
2
|
|
|
6
3
|
use magnus::{
|
|
7
|
-
Error as MagnusError, RModule, Ruby,
|
|
8
|
-
value::Lazy,
|
|
4
|
+
Error as MagnusError, RModule, Ruby, exception::ExceptionClass, prelude::*, value::Lazy,
|
|
9
5
|
};
|
|
6
|
+
use serde_magnus::options::contextualize_magnus_error;
|
|
10
7
|
use tokio::sync::mpsc::error::SendError;
|
|
11
8
|
|
|
12
9
|
const RACE_CONDITION_ERROR_MSG: &str = r#"Due to Rust's memory management with borrowing,
|
|
@@ -190,23 +187,6 @@ pub fn json_serialization_error(ruby: &Ruby, err: MagnusError) -> MagnusError {
|
|
|
190
187
|
)
|
|
191
188
|
}
|
|
192
189
|
|
|
193
|
-
/// Prefix a Magnus error while preserving its original Ruby exception class.
|
|
194
|
-
pub(crate) fn contextualize_magnus_error(
|
|
195
|
-
err: MagnusError,
|
|
196
|
-
context: fmt::Arguments<'_>,
|
|
197
|
-
) -> MagnusError {
|
|
198
|
-
match err.error_type() {
|
|
199
|
-
ErrorType::Error(class, message) => {
|
|
200
|
-
MagnusError::new(*class, format!("{context}: {message}"))
|
|
201
|
-
}
|
|
202
|
-
ErrorType::Exception(exception) => MagnusError::new(
|
|
203
|
-
exception.exception_class(),
|
|
204
|
-
format!("{context}: {exception}"),
|
|
205
|
-
),
|
|
206
|
-
ErrorType::Jump(_) => err,
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
|
|
210
190
|
/// Add an option name while preserving the original Ruby exception class.
|
|
211
191
|
pub fn option_value_error(option: &str, err: MagnusError) -> MagnusError {
|
|
212
192
|
contextualize_magnus_error(err, format_args!("invalid value for :{option}"))
|
data/src/lib.rs
CHANGED
data/src/options.rs
CHANGED
|
@@ -1,20 +1,15 @@
|
|
|
1
1
|
//! Parsing and validation for Ruby option hashes.
|
|
2
2
|
|
|
3
|
-
use std::
|
|
3
|
+
use std::fmt;
|
|
4
4
|
|
|
5
5
|
use ::serde::{
|
|
6
6
|
Deserialize, Deserializer,
|
|
7
7
|
de::{DeserializeOwned, Visitor},
|
|
8
8
|
};
|
|
9
9
|
use magnus::{Error, RHash, Ruby, TryConvert, Value, value::ReprValue};
|
|
10
|
+
use serde_magnus::options::{deserialize_options, validate_option_keys};
|
|
10
11
|
|
|
11
|
-
use crate::{
|
|
12
|
-
error::{argument_error, option_value_error, type_error},
|
|
13
|
-
serde,
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
/// Private Serde newtype used to recognize values converted by Magnus.
|
|
17
|
-
pub(crate) const NATIVE_OPTION_TOKEN: &str = "$wreq::private::NativeOption";
|
|
12
|
+
use crate::error::{argument_error, option_value_error, type_error};
|
|
18
13
|
|
|
19
14
|
/// An option represented in the Serde schema but converted through Magnus.
|
|
20
15
|
///
|
|
@@ -46,22 +41,29 @@ impl<'de, T> Deserialize<'de> for NativeOption<T> {
|
|
|
46
41
|
where
|
|
47
42
|
D: Deserializer<'de>,
|
|
48
43
|
{
|
|
49
|
-
struct NativeOptionVisitor
|
|
44
|
+
struct NativeOptionVisitor;
|
|
50
45
|
|
|
51
|
-
impl<
|
|
52
|
-
type Value =
|
|
46
|
+
impl<'de> Visitor<'de> for NativeOptionVisitor {
|
|
47
|
+
type Value = ();
|
|
53
48
|
|
|
54
49
|
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
55
50
|
formatter.write_str("a Ruby-native option")
|
|
56
51
|
}
|
|
57
52
|
|
|
58
|
-
fn
|
|
59
|
-
Ok(
|
|
53
|
+
fn visit_none<E>(self) -> Result<Self::Value, E> {
|
|
54
|
+
Ok(())
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
fn visit_some<D>(self, _deserializer: D) -> Result<Self::Value, D::Error>
|
|
58
|
+
where
|
|
59
|
+
D: Deserializer<'de>,
|
|
60
|
+
{
|
|
61
|
+
Ok(())
|
|
60
62
|
}
|
|
61
63
|
}
|
|
62
64
|
|
|
63
|
-
deserializer
|
|
64
|
-
|
|
65
|
+
deserializer.deserialize_option(NativeOptionVisitor)?;
|
|
66
|
+
Ok(Self::default())
|
|
65
67
|
}
|
|
66
68
|
}
|
|
67
69
|
|
|
@@ -114,7 +116,7 @@ impl<'ruby> Options<'ruby> {
|
|
|
114
116
|
where
|
|
115
117
|
T: DeserializeOwned,
|
|
116
118
|
{
|
|
117
|
-
|
|
119
|
+
validate_option_keys::<_, T>(self.ruby, self.hash)?;
|
|
118
120
|
Ok(self)
|
|
119
121
|
}
|
|
120
122
|
|
|
@@ -128,7 +130,7 @@ impl<'ruby> Options<'ruby> {
|
|
|
128
130
|
where
|
|
129
131
|
T: DeserializeOwned,
|
|
130
132
|
{
|
|
131
|
-
|
|
133
|
+
deserialize_options(self.ruby, self.hash)
|
|
132
134
|
}
|
|
133
135
|
|
|
134
136
|
/// Return whether an option key is present, including a `nil` value.
|