wreq 1.3.2.4 → 1.3.3

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.
Files changed (88) hide show
  1. checksums.yaml +4 -4
  2. data/Cargo.lock +470 -50
  3. data/Cargo.toml +19 -16
  4. data/README.md +5 -12
  5. data/Rakefile +3 -9
  6. data/crates/wreq-util/Cargo.toml +1 -1
  7. data/crates/wreq-util/README.md +4 -2
  8. data/crates/wreq-util/examples/emulate.rs +7 -3
  9. data/crates/wreq-util/examples/tower_delay.rs +4 -4
  10. data/crates/wreq-util/src/emulate/macros.rs +197 -32
  11. data/crates/wreq-util/src/emulate/profile/chrome/header.rs +116 -0
  12. data/crates/wreq-util/src/emulate/profile/chrome/http2.rs +65 -0
  13. data/crates/wreq-util/src/emulate/profile/chrome/tls.rs +268 -0
  14. data/crates/wreq-util/src/emulate/profile/chrome.rs +2269 -0
  15. data/crates/wreq-util/src/emulate/profile/firefox/header.rs +54 -48
  16. data/crates/wreq-util/src/emulate/profile/firefox/http2.rs +102 -92
  17. data/crates/wreq-util/src/emulate/profile/firefox/tls.rs +146 -155
  18. data/crates/wreq-util/src/emulate/profile/firefox.rs +440 -312
  19. data/crates/wreq-util/src/emulate/profile/okhttp.rs +2 -3
  20. data/crates/wreq-util/src/emulate/profile/opera/header.rs +29 -0
  21. data/crates/wreq-util/src/emulate/profile/opera/http2.rs +50 -0
  22. data/crates/wreq-util/src/emulate/profile/opera/tls.rs +103 -0
  23. data/crates/wreq-util/src/emulate/profile/opera.rs +299 -0
  24. data/crates/wreq-util/src/emulate/profile.rs +10 -10
  25. data/crates/wreq-util/src/emulate.rs +1 -34
  26. data/crates/wreq-util/src/tower/delay/layer.rs +5 -5
  27. data/crates/wreq-util/src/tower/delay.rs +4 -4
  28. data/crates/wreq-util/tests/client.rs +50 -187
  29. data/crates/wreq-util/tests/emulate_chrome.rs +0 -17
  30. data/crates/wreq-util/tests/emulate_firefox.rs +26 -36
  31. data/crates/wreq-util/tests/emulate_opera.rs +0 -24
  32. data/crates/wreq-util/tests/support/mod.rs +1 -1
  33. data/lib/wreq_ruby/client.rb +27 -13
  34. data/lib/wreq_ruby/emulate.rb +3 -28
  35. data/lib/wreq_ruby/http.rb +9 -2
  36. data/src/client/body/json.rs +1 -2
  37. data/src/client/resp.rs +1 -1
  38. data/src/client.rs +8 -4
  39. data/src/emulate.rs +5 -28
  40. data/src/error.rs +23 -3
  41. data/src/lib.rs +3 -2
  42. data/src/options.rs +17 -19
  43. data/{crates/serde-magnus/src → src/serde}/de/array_deserializer.rs +2 -2
  44. data/{crates/serde-magnus/src → src/serde}/de/array_enumerator.rs +1 -1
  45. data/{crates/serde-magnus/src → src/serde}/de/deserializer.rs +31 -25
  46. data/{crates/serde-magnus/src → src/serde}/de/enum_deserializer.rs +3 -3
  47. data/{crates/serde-magnus/src → src/serde}/de/hash_deserializer.rs +2 -2
  48. data/{crates/serde-magnus/src → src/serde}/de/number_deserializer.rs +2 -2
  49. data/{crates/serde-magnus/src → src/serde}/de/variant_deserializer.rs +20 -11
  50. data/src/serde/de.rs +33 -0
  51. data/src/serde/error.rs +146 -0
  52. data/src/serde/ser/enums.rs +14 -0
  53. data/{crates/serde-magnus/src → src/serde}/ser/map_serializer.rs +2 -2
  54. data/src/serde/ser/seq_serializer.rs +71 -0
  55. data/src/serde/ser/serializer.rs +194 -0
  56. data/{crates/serde-magnus/src → src/serde}/ser/struct_serializer.rs +4 -4
  57. data/src/serde/ser/struct_variant_serializer.rs +44 -0
  58. data/src/serde/ser/tuple_variant_serializer.rs +41 -0
  59. data/src/serde/ser.rs +18 -0
  60. data/{crates/serde-magnus/src → src/serde}/tests.rs +68 -19
  61. data/src/serde.rs +202 -0
  62. data/test/emulation_test.rb +104 -21
  63. data/test/option_validation_test.rb +13 -4
  64. data/wreq.gemspec +15 -3
  65. metadata +29 -35
  66. data/VERSION +0 -1
  67. data/crates/README.md +0 -6
  68. data/crates/serde-magnus/Cargo.toml +0 -29
  69. data/crates/serde-magnus/LICENSE +0 -19
  70. data/crates/serde-magnus/README.md +0 -147
  71. data/crates/serde-magnus/src/de/mod.rs +0 -554
  72. data/crates/serde-magnus/src/error.rs +0 -84
  73. data/crates/serde-magnus/src/lib.rs +0 -137
  74. data/crates/serde-magnus/src/options.rs +0 -151
  75. data/crates/serde-magnus/src/ser/enums.rs +0 -11
  76. data/crates/serde-magnus/src/ser/mod.rs +0 -428
  77. data/crates/serde-magnus/src/ser/seq_serializer.rs +0 -68
  78. data/crates/serde-magnus/src/ser/serializer.rs +0 -220
  79. data/crates/serde-magnus/src/ser/struct_variant_serializer.rs +0 -45
  80. data/crates/serde-magnus/src/ser/tuple_variant_serializer.rs +0 -38
  81. data/crates/wreq-util/src/emulate/profile/chromium/chrome.rs +0 -731
  82. data/crates/wreq-util/src/emulate/profile/chromium/edge.rs +0 -418
  83. data/crates/wreq-util/src/emulate/profile/chromium/header.rs +0 -74
  84. data/crates/wreq-util/src/emulate/profile/chromium/http2.rs +0 -59
  85. data/crates/wreq-util/src/emulate/profile/chromium/opera.rs +0 -312
  86. data/crates/wreq-util/src/emulate/profile/chromium/tls.rs +0 -310
  87. data/crates/wreq-util/src/emulate/profile/chromium.rs +0 -9
  88. data/test/emulation_client_hello_test.rb +0 -142
data/src/client.rs CHANGED
@@ -76,11 +76,11 @@ struct Builder {
76
76
  tcp_reuse_address: Option<bool>,
77
77
 
78
78
  // ========= Connection pool options =========
79
- /// Set an optional timeout for idle sockets being kept-alive. (in seconds)
79
+ /// Idle connection reuse timeout, in seconds.
80
80
  pool_idle_timeout: Option<u64>,
81
- /// Sets the maximum idle connection per host allowed in the pool.
81
+ /// Maximum idle HTTP/1 connections retained per compatibility group.
82
82
  pool_max_idle_per_host: Option<usize>,
83
- /// Sets the maximum number of connections in the pool.
83
+ /// Maximum compatibility groups retaining reusable connections; zero is unlimited.
84
84
  pool_max_size: Option<usize>,
85
85
 
86
86
  // ========= Protocol options =========
@@ -88,6 +88,8 @@ struct Builder {
88
88
  http1_only: Option<bool>,
89
89
  /// Whether to use the HTTP/2 protocol only.
90
90
  http2_only: Option<bool>,
91
+ /// Whether to use HTTP/3 over QUIC without falling back to TCP.
92
+ http3_only: Option<bool>,
91
93
  /// Whether to use HTTPS only.
92
94
  https_only: Option<bool>,
93
95
 
@@ -147,6 +149,7 @@ impl Builder {
147
149
  .reject_conflicts([
148
150
  (stringify!(http1_only), builder.http1_only == Some(true)),
149
151
  (stringify!(http2_only), builder.http2_only == Some(true)),
152
+ (stringify!(http3_only), builder.http3_only == Some(true)),
150
153
  ])
151
154
  .reject_conflicts([
152
155
  (stringify!(proxy), options.is_non_nil(stringify!(proxy))),
@@ -222,7 +225,7 @@ impl Client {
222
225
  .map(|jar| jar.clone_store(ruby))
223
226
  .transpose()?;
224
227
  let result = gvl::nogvl(|| {
225
- let mut builder = wreq::Client::builder();
228
+ let mut builder = wreq::Client::builder_tokio();
226
229
 
227
230
  // Emulation options.
228
231
  apply_option!(set_if_some_inner, builder, params.emulation, emulation);
@@ -345,6 +348,7 @@ impl Client {
345
348
  // Protocol options.
346
349
  apply_option!(set_if_true, builder, params.http1_only, http1_only, false);
347
350
  apply_option!(set_if_true, builder, params.http2_only, http2_only, false);
351
+ apply_option!(set_if_true, builder, params.http3_only, http3_only, false);
348
352
  apply_option!(set_if_some, builder, params.https_only, https_only);
349
353
 
350
354
  // TLS options.
data/src/emulate.rs CHANGED
@@ -95,7 +95,7 @@ define_ruby_enum!(
95
95
  Chrome155,
96
96
  Chrome156,
97
97
  Chrome157,
98
- Chrome158,
98
+
99
99
  Edge101,
100
100
  Edge122,
101
101
  Edge127,
@@ -115,16 +115,7 @@ define_ruby_enum!(
115
115
  Edge146,
116
116
  Edge147,
117
117
  Edge148,
118
- Edge149,
119
- Edge150,
120
- Edge151,
121
- Edge152,
122
- Edge153,
123
- Edge154,
124
- Edge155,
125
- Edge156,
126
- Edge157,
127
- Edge158,
118
+
128
119
  Firefox109,
129
120
  Firefox117,
130
121
  Firefox128,
@@ -146,14 +137,7 @@ define_ruby_enum!(
146
137
  Firefox150,
147
138
  Firefox151,
148
139
  Firefox152,
149
- Firefox153,
150
- Firefox154,
151
- Firefox155,
152
- Firefox156,
153
- Firefox157,
154
- Firefox158,
155
- Firefox159,
156
- Firefox160,
140
+
157
141
  SafariIos17_2,
158
142
  SafariIos17_4_1,
159
143
  SafariIos16_5,
@@ -183,6 +167,7 @@ define_ruby_enum!(
183
167
  SafariIos26_2,
184
168
  SafariIPad26,
185
169
  SafariIpad26_2,
170
+
186
171
  OkHttp3_9,
187
172
  OkHttp3_11,
188
173
  OkHttp3_13,
@@ -191,6 +176,7 @@ define_ruby_enum!(
191
176
  OkHttp4_10,
192
177
  OkHttp4_12,
193
178
  OkHttp5,
179
+
194
180
  Opera116,
195
181
  Opera117,
196
182
  Opera118,
@@ -207,15 +193,6 @@ define_ruby_enum!(
207
193
  Opera129,
208
194
  Opera130,
209
195
  Opera131,
210
- Opera132,
211
- Opera133,
212
- Opera134,
213
- Opera135,
214
- Opera136,
215
- Opera137,
216
- Opera138,
217
- Opera139,
218
- Opera140,
219
196
  );
220
197
 
221
198
  // Defines constant registration, `into_ffi`/`from_ffi`, and handlers for
data/src/error.rs CHANGED
@@ -1,9 +1,12 @@
1
- use std::cell::{BorrowError, BorrowMutError};
1
+ use std::{
2
+ cell::{BorrowError, BorrowMutError},
3
+ fmt,
4
+ };
2
5
 
3
6
  use magnus::{
4
- Error as MagnusError, RModule, Ruby, exception::ExceptionClass, prelude::*, value::Lazy,
7
+ Error as MagnusError, RModule, Ruby, error::ErrorType, exception::ExceptionClass, prelude::*,
8
+ value::Lazy,
5
9
  };
6
- use serde_magnus::options::contextualize_magnus_error;
7
10
  use tokio::sync::mpsc::error::SendError;
8
11
 
9
12
  const RACE_CONDITION_ERROR_MSG: &str = r#"Due to Rust's memory management with borrowing,
@@ -187,6 +190,23 @@ pub fn json_serialization_error(ruby: &Ruby, err: MagnusError) -> MagnusError {
187
190
  )
188
191
  }
189
192
 
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
+
190
210
  /// Add an option name while preserving the original Ruby exception class.
191
211
  pub fn option_value_error(option: &str, err: MagnusError) -> MagnusError {
192
212
  contextualize_magnus_error(err, format_args!("invalid value for :{option}"))
data/src/lib.rs CHANGED
@@ -14,6 +14,7 @@ mod header;
14
14
  mod http;
15
15
  mod options;
16
16
  mod rt;
17
+ mod serde;
17
18
  mod tls;
18
19
 
19
20
  use magnus::{Error, Module, Ruby, Value};
@@ -24,7 +25,7 @@ use crate::{
24
25
  };
25
26
 
26
27
  const RUBY_MODULE_NAME: &str = "Wreq";
27
- const VERSION: &str = include_str!("../VERSION");
28
+ const VERSION: &str = env!("CARGO_PKG_VERSION");
28
29
 
29
30
  /// Send a HTTP request.
30
31
  #[inline]
@@ -84,7 +85,7 @@ pub fn patch(ruby: &Ruby, args: &[Value]) -> Result<Response, magnus::Error> {
84
85
  #[magnus::init]
85
86
  fn init(ruby: &Ruby) -> Result<(), Error> {
86
87
  let gem_module = ruby.define_module(RUBY_MODULE_NAME)?;
87
- gem_module.const_set("VERSION", VERSION.trim_end())?;
88
+ gem_module.const_set("VERSION", VERSION)?;
88
89
  error::include(ruby, &gem_module)?;
89
90
  gem_module.define_module_function("request", magnus::function!(request, -1))?;
90
91
  gem_module.define_module_function("get", magnus::function!(get, -1))?;
data/src/options.rs CHANGED
@@ -1,15 +1,20 @@
1
1
  //! Parsing and validation for Ruby option hashes.
2
2
 
3
- use std::fmt;
3
+ use std::{fmt, marker::PhantomData};
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};
11
10
 
12
- use crate::error::{argument_error, option_value_error, type_error};
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";
13
18
 
14
19
  /// An option represented in the Serde schema but converted through Magnus.
15
20
  ///
@@ -41,29 +46,22 @@ impl<'de, T> Deserialize<'de> for NativeOption<T> {
41
46
  where
42
47
  D: Deserializer<'de>,
43
48
  {
44
- struct NativeOptionVisitor;
49
+ struct NativeOptionVisitor<T>(PhantomData<fn() -> T>);
45
50
 
46
- impl<'de> Visitor<'de> for NativeOptionVisitor {
47
- type Value = ();
51
+ impl<T> Visitor<'_> for NativeOptionVisitor<T> {
52
+ type Value = NativeOption<T>;
48
53
 
49
54
  fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
50
55
  formatter.write_str("a Ruby-native option")
51
56
  }
52
57
 
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(())
58
+ fn visit_unit<E>(self) -> Result<Self::Value, E> {
59
+ Ok(NativeOption::default())
62
60
  }
63
61
  }
64
62
 
65
- deserializer.deserialize_option(NativeOptionVisitor)?;
66
- Ok(Self::default())
63
+ deserializer
64
+ .deserialize_newtype_struct(NATIVE_OPTION_TOKEN, NativeOptionVisitor(PhantomData))
67
65
  }
68
66
  }
69
67
 
@@ -116,7 +114,7 @@ impl<'ruby> Options<'ruby> {
116
114
  where
117
115
  T: DeserializeOwned,
118
116
  {
119
- validate_option_keys::<_, T>(self.ruby, self.hash)?;
117
+ serde::validate_option_keys::<_, T>(self.ruby, self.hash)?;
120
118
  Ok(self)
121
119
  }
122
120
 
@@ -130,7 +128,7 @@ impl<'ruby> Options<'ruby> {
130
128
  where
131
129
  T: DeserializeOwned,
132
130
  {
133
- deserialize_options(self.ruby, self.hash)
131
+ serde::deserialize_options(self.ruby, self.hash)
134
132
  }
135
133
 
136
134
  /// Return whether an option key is present, including a `nil` value.
@@ -1,8 +1,8 @@
1
+ use ::serde::de::{DeserializeSeed, SeqAccess};
1
2
  use magnus::{RArray, Ruby};
2
- use serde::de::{DeserializeSeed, SeqAccess};
3
3
 
4
4
  use super::{Deserializer, Mode, array_enumerator::ArrayEnumerator};
5
- use crate::error::Error;
5
+ use crate::serde::Error;
6
6
 
7
7
  /// Serde sequence access over a Ruby array.
8
8
  pub(super) struct ArrayDeserializer<'ruby> {
@@ -1,6 +1,6 @@
1
1
  use magnus::{RArray, Ruby, Value};
2
2
 
3
- use crate::error::Error;
3
+ use super::super::Error;
4
4
 
5
5
  /// Index-based Ruby array iterator that avoids Ruby Enumerator fiber overhead.
6
6
  pub(super) struct ArrayEnumerator<'ruby> {
@@ -1,17 +1,15 @@
1
+ use ::serde::forward_to_deserialize_any;
1
2
  use magnus::{
2
3
  Fixnum, Float, Integer, RArray, RBignum, RHash, RString, Ruby, Symbol, Value,
3
4
  value::{Qfalse, Qtrue, ReprValue},
4
5
  };
5
- use serde::forward_to_deserialize_any;
6
6
 
7
+ use super::super::{Error, MAX_JSON_NESTING};
7
8
  use super::{
8
- array_deserializer::ArrayDeserializer, class_name, enum_deserializer::EnumDeserializer,
9
+ array_deserializer::ArrayDeserializer, enum_deserializer::EnumDeserializer,
9
10
  hash_deserializer::HashDeserializer, number_deserializer::NumberDeserializer,
10
11
  };
11
- use crate::error::Error;
12
-
13
- /// Maximum nesting of Ruby arrays and hashes in JSON input.
14
- const MAX_JSON_NESTING: usize = 100;
12
+ use crate::options::NATIVE_OPTION_TOKEN;
15
13
 
16
14
  /// Implement typed Serde integer entry points with Magnus's checked conversions.
17
15
  macro_rules! impl_deserialize_integers {
@@ -19,7 +17,7 @@ macro_rules! impl_deserialize_integers {
19
17
  $(
20
18
  fn $method<Visitor>(self, visitor: Visitor) -> Result<Visitor::Value, Self::Error>
21
19
  where
22
- Visitor: serde::de::Visitor<'de>,
20
+ Visitor: ::serde::de::Visitor<'de>,
23
21
  {
24
22
  match Integer::from_value(self.value) {
25
23
  Some(value) => visitor.$visit(value.$convert()?),
@@ -49,7 +47,7 @@ impl Mode {
49
47
  }
50
48
 
51
49
  /// Serde deserializer over Ruby values.
52
- pub(crate) struct Deserializer<'ruby> {
50
+ pub(in crate::serde) struct Deserializer<'ruby> {
53
51
  ruby: &'ruby Ruby,
54
52
  value: Value,
55
53
  depth: usize,
@@ -58,12 +56,12 @@ pub(crate) struct Deserializer<'ruby> {
58
56
 
59
57
  impl<'ruby> Deserializer<'ruby> {
60
58
  /// Create a deserializer with native `serde_magnus` Ruby behavior.
61
- pub(crate) fn new(ruby: &'ruby Ruby, value: Value) -> Self {
59
+ pub(in crate::serde) fn new_ruby(ruby: &'ruby Ruby, value: Value) -> Self {
62
60
  Self::with_mode(ruby, value, 0, Mode::Ruby)
63
61
  }
64
62
 
65
63
  /// Create a deserializer that validates option keys without reading values.
66
- pub(crate) fn new_option_keys(ruby: &'ruby Ruby, value: Value) -> Self {
64
+ pub(in crate::serde) fn new_option_keys(ruby: &'ruby Ruby, value: Value) -> Self {
67
65
  Self::with_mode(ruby, value, 0, Mode::OptionKeys)
68
66
  }
69
67
 
@@ -98,12 +96,12 @@ impl<'ruby> Deserializer<'ruby> {
98
96
  }
99
97
  }
100
98
 
101
- impl<'de> serde::Deserializer<'de> for Deserializer<'_> {
99
+ impl<'de> ::serde::Deserializer<'de> for Deserializer<'_> {
102
100
  type Error = Error;
103
101
 
104
102
  fn deserialize_any<Visitor>(self, visitor: Visitor) -> Result<Visitor::Value, Self::Error>
105
103
  where
106
- Visitor: serde::de::Visitor<'de>,
104
+ Visitor: ::serde::de::Visitor<'de>,
107
105
  {
108
106
  if self.value.is_nil() {
109
107
  return visitor.visit_unit();
@@ -159,34 +157,37 @@ impl<'de> serde::Deserializer<'de> for Deserializer<'_> {
159
157
 
160
158
  Err(Error::type_error(format!(
161
159
  "can't deserialize {}",
162
- class_name(self.value)
160
+ // SAFETY: conversion runs while the Ruby GVL is held.
161
+ unsafe { self.value.classname() }
163
162
  )))
164
163
  }
165
164
 
166
165
  fn deserialize_bytes<Visitor>(self, _visitor: Visitor) -> Result<Visitor::Value, Self::Error>
167
166
  where
168
- Visitor: serde::de::Visitor<'de>,
167
+ Visitor: ::serde::de::Visitor<'de>,
169
168
  {
170
169
  Err(Error::type_error("can't deserialize into byte slice"))
171
170
  }
172
171
 
173
172
  fn deserialize_byte_buf<Visitor>(self, visitor: Visitor) -> Result<Visitor::Value, Self::Error>
174
173
  where
175
- Visitor: serde::de::Visitor<'de>,
174
+ Visitor: ::serde::de::Visitor<'de>,
176
175
  {
177
176
  if let Some(string) = RString::from_value(self.value) {
178
- visitor.visit_byte_buf(string.to_bytes().into())
177
+ // SAFETY: the bytes are copied before any further Ruby API call.
178
+ visitor.visit_byte_buf(unsafe { string.as_slice() }.to_owned())
179
179
  } else {
180
180
  Err(Error::type_error(format!(
181
181
  "no implicit conversion of {} to String",
182
- class_name(self.value)
182
+ // SAFETY: conversion runs while the Ruby GVL is held.
183
+ unsafe { self.value.classname() }
183
184
  )))
184
185
  }
185
186
  }
186
187
 
187
188
  fn deserialize_option<Visitor>(self, visitor: Visitor) -> Result<Visitor::Value, Self::Error>
188
189
  where
189
- Visitor: serde::de::Visitor<'de>,
190
+ Visitor: ::serde::de::Visitor<'de>,
190
191
  {
191
192
  if matches!(self.mode, Mode::OptionKeys) || self.value.is_nil() {
192
193
  visitor.visit_none()
@@ -202,7 +203,7 @@ impl<'de> serde::Deserializer<'de> for Deserializer<'_> {
202
203
  visitor: Visitor,
203
204
  ) -> Result<Visitor::Value, Self::Error>
204
205
  where
205
- Visitor: serde::de::Visitor<'de>,
206
+ Visitor: ::serde::de::Visitor<'de>,
206
207
  {
207
208
  if let Some(variant) = RString::from_value(self.value) {
208
209
  return visitor.visit_enum(EnumDeserializer::new(
@@ -233,19 +234,24 @@ impl<'de> serde::Deserializer<'de> for Deserializer<'_> {
233
234
 
234
235
  Err(Error::type_error(format!(
235
236
  "can't deserialize {} to Enum",
236
- class_name(self.value)
237
+ // SAFETY: conversion runs while the Ruby GVL is held.
238
+ unsafe { self.value.classname() }
237
239
  )))
238
240
  }
239
241
 
240
242
  fn deserialize_newtype_struct<Visitor>(
241
243
  self,
242
- _name: &'static str,
244
+ name: &'static str,
243
245
  visitor: Visitor,
244
246
  ) -> Result<Visitor::Value, Self::Error>
245
247
  where
246
- Visitor: serde::de::Visitor<'de>,
248
+ Visitor: ::serde::de::Visitor<'de>,
247
249
  {
248
- visitor.visit_newtype_struct(self)
250
+ if name == NATIVE_OPTION_TOKEN {
251
+ visitor.visit_unit()
252
+ } else {
253
+ visitor.visit_newtype_struct(self)
254
+ }
249
255
  }
250
256
 
251
257
  fn deserialize_ignored_any<Visitor>(
@@ -253,7 +259,7 @@ impl<'de> serde::Deserializer<'de> for Deserializer<'_> {
253
259
  visitor: Visitor,
254
260
  ) -> Result<Visitor::Value, Self::Error>
255
261
  where
256
- Visitor: serde::de::Visitor<'de>,
262
+ Visitor: ::serde::de::Visitor<'de>,
257
263
  {
258
264
  visitor.visit_unit()
259
265
  }
@@ -263,7 +269,7 @@ impl<'de> serde::Deserializer<'de> for Deserializer<'_> {
263
269
  visitor: Visitor,
264
270
  ) -> Result<Visitor::Value, Self::Error>
265
271
  where
266
- Visitor: serde::de::Visitor<'de>,
272
+ Visitor: ::serde::de::Visitor<'de>,
267
273
  {
268
274
  if matches!(self.mode, Mode::OptionKeys) {
269
275
  if let Some(value) = RString::from_value(self.value) {
@@ -1,8 +1,8 @@
1
+ use ::serde::de::{DeserializeSeed, EnumAccess, value::StringDeserializer};
1
2
  use magnus::{Ruby, Value};
2
- use serde::de::{DeserializeSeed, EnumAccess, IntoDeserializer};
3
3
 
4
4
  use super::{Mode, VariantDeserializer};
5
- use crate::error::Error;
5
+ use crate::serde::Error;
6
6
 
7
7
  /// Serde enum access over a Ruby string or one-entry hash.
8
8
  pub(super) struct EnumDeserializer<'ruby> {
@@ -41,7 +41,7 @@ impl<'ruby, 'de> EnumAccess<'de> for EnumDeserializer<'ruby> {
41
41
  Seed: DeserializeSeed<'de>,
42
42
  {
43
43
  let variant = VariantDeserializer::new(self.ruby, self.value, self.depth, self.mode);
44
- seed.deserialize(self.variant.into_deserializer())
44
+ seed.deserialize(StringDeserializer::<Error>::new(self.variant))
45
45
  .map(|value| (value, variant))
46
46
  }
47
47
  }
@@ -1,10 +1,10 @@
1
1
  use std::iter::Peekable;
2
2
 
3
+ use ::serde::de::{DeserializeSeed, MapAccess};
3
4
  use magnus::{RHash, RString, Ruby, Symbol, Value, r_hash::ForEach};
4
- use serde::de::{DeserializeSeed, MapAccess};
5
5
 
6
6
  use super::{Deserializer, Mode, array_enumerator::ArrayEnumerator};
7
- use crate::error::Error;
7
+ use crate::serde::Error;
8
8
 
9
9
  /// Serde map access over a Ruby hash.
10
10
  pub(super) struct HashDeserializer<'ruby> {
@@ -1,9 +1,9 @@
1
- use serde::de::{
1
+ use ::serde::de::{
2
2
  DeserializeSeed, MapAccess,
3
3
  value::{BorrowedStrDeserializer, StringDeserializer},
4
4
  };
5
5
 
6
- use crate::{JSON_NUMBER_TOKEN, error::Error};
6
+ use super::super::{Error, JSON_NUMBER_TOKEN};
7
7
 
8
8
  /// Serde map representation used by `serde_json` for arbitrary-precision numbers.
9
9
  pub(super) struct NumberDeserializer {
@@ -1,8 +1,8 @@
1
+ use ::serde::de::{DeserializeSeed, Unexpected, VariantAccess};
1
2
  use magnus::{RArray, RHash, Ruby, Value, value::ReprValue};
2
- use serde::de::{DeserializeSeed, Unexpected, VariantAccess};
3
3
 
4
- use super::{ArrayDeserializer, Deserializer, HashDeserializer, Mode, class_name};
5
- use crate::error::Error;
4
+ use super::{ArrayDeserializer, Deserializer, HashDeserializer, Mode};
5
+ use crate::serde::Error;
6
6
 
7
7
  /// Serde access to the payload of a Ruby enum representation.
8
8
  pub(super) struct VariantDeserializer<'ruby> {
@@ -36,8 +36,11 @@ impl<'de> VariantAccess<'de> for VariantDeserializer<'_> {
36
36
  if self.value.is_nil() {
37
37
  Ok(())
38
38
  } else {
39
- Err(serde::de::Error::invalid_type(
40
- Unexpected::Other(&class_name(self.value)),
39
+ Err(::serde::de::Error::invalid_type(
40
+ Unexpected::Other(
41
+ // SAFETY: conversion runs while the Ruby GVL is held.
42
+ &unsafe { self.value.classname() },
43
+ ),
41
44
  &"unit variant",
42
45
  ))
43
46
  }
@@ -58,14 +61,17 @@ impl<'de> VariantAccess<'de> for VariantDeserializer<'_> {
58
61
  visitor: Visitor,
59
62
  ) -> Result<Visitor::Value, Self::Error>
60
63
  where
61
- Visitor: serde::de::Visitor<'de>,
64
+ Visitor: ::serde::de::Visitor<'de>,
62
65
  {
63
66
  if let Some(array) = RArray::from_value(self.value) {
64
67
  let depth = self.nested_depth()?;
65
68
  visitor.visit_seq(ArrayDeserializer::new(self.ruby, array, depth, self.mode))
66
69
  } else {
67
- Err(serde::de::Error::invalid_type(
68
- Unexpected::Other(&class_name(self.value)),
70
+ Err(::serde::de::Error::invalid_type(
71
+ Unexpected::Other(
72
+ // SAFETY: conversion runs while the Ruby GVL is held.
73
+ &unsafe { self.value.classname() },
74
+ ),
69
75
  &"tuple variant",
70
76
  ))
71
77
  }
@@ -77,14 +83,17 @@ impl<'de> VariantAccess<'de> for VariantDeserializer<'_> {
77
83
  visitor: Visitor,
78
84
  ) -> Result<Visitor::Value, Self::Error>
79
85
  where
80
- Visitor: serde::de::Visitor<'de>,
86
+ Visitor: ::serde::de::Visitor<'de>,
81
87
  {
82
88
  if let Some(hash) = RHash::from_value(self.value) {
83
89
  let depth = self.nested_depth()?;
84
90
  visitor.visit_map(HashDeserializer::new(self.ruby, hash, depth, self.mode)?)
85
91
  } else {
86
- Err(serde::de::Error::invalid_type(
87
- Unexpected::Other(&class_name(self.value)),
92
+ Err(::serde::de::Error::invalid_type(
93
+ Unexpected::Other(
94
+ // SAFETY: conversion runs while the Ruby GVL is held.
95
+ &unsafe { self.value.classname() },
96
+ ),
88
97
  &"struct variant",
89
98
  ))
90
99
  }
data/src/serde/de.rs ADDED
@@ -0,0 +1,33 @@
1
+ mod array_deserializer;
2
+ mod array_enumerator;
3
+ mod deserializer;
4
+ mod enum_deserializer;
5
+ mod hash_deserializer;
6
+ mod number_deserializer;
7
+ mod variant_deserializer;
8
+
9
+ use ::serde::Deserialize;
10
+ use magnus::{Ruby, Value};
11
+
12
+ use super::Error;
13
+ use array_deserializer::ArrayDeserializer;
14
+ pub(super) use deserializer::Deserializer;
15
+ use deserializer::Mode;
16
+ use hash_deserializer::HashDeserializer;
17
+ use variant_deserializer::VariantDeserializer;
18
+
19
+ /// Deserialize one Ruby value using native Ruby data model semantics.
20
+ pub(super) fn deserialize_ruby<'de, Output>(ruby: &Ruby, value: Value) -> Result<Output, Error>
21
+ where
22
+ Output: Deserialize<'de>,
23
+ {
24
+ Output::deserialize(Deserializer::new_ruby(ruby, value))
25
+ }
26
+
27
+ /// Deserialize one Ruby value using JSON-specific conversion rules.
28
+ pub(super) fn deserialize_json<'de, Output>(ruby: &Ruby, value: Value) -> Result<Output, Error>
29
+ where
30
+ Output: Deserialize<'de>,
31
+ {
32
+ Output::deserialize(Deserializer::new_json(ruby, value))
33
+ }