From f461064180a5177aeb28fb7dd3ee8a2d90049cd2 Mon Sep 17 00:00:00 2001 From: gngpp Date: Thu, 19 Nov 2026 13:44:47 +0800 Subject: [PATCH 1/4] refactor: extract the Ruby Serde bridge into a local crate --- .github/workflows/ci.yml | 4 +- Cargo.lock | 25 +- Cargo.toml | 7 +- Rakefile | 6 +- crates/README.md | 1 + crates/serde-magnus/Cargo.toml | 29 + crates/serde-magnus/LICENSE | 19 + crates/serde-magnus/README.md | 147 +++++ .../src}/de/array_deserializer.rs | 4 +- .../serde-magnus/src}/de/array_enumerator.rs | 2 +- .../serde-magnus/src}/de/deserializer.rs | 56 +- .../serde-magnus/src}/de/enum_deserializer.rs | 6 +- .../serde-magnus/src}/de/hash_deserializer.rs | 4 +- crates/serde-magnus/src/de/mod.rs | 554 ++++++++++++++++++ .../src}/de/number_deserializer.rs | 4 +- .../src}/de/variant_deserializer.rs | 31 +- crates/serde-magnus/src/error.rs | 84 +++ crates/serde-magnus/src/lib.rs | 137 +++++ crates/serde-magnus/src/options.rs | 151 +++++ crates/serde-magnus/src/ser/enums.rs | 11 + .../serde-magnus/src}/ser/map_serializer.rs | 4 +- crates/serde-magnus/src/ser/mod.rs | 428 ++++++++++++++ crates/serde-magnus/src/ser/seq_serializer.rs | 68 +++ crates/serde-magnus/src/ser/serializer.rs | 206 +++++++ .../src}/ser/struct_serializer.rs | 8 +- .../src/ser/struct_variant_serializer.rs | 45 ++ .../src/ser/tuple_variant_serializer.rs | 38 ++ .../serde-magnus/src}/tests.rs | 87 +-- src/client/body/json.rs | 3 +- src/client/response.rs | 2 +- src/error.rs | 26 +- src/lib.rs | 1 - src/options.rs | 36 +- src/serde.rs | 202 ------- src/serde/de.rs | 33 -- src/serde/error.rs | 146 ----- src/serde/ser.rs | 18 - src/serde/ser/enums.rs | 14 - src/serde/ser/seq_serializer.rs | 71 --- src/serde/ser/serializer.rs | 194 ------ src/serde/ser/struct_variant_serializer.rs | 44 -- src/serde/ser/tuple_variant_serializer.rs | 41 -- 42 files changed, 2047 insertions(+), 950 deletions(-) create mode 100644 crates/serde-magnus/Cargo.toml create mode 100644 crates/serde-magnus/LICENSE create mode 100644 crates/serde-magnus/README.md rename {src/serde => crates/serde-magnus/src}/de/array_deserializer.rs (94%) rename {src/serde => crates/serde-magnus/src}/de/array_enumerator.rs (98%) rename {src/serde => crates/serde-magnus/src}/de/deserializer.rs (84%) rename {src/serde => crates/serde-magnus/src}/de/enum_deserializer.rs (86%) rename {src/serde => crates/serde-magnus/src}/de/hash_deserializer.rs (97%) create mode 100644 crates/serde-magnus/src/de/mod.rs rename {src/serde => crates/serde-magnus/src}/de/number_deserializer.rs (95%) rename {src/serde => crates/serde-magnus/src}/de/variant_deserializer.rs (71%) create mode 100644 crates/serde-magnus/src/error.rs create mode 100644 crates/serde-magnus/src/lib.rs create mode 100644 crates/serde-magnus/src/options.rs create mode 100644 crates/serde-magnus/src/ser/enums.rs rename {src/serde => crates/serde-magnus/src}/ser/map_serializer.rs (95%) create mode 100644 crates/serde-magnus/src/ser/mod.rs create mode 100644 crates/serde-magnus/src/ser/seq_serializer.rs create mode 100644 crates/serde-magnus/src/ser/serializer.rs rename {src/serde => crates/serde-magnus/src}/ser/struct_serializer.rs (93%) create mode 100644 crates/serde-magnus/src/ser/struct_variant_serializer.rs create mode 100644 crates/serde-magnus/src/ser/tuple_variant_serializer.rs rename {src/serde => crates/serde-magnus/src}/tests.rs (70%) delete mode 100644 src/serde.rs delete mode 100644 src/serde/de.rs delete mode 100644 src/serde/error.rs delete mode 100644 src/serde/ser.rs delete mode 100644 src/serde/ser/enums.rs delete mode 100644 src/serde/ser/seq_serializer.rs delete mode 100644 src/serde/ser/serializer.rs delete mode 100644 src/serde/ser/struct_variant_serializer.rs delete mode 100644 src/serde/ser/tuple_variant_serializer.rs diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fe162ad..cbab39a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -29,10 +29,10 @@ jobs: components: rustfmt, clippy - name: Run cargo fmt - run: cargo fmt -- --check + run: cargo fmt --all --check - name: Run cargo clippy - run: cargo clippy --all-targets --all-features -- -D warnings + run: cargo clippy --workspace --locked --all-targets --all-features -- -D warnings linux-tests: name: Linux Tests (${{ matrix.os }}, ${{ matrix.ruby }}) diff --git a/Cargo.lock b/Cargo.lock index 68756de..88db0ce 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1264,6 +1264,16 @@ dependencies = [ "serde_derive", ] +[[package]] +name = "serde_bytes" +version = "0.11.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5d440709e79d88e51ac01c4b72fc6cb7314017bb7da9eeff678aa94c10e3ea8" +dependencies = [ + "serde", + "serde_core", +] + [[package]] name = "serde_core" version = "1.0.229" @@ -1322,6 +1332,18 @@ dependencies = [ "zmij", ] +[[package]] +name = "serde_magnus" +version = "0.11.0" +dependencies = [ + "indexmap", + "magnus", + "serde", + "serde_bytes", + "serde_ignored", + "serde_path_to_error", +] + [[package]] name = "serde_path_to_error" version = "0.1.20" @@ -1937,9 +1959,8 @@ dependencies = [ "rb-sys", "rb-sys-env", "serde", - "serde_ignored", "serde_json", - "serde_path_to_error", + "serde_magnus", "tokio", "wreq", "wreq-util", diff --git a/Cargo.toml b/Cargo.toml index f1599b8..27033f0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,10 @@ edition = "2024" version = "1.3.5" rust-version = "1.98" +[workspace] +members = ["crates/serde-magnus"] +exclude = ["crates/wreq-util"] + [lib] crate-type = ["cdylib"] doc = false @@ -37,12 +41,11 @@ wreq = { version = "0.16.1", default-features = false, features = [ ] } wreq-util = { version = "3.0.0-rc", features = ["emulation-compression"] } serde = { version = "1.0.229", features = ["derive"] } -serde_ignored = "0.1.14" +serde_magnus = { path = "crates/serde-magnus" } serde_json = { version = "1.0.151", features = [ "arbitrary_precision", "preserve_order", ] } -serde_path_to_error = "0.1.20" indexmap = { version = "2.14.0", features = ["serde"] } cookie = "0.18.1" bytes = "1.12.1" diff --git a/Rakefile b/Rakefile index bdfb7c3..91f3ca2 100644 --- a/Rakefile +++ b/Rakefile @@ -44,17 +44,17 @@ end desc "Run Rust tests" task :cargo_test do - sh "cargo test" + sh "cargo test --workspace --locked" end desc "Format Rust code" task :fmt do - sh "cargo fmt" + sh "cargo fmt --all" end desc "Run Clippy linter" task :lint do - sh "cargo clippy -- -D warnings" + sh "cargo clippy --workspace --locked -- -D warnings" end desc "Build native gem for platform (e.g., rake 'native[x86_64-linux]')" diff --git a/crates/README.md b/crates/README.md index 3181970..018512d 100644 --- a/crates/README.md +++ b/crates/README.md @@ -1,5 +1,6 @@ # Local Rust dependencies +- [`serde_magnus`](serde-magnus/) is based on OneSignal's `serde_magnus`. The root package uses it as a path dependency and workspace member so it shares the lockfile and runs in `bundle exec rake test`. - [`wreq-util`](wreq-util/) is a local copy of [0x676e67/wreq-util](https://github.com/0x676e67/wreq-util). The root [`Cargo.toml`](../Cargo.toml) resolves it from `crates/wreq-util` through `[patch.crates-io]`. When building or testing `wreq-util` on its own, use the Git dependency patches from the root manifest of the same branch. Cargo does not inherit those patches from the parent package. diff --git a/crates/serde-magnus/Cargo.toml b/crates/serde-magnus/Cargo.toml new file mode 100644 index 0000000..b5a791c --- /dev/null +++ b/crates/serde-magnus/Cargo.toml @@ -0,0 +1,29 @@ +[package] +name = "serde_magnus" +version = "0.11.0" +description = "Serde integration for Magnus" +edition = "2024" +rust-version = "1.98" +publish = false +authors = [ + "Joe Wilm ", + "George Claghorn ", +] +repository = "https://github.com/OneSignal/serde-magnus" +documentation = "https://docs.rs/serde_magnus" +license = "MIT" +readme = "README.md" +keywords = ["serde", "magnus", "ruby"] +categories = ["development-tools::ffi"] + +[dependencies] +serde = "1.0" +magnus = { version = "0.9.0", features = ["bytes"] } +indexmap = "2.14.0" +serde_ignored = "0.1.14" +serde_path_to_error = "0.1.20" + +[dev-dependencies] +serde = { version = "1.0", features = ["derive"] } +serde_bytes = "0.11" +magnus = { version = "0.9.0", features = ["embed"] } diff --git a/crates/serde-magnus/LICENSE b/crates/serde-magnus/LICENSE new file mode 100644 index 0000000..9a53875 --- /dev/null +++ b/crates/serde-magnus/LICENSE @@ -0,0 +1,19 @@ +Copyright 2022 George Claghorn + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/crates/serde-magnus/README.md b/crates/serde-magnus/README.md new file mode 100644 index 0000000..7d79931 --- /dev/null +++ b/crates/serde-magnus/README.md @@ -0,0 +1,147 @@ +# `serde_magnus` + +`serde_magnus` converts between Rust and Ruby data structures using [Serde] and [Magnus]. + +[Serde]: https://github.com/serde-rs/serde +[Magnus]: https://github.com/matsadler/magnus + +## Local maintenance + +This crate is based on [OneSignal's serde_magnus 0.11.0 source][upstream]. It +uses the same Magnus 0.9 series and Rust 1.98 minimum as `wreq-ruby`. + +### Local patches + +- `src/de/` and `src/ser/` preserve arbitrary-size JSON integers in both + directions and support typed `i128` and `u128`. JSON input validates object keys, + finite floats, and container depth. The precision tests also cover the + private number token shared with `serde_json`. +- `src/options.rs` validates option names before converting values and attaches + the failing field path to Ruby exceptions. +- `src/error.rs` carries Serde errors until the conversion entry point can use + its Ruby handle. The deserializer and map serializer check iteration and map + state. + +To sync, diff this directory against the pinned source tree, reapply the local +changes to the new upstream commit, and update the pin below. Run +`cargo test --locked -p serde_magnus` and `bundle exec rake test` from the +repository root. The Ruby tests in `test/json_precision_test.rb` and +`test/option_validation_test.rb` exercise the request, response, and option +entry points. + +[upstream]: https://github.com/OneSignal/serde-magnus/tree/b84e35d03af9f84edf32e29c1fc022b4411ba9b9 + +## Upstream documentation + +* [API documentation](https://docs.rs/serde_magnus) +* [Releases/changelog](https://github.com/OneSignal/serde-magnus/releases) +* [`serde_magnus` on crates.io](https://crates.io/crates/serde_magnus) + +## Usage + +The [`serde_magnus::serialize`] function converts from a Rust type implementing the +[`serde::Serialize`] trait into a Ruby equivalent. + +```rust +use serde::{Serialize, Deserialize}; +use magnus::{eval, Ruby, Value}; +use serde_magnus::serialize; + +#[derive(Serialize, Deserialize, PartialEq, Debug)] +struct Post { + title: String, + content: String, + author: Author, + tags: Vec +} + +#[derive(Serialize, Deserialize, PartialEq, Debug)] +struct Author { + name: String, + email_address: String +} + +let post = Post { + title: "Spring carnival planning update".into(), + content: "Here's what's new.".into(), + author: Author { + name: "Martha".into(), + email_address: "martha@example.com".into() + }, + tags: vec![ + "carnival".into(), + "update".into() + ] +}; + +let ruby = Ruby::get().unwrap(); +let post: Value = serialize(&ruby, &post)?; + +assert!(eval!( + &ruby, + r#" + post == { + title: "Spring carnival planning update", + content: "Here's what's new.", + author: { + name: "Martha", + email_address: "martha@example.com" + }, + tags: ["carnival", "update"] + } + "#, + post +)?); +``` + +[`serde_magnus::deserialize`] converts from a Ruby value to a Rust type implementing +[`serde::Deserialize`]. + +```rust +use magnus::{RHash, Ruby}; +use serde_magnus::deserialize; + +let ruby = Ruby::get().unwrap(); +let post: RHash = eval!(&ruby, r#" + { + title: "Spring carnival planning update", + content: "Here's what's new.", + author: { + name: "Martha", + email_address: "martha@example.com" + }, + tags: ["carnival", "update"] + } +"#)?; + +let post: Post = deserialize(&ruby, post)?; + +assert_eq!( + Post { + title: "Spring carnival planning update".into(), + content: "Here's what's new.".into(), + author: Author { + name: "Martha".into(), + email_address: "martha@example.com".into(), + }, + tags: vec![ + "carnival".into(), + "update".into() + ] + }, + post +); +``` + +[`serde_magnus::serialize`]: https://docs.rs/serde_magnus/latest/serde_magnus/fn.serialize.html +[`serde::Serialize`]: https://docs.rs/serde/latest/serde/trait.Serialize.html +[`serde_magnus::deserialize`]: https://docs.rs/serde_magnus/latest/serde_magnus/fn.deserialize.html +[`serde::Deserialize`]: https://docs.rs/serde/latest/serde/trait.Deserialize.html + +## Requirements + +The local crate shares the Rust and Ruby requirements of `wreq-ruby`. + +## License + +`serde_magnus` is released under the terms of the MIT License. See `LICENSE` for details. diff --git a/src/serde/de/array_deserializer.rs b/crates/serde-magnus/src/de/array_deserializer.rs similarity index 94% rename from src/serde/de/array_deserializer.rs rename to crates/serde-magnus/src/de/array_deserializer.rs index fecb3e9..90ca993 100644 --- a/src/serde/de/array_deserializer.rs +++ b/crates/serde-magnus/src/de/array_deserializer.rs @@ -1,8 +1,8 @@ -use ::serde::de::{DeserializeSeed, SeqAccess}; use magnus::{RArray, Ruby}; +use serde::de::{DeserializeSeed, SeqAccess}; use super::{Deserializer, Mode, array_enumerator::ArrayEnumerator}; -use crate::serde::Error; +use crate::error::Error; /// Serde sequence access over a Ruby array. pub(super) struct ArrayDeserializer<'ruby> { diff --git a/src/serde/de/array_enumerator.rs b/crates/serde-magnus/src/de/array_enumerator.rs similarity index 98% rename from src/serde/de/array_enumerator.rs rename to crates/serde-magnus/src/de/array_enumerator.rs index 1f1d67f..f8759c3 100644 --- a/src/serde/de/array_enumerator.rs +++ b/crates/serde-magnus/src/de/array_enumerator.rs @@ -1,6 +1,6 @@ use magnus::{RArray, Ruby, Value}; -use super::super::Error; +use crate::error::Error; /// Index-based Ruby array iterator that avoids Ruby Enumerator fiber overhead. pub(super) struct ArrayEnumerator<'ruby> { diff --git a/src/serde/de/deserializer.rs b/crates/serde-magnus/src/de/deserializer.rs similarity index 84% rename from src/serde/de/deserializer.rs rename to crates/serde-magnus/src/de/deserializer.rs index a8db24e..1c4700b 100644 --- a/src/serde/de/deserializer.rs +++ b/crates/serde-magnus/src/de/deserializer.rs @@ -1,15 +1,17 @@ -use ::serde::forward_to_deserialize_any; use magnus::{ Fixnum, Float, Integer, RArray, RBignum, RHash, RString, Ruby, Symbol, Value, value::{Qfalse, Qtrue, ReprValue}, }; +use serde::forward_to_deserialize_any; -use super::super::{Error, MAX_JSON_NESTING}; use super::{ - array_deserializer::ArrayDeserializer, enum_deserializer::EnumDeserializer, + array_deserializer::ArrayDeserializer, class_name, enum_deserializer::EnumDeserializer, hash_deserializer::HashDeserializer, number_deserializer::NumberDeserializer, }; -use crate::options::NATIVE_OPTION_TOKEN; +use crate::error::Error; + +/// Maximum nesting of Ruby arrays and hashes in JSON input. +const MAX_JSON_NESTING: usize = 100; /// Implement typed Serde integer entry points with Magnus's checked conversions. macro_rules! impl_deserialize_integers { @@ -17,7 +19,7 @@ macro_rules! impl_deserialize_integers { $( fn $method(self, visitor: Visitor) -> Result where - Visitor: ::serde::de::Visitor<'de>, + Visitor: serde::de::Visitor<'de>, { match Integer::from_value(self.value) { Some(value) => visitor.$visit(value.$convert()?), @@ -47,7 +49,7 @@ impl Mode { } /// Serde deserializer over Ruby values. -pub(in crate::serde) struct Deserializer<'ruby> { +pub(crate) struct Deserializer<'ruby> { ruby: &'ruby Ruby, value: Value, depth: usize, @@ -56,12 +58,12 @@ pub(in crate::serde) struct Deserializer<'ruby> { impl<'ruby> Deserializer<'ruby> { /// Create a deserializer with native `serde_magnus` Ruby behavior. - pub(in crate::serde) fn new_ruby(ruby: &'ruby Ruby, value: Value) -> Self { + pub(crate) fn new(ruby: &'ruby Ruby, value: Value) -> Self { Self::with_mode(ruby, value, 0, Mode::Ruby) } /// Create a deserializer that validates option keys without reading values. - pub(in crate::serde) fn new_option_keys(ruby: &'ruby Ruby, value: Value) -> Self { + pub(crate) fn new_option_keys(ruby: &'ruby Ruby, value: Value) -> Self { Self::with_mode(ruby, value, 0, Mode::OptionKeys) } @@ -96,12 +98,12 @@ impl<'ruby> Deserializer<'ruby> { } } -impl<'de> ::serde::Deserializer<'de> for Deserializer<'_> { +impl<'de> serde::Deserializer<'de> for Deserializer<'_> { type Error = Error; fn deserialize_any(self, visitor: Visitor) -> Result where - Visitor: ::serde::de::Visitor<'de>, + Visitor: serde::de::Visitor<'de>, { if self.value.is_nil() { return visitor.visit_unit(); @@ -157,37 +159,34 @@ impl<'de> ::serde::Deserializer<'de> for Deserializer<'_> { Err(Error::type_error(format!( "can't deserialize {}", - // SAFETY: conversion runs while the Ruby GVL is held. - unsafe { self.value.classname() } + class_name(self.value) ))) } fn deserialize_bytes(self, _visitor: Visitor) -> Result where - Visitor: ::serde::de::Visitor<'de>, + Visitor: serde::de::Visitor<'de>, { Err(Error::type_error("can't deserialize into byte slice")) } fn deserialize_byte_buf(self, visitor: Visitor) -> Result where - Visitor: ::serde::de::Visitor<'de>, + Visitor: serde::de::Visitor<'de>, { if let Some(string) = RString::from_value(self.value) { - // SAFETY: the bytes are copied before any further Ruby API call. - visitor.visit_byte_buf(unsafe { string.as_slice() }.to_owned()) + visitor.visit_byte_buf(string.to_bytes().into()) } else { Err(Error::type_error(format!( "no implicit conversion of {} to String", - // SAFETY: conversion runs while the Ruby GVL is held. - unsafe { self.value.classname() } + class_name(self.value) ))) } } fn deserialize_option(self, visitor: Visitor) -> Result where - Visitor: ::serde::de::Visitor<'de>, + Visitor: serde::de::Visitor<'de>, { if matches!(self.mode, Mode::OptionKeys) || self.value.is_nil() { visitor.visit_none() @@ -203,7 +202,7 @@ impl<'de> ::serde::Deserializer<'de> for Deserializer<'_> { visitor: Visitor, ) -> Result where - Visitor: ::serde::de::Visitor<'de>, + Visitor: serde::de::Visitor<'de>, { if let Some(variant) = RString::from_value(self.value) { return visitor.visit_enum(EnumDeserializer::new( @@ -234,24 +233,19 @@ impl<'de> ::serde::Deserializer<'de> for Deserializer<'_> { Err(Error::type_error(format!( "can't deserialize {} to Enum", - // SAFETY: conversion runs while the Ruby GVL is held. - unsafe { self.value.classname() } + class_name(self.value) ))) } fn deserialize_newtype_struct( self, - name: &'static str, + _name: &'static str, visitor: Visitor, ) -> Result where - Visitor: ::serde::de::Visitor<'de>, + Visitor: serde::de::Visitor<'de>, { - if name == NATIVE_OPTION_TOKEN { - visitor.visit_unit() - } else { - visitor.visit_newtype_struct(self) - } + visitor.visit_newtype_struct(self) } fn deserialize_ignored_any( @@ -259,7 +253,7 @@ impl<'de> ::serde::Deserializer<'de> for Deserializer<'_> { visitor: Visitor, ) -> Result where - Visitor: ::serde::de::Visitor<'de>, + Visitor: serde::de::Visitor<'de>, { visitor.visit_unit() } @@ -269,7 +263,7 @@ impl<'de> ::serde::Deserializer<'de> for Deserializer<'_> { visitor: Visitor, ) -> Result where - Visitor: ::serde::de::Visitor<'de>, + Visitor: serde::de::Visitor<'de>, { if matches!(self.mode, Mode::OptionKeys) { if let Some(value) = RString::from_value(self.value) { diff --git a/src/serde/de/enum_deserializer.rs b/crates/serde-magnus/src/de/enum_deserializer.rs similarity index 86% rename from src/serde/de/enum_deserializer.rs rename to crates/serde-magnus/src/de/enum_deserializer.rs index f759c04..877bdd5 100644 --- a/src/serde/de/enum_deserializer.rs +++ b/crates/serde-magnus/src/de/enum_deserializer.rs @@ -1,8 +1,8 @@ -use ::serde::de::{DeserializeSeed, EnumAccess, value::StringDeserializer}; use magnus::{Ruby, Value}; +use serde::de::{DeserializeSeed, EnumAccess, IntoDeserializer}; use super::{Mode, VariantDeserializer}; -use crate::serde::Error; +use crate::error::Error; /// Serde enum access over a Ruby string or one-entry hash. pub(super) struct EnumDeserializer<'ruby> { @@ -41,7 +41,7 @@ impl<'ruby, 'de> EnumAccess<'de> for EnumDeserializer<'ruby> { Seed: DeserializeSeed<'de>, { let variant = VariantDeserializer::new(self.ruby, self.value, self.depth, self.mode); - seed.deserialize(StringDeserializer::::new(self.variant)) + seed.deserialize(self.variant.into_deserializer()) .map(|value| (value, variant)) } } diff --git a/src/serde/de/hash_deserializer.rs b/crates/serde-magnus/src/de/hash_deserializer.rs similarity index 97% rename from src/serde/de/hash_deserializer.rs rename to crates/serde-magnus/src/de/hash_deserializer.rs index a45ab56..039af63 100644 --- a/src/serde/de/hash_deserializer.rs +++ b/crates/serde-magnus/src/de/hash_deserializer.rs @@ -1,10 +1,10 @@ use std::iter::Peekable; -use ::serde::de::{DeserializeSeed, MapAccess}; use magnus::{RHash, RString, Ruby, Symbol, Value, r_hash::ForEach}; +use serde::de::{DeserializeSeed, MapAccess}; use super::{Deserializer, Mode, array_enumerator::ArrayEnumerator}; -use crate::serde::Error; +use crate::error::Error; /// Serde map access over a Ruby hash. pub(super) struct HashDeserializer<'ruby> { diff --git a/crates/serde-magnus/src/de/mod.rs b/crates/serde-magnus/src/de/mod.rs new file mode 100644 index 0000000..7857589 --- /dev/null +++ b/crates/serde-magnus/src/de/mod.rs @@ -0,0 +1,554 @@ +mod deserializer; + +mod array_deserializer; +mod array_enumerator; +mod enum_deserializer; +mod hash_deserializer; +mod number_deserializer; +mod variant_deserializer; + +pub(crate) use self::deserializer::Deserializer; +use self::deserializer::Mode; + +use self::{ + array_deserializer::ArrayDeserializer, hash_deserializer::HashDeserializer, + variant_deserializer::VariantDeserializer, +}; + +use magnus::{Error, IntoValue, Ruby, Value, value::ReprValue}; +use serde::Deserialize; + +/// Deserialize a Ruby [`Value`][`magnus::Value`] to Rust. +/// +/// See [`crate::serialize`] for the expected Ruby formats of various Rust types. +/// +/// # Errors +/// +/// Returns a Ruby error when the input cannot be converted to `Output`. +/// +/// ### Primitive types +/// +/// #### Unit type +/// +/// ``` +/// # // SAFETY: this doctest initializes Ruby once on its current thread. +/// # let ruby = unsafe { magnus::embed::init() }; +/// # +/// use magnus::{eval, Value}; +/// use serde_magnus::deserialize; +/// +/// let input: Value = eval!(&ruby, "nil")?; +/// let output: () = deserialize(&ruby, input)?; +/// assert_eq!((), output); +/// # +/// # Ok::<(), magnus::Error>(()) +/// ``` +/// +/// #### Booleans +/// +/// ``` +/// # use magnus::{eval, Value}; +/// # use serde_magnus::deserialize; +/// # +/// # // SAFETY: this doctest initializes Ruby once on its current thread. +/// # let ruby = unsafe { magnus::embed::init() }; +/// # +/// let input: Value = eval!(&ruby, "true")?; +/// let output: bool = deserialize(&ruby, input)?; +/// assert_eq!(true, output); +/// # +/// # Ok::<(), magnus::Error>(()) +/// ``` +/// +/// #### Integers +/// +/// ``` +/// # use magnus::{eval, Value}; +/// # use serde_magnus::deserialize; +/// # +/// # // SAFETY: this doctest initializes Ruby once on its current thread. +/// # let ruby = unsafe { magnus::embed::init() }; +/// # +/// let input: Value = eval!(&ruby, "1234")?; +/// let output: i64 = deserialize(&ruby, input)?; +/// assert_eq!(1234, output); +/// # +/// # Ok::<(), magnus::Error>(()) +/// ``` +/// +/// #### Floats +/// +/// ``` +/// # use magnus::{eval, Value}; +/// # use serde_magnus::deserialize; +/// # +/// # // SAFETY: this doctest initializes Ruby once on its current thread. +/// # let ruby = unsafe { magnus::embed::init() }; +/// # +/// let input: Value = eval!(&ruby, "3.14")?; +/// let output: f64 = deserialize(&ruby, input)?; +/// assert_eq!(3.14, output); +/// # +/// # Ok::<(), magnus::Error>(()) +/// ``` +/// +/// #### Strings +/// +/// ``` +/// # use magnus::{eval, Value}; +/// # use serde_magnus::deserialize; +/// # +/// # // SAFETY: this doctest initializes Ruby once on its current thread. +/// # let ruby = unsafe { magnus::embed::init() }; +/// # +/// let input: Value = eval!(&ruby, r#""Hello, world!""#)?; +/// let output: String = deserialize(&ruby, input)?; +/// assert_eq!("Hello, world!", output); +/// # +/// # Ok::<(), magnus::Error>(()) +/// ``` +/// +/// ### `Option` +/// +/// ``` +/// # use magnus::{eval, Value}; +/// # use serde_magnus::deserialize; +/// # +/// # // SAFETY: this doctest initializes Ruby once on its current thread. +/// # let ruby = unsafe { magnus::embed::init() }; +/// # +/// let input: Value = eval!(&ruby, "nil")?; +/// let output: Option = deserialize(&ruby, input)?; +/// assert_eq!(None, output); +/// +/// let input: Value = eval!(&ruby, "1234")?; +/// let output: Option = deserialize(&ruby, input)?; +/// assert_eq!(Some(1234), output); +/// # +/// # Ok::<(), magnus::Error>(()) +/// ``` +/// +/// ### `Result` +/// +/// ``` +/// # use magnus::{eval, Value}; +/// # use serde_magnus::deserialize; +/// # +/// # // SAFETY: this doctest initializes Ruby once on its current thread. +/// # let ruby = unsafe { magnus::embed::init() }; +/// # +/// let input: Value = eval!(&ruby, "{ 'Ok' => 1234 }")?; +/// let output: Result = deserialize(&ruby, input)?; +/// assert_eq!(Ok(1234), output); +/// +/// let input: Value = eval!(&ruby, "{ 'Err' => 'something went wrong' }")?; +/// let output: Result = deserialize(&ruby, input)?; +/// assert_eq!(Err("something went wrong".into()), output); +/// # +/// # Ok::<(), magnus::Error>(()) +/// ``` +/// +/// ### Structs +/// +/// #### Unit structs +/// +/// ``` +/// # use magnus::{eval, Value}; +/// # use serde_magnus::deserialize; +/// # +/// # // SAFETY: this doctest initializes Ruby once on its current thread. +/// # let ruby = unsafe { magnus::embed::init() }; +/// # +/// use serde::Deserialize; +/// +/// #[derive(PartialEq, Debug, Deserialize)] +/// struct Foo; +/// +/// let input: Value = eval!(&ruby, "nil")?; +/// let output: Foo = deserialize(&ruby, input)?; +/// assert_eq!(Foo, output); +/// # +/// # Ok::<(), magnus::Error>(()) +/// ``` +/// +/// #### Newtype structs +/// +/// ``` +/// # use magnus::{eval, Value}; +/// # use serde_magnus::deserialize; +/// # +/// # // SAFETY: this doctest initializes Ruby once on its current thread. +/// # let ruby = unsafe { magnus::embed::init() }; +/// # +/// use serde::Deserialize; +/// +/// #[derive(PartialEq, Debug, Deserialize)] +/// struct Foo(u16); +/// +/// let input: Value = eval!(&ruby, "1234")?; +/// let output: Foo = deserialize(&ruby, input)?; +/// assert_eq!(Foo(1234), output); +/// # +/// # Ok::<(), magnus::Error>(()) +/// ``` +/// +/// #### Tuple structs +/// +/// ``` +/// # use magnus::{eval, Value}; +/// # use serde_magnus::deserialize; +/// # +/// # // SAFETY: this doctest initializes Ruby once on its current thread. +/// # let ruby = unsafe { magnus::embed::init() }; +/// # +/// use serde::Deserialize; +/// +/// #[derive(PartialEq, Debug, Deserialize)] +/// struct Foo(u16, bool, String); +/// +/// let input: Value = eval!(&ruby, "[123, true, 'Hello, world!']")?; +/// let output: Foo = deserialize(&ruby, input)?; +/// assert_eq!(Foo(123, true, "Hello, world!".into()), output); +/// # +/// # Ok::<(), magnus::Error>(()) +/// ``` +/// +/// #### Structs with named fields +/// +/// ``` +/// # use magnus::{eval, Value}; +/// # use serde::Deserialize; +/// # use serde_magnus::deserialize; +/// # +/// # // SAFETY: this doctest initializes Ruby once on its current thread. +/// # let ruby = unsafe { magnus::embed::init() }; +/// # +/// #[derive(PartialEq, Debug, Deserialize)] +/// struct Foo { +/// bar: u16, +/// baz: bool, +/// glorp: String +/// } +/// +/// let input: Value = eval!(&ruby, "{ bar: 1234, baz: true, glorp: 'Hello, world!' }")?; +/// let output: Foo = deserialize(&ruby, input)?; +/// assert_eq!( +/// Foo { bar: 1234, baz: true, glorp: "Hello, world!".into() }, +/// output +/// ); +/// # +/// # Ok::<(), magnus::Error>(()) +/// ``` +/// +/// ### Enums +/// +/// ``` +/// # use serde::Deserialize; +/// #[derive(PartialEq, Debug, Deserialize)] +/// enum Foo { +/// Bar, +/// Baz(u16), +/// Glorp(u16, bool, String), +/// Quux { +/// frob: u16, +/// wally: bool, +/// plugh: String +/// } +/// } +/// ``` +/// +/// #### Unit enum variants +/// +/// ``` +/// # use magnus::{eval, Value}; +/// # use serde::Deserialize; +/// # use serde_magnus::deserialize; +/// # +/// # // SAFETY: this doctest initializes Ruby once on its current thread. +/// # let ruby = unsafe { magnus::embed::init() }; +/// # +/// # #[derive(PartialEq, Debug, Deserialize)] +/// # enum Foo { Bar } +/// # +/// let input: Value = eval!(&ruby, "'Bar'")?; +/// let output: Foo = deserialize(&ruby, input)?; +/// assert_eq!(Foo::Bar, output); +/// # +/// # Ok::<(), magnus::Error>(()) +/// ``` +/// +/// #### Newtype enum variants +/// +/// ``` +/// # use magnus::{eval, Value}; +/// # use serde::Deserialize; +/// # use serde_magnus::deserialize; +/// # +/// # // SAFETY: this doctest initializes Ruby once on its current thread. +/// # let ruby = unsafe { magnus::embed::init() }; +/// # +/// # #[derive(PartialEq, Debug, Deserialize)] +/// # enum Foo { Baz(u16) } +/// # +/// let input: Value = eval!(&ruby, "{ 'Baz' => 1234 }")?; +/// let output: Foo = deserialize(&ruby, input)?; +/// assert_eq!(Foo::Baz(1234), output); +/// # +/// # Ok::<(), magnus::Error>(()) +/// ``` +/// +/// #### Tuple enum variants +/// +/// ``` +/// # use magnus::{eval, Value}; +/// # use serde::Deserialize; +/// # use serde_magnus::deserialize; +/// # +/// # // SAFETY: this doctest initializes Ruby once on its current thread. +/// # let ruby = unsafe { magnus::embed::init() }; +/// # +/// # #[derive(PartialEq, Debug, Deserialize)] +/// # enum Foo { Glorp(u16, bool, String) } +/// # +/// let input: Value = eval!(&ruby, "{ 'Glorp' => [123, true, 'Hello, world!'] }")?; +/// let output: Foo = deserialize(&ruby, input)?; +/// assert_eq!(Foo::Glorp(123, true, "Hello, world!".into()), output); +/// # +/// # Ok::<(), magnus::Error>(()) +/// ``` +/// +/// #### Struct enum variants +/// +/// ``` +/// # use magnus::{eval, Value}; +/// # use serde::Deserialize; +/// # use serde_magnus::deserialize; +/// # +/// # // SAFETY: this doctest initializes Ruby once on its current thread. +/// # let ruby = unsafe { magnus::embed::init() }; +/// # +/// # #[derive(PartialEq, Debug, Deserialize)] +/// # enum Foo { +/// # Quux { +/// # frob: u16, +/// # wally: bool, +/// # plugh: String +/// # } +/// # } +/// # +/// let input: Value = eval!(&ruby, r#" +/// { +/// "Quux" => { +/// frob: 1234, +/// wally: true, +/// plugh: "Hello, world!" +/// } +/// } +/// "#)?; +/// let output: Foo = deserialize(&ruby, input)?; +/// assert_eq!( +/// Foo::Quux { frob: 1234, wally: true, plugh: "Hello, world!".into() }, +/// output +/// ); +/// # +/// # Ok::<(), magnus::Error>(()) +/// ``` +/// +/// ### Compound types +/// +/// #### Tuples +/// +/// ``` +/// # use magnus::{eval, Value}; +/// # use serde_magnus::deserialize; +/// # +/// # // SAFETY: this doctest initializes Ruby once on its current thread. +/// # let ruby = unsafe { magnus::embed::init() }; +/// # +/// let input: Value = eval!(&ruby, "[123, true, 'Hello, world!']")?; +/// let output: (i16, bool, String) = deserialize(&ruby, input)?; +/// assert_eq!((123, true, "Hello, world!".into()), output); +/// # +/// # Ok::<(), magnus::Error>(()) +/// ``` +/// +/// #### Arrays +/// +/// ``` +/// # use magnus::{eval, Value}; +/// # use serde_magnus::deserialize; +/// # +/// # // SAFETY: this doctest initializes Ruby once on its current thread. +/// # let ruby = unsafe { magnus::embed::init() }; +/// # +/// let input: Value = eval!(&ruby, "[123, 456, 789]")?; +/// let output: [i64; 3] = deserialize(&ruby, input)?; +/// assert_eq!([123, 456, 789], output); +/// # +/// # Ok::<(), magnus::Error>(()) +/// ``` +/// +/// ### Collections +/// +/// #### Sequences +/// +/// ``` +/// # use magnus::{eval, Value}; +/// # use serde_magnus::deserialize; +/// # +/// # // SAFETY: this doctest initializes Ruby once on its current thread. +/// # let ruby = unsafe { magnus::embed::init() }; +/// # +/// let input: Value = eval!(&ruby, "[123, 456, 789]")?; +/// let output: Vec = deserialize(&ruby, input)?; +/// assert_eq!(vec![123, 456, 789], output); +/// # +/// # Ok::<(), magnus::Error>(()) +/// ``` +/// +/// #### Maps +/// +/// ``` +/// # use magnus::{eval, Value}; +/// # use serde_magnus::deserialize; +/// # +/// # // SAFETY: this doctest initializes Ruby once on its current thread. +/// # let ruby = unsafe { magnus::embed::init() }; +/// # +/// use std::collections::HashMap; +/// +/// let input: Value = eval!(&ruby, r#" +/// { +/// "yes" => "no", +/// "stop" => "go", +/// "high" => "low", +/// "goodbye" => "hello" +/// } +/// "#)?; +/// let output: HashMap = deserialize(&ruby, input)?; +/// assert_eq!(4, output.len()); +/// assert_eq!(Some(&String::from("no")), output.get("yes")); +/// # +/// # Ok::<(), magnus::Error>(()) +/// ``` +/// +/// ### Borrows +/// +/// Although [`serde::Deserialize`] is implemented for `&str`, `&[u8]`, and `&std::path::Path`, it +/// is not possible to deserialize to these types from Ruby values. Any attempt to do so will +/// result in an `Err`. The reason is that it cannot be guaranteed the underlying Ruby data will +/// outlive the borrow. +/// +/// Use owned equivalents such as `String`, `Vec`, and [`std::path::PathBuf`] instead. +/// +/// #### Strings +/// +/// Bad (attempts to deserialize a Ruby `String` into a Rust `&str`): +/// +/// ``` +/// # use magnus::{eval, Error, Value}; +/// # use serde_magnus::deserialize; +/// # +/// # // SAFETY: this doctest initializes Ruby once on its current thread. +/// # let ruby = unsafe { magnus::embed::init() }; +/// # +/// let input: Value = eval!(&ruby, "'Hello, world!'")?; +/// let output: Result<&str, Error> = deserialize(&ruby, input); +/// assert!(output.is_err()); +/// assert_eq!( +/// r#"TypeError: invalid type: expected a borrowed string, got string "Hello, world!""#, +/// output.unwrap_err().to_string() +/// ); +/// # +/// # Ok::<(), magnus::Error>(()) +/// ``` +/// +/// Good (deserializes into an owned Rust `String` instead): +/// +/// ``` +/// # use magnus::{eval, Error, Value}; +/// # use serde_magnus::deserialize; +/// # +/// # // SAFETY: this doctest initializes Ruby once on its current thread. +/// # let ruby = unsafe { magnus::embed::init() }; +/// # +/// let input: Value = eval!(&ruby, "'Hello, world!'")?; +/// let output: String = deserialize(&ruby, input)?; +/// assert_eq!("Hello, world!", output); +/// # +/// # Ok::<(), magnus::Error>(()) +/// ``` +/// +/// #### Bytes +/// +/// Bad (attempts to deserialize a Ruby `String` into a Rust byte slice): +/// +/// ``` +/// # use magnus::{eval, Error, Value}; +/// # use serde_magnus::deserialize; +/// # +/// # // SAFETY: this doctest initializes Ruby once on its current thread. +/// # let ruby = unsafe { magnus::embed::init() }; +/// # +/// let input: Value = eval!(&ruby, "'☃'")?; +/// let output: Result<&[u8], Error> = deserialize(&ruby, input); +/// assert!(output.is_err()); +/// assert_eq!( +/// "TypeError: can't deserialize into byte slice", +/// output.unwrap_err().to_string() +/// ); +/// # +/// # Ok::<(), magnus::Error>(()) +/// ``` +/// +/// Good (uses `serde_bytes::ByteBuf` to deserialize into an owned `Vec` instead): +/// +/// ``` +/// # use magnus::{eval, Error, Value}; +/// # use serde_magnus::deserialize; +/// # +/// # // SAFETY: this doctest initializes Ruby once on its current thread. +/// # let ruby = unsafe { magnus::embed::init() }; +/// # +/// use serde_bytes::ByteBuf; +/// +/// let input: Value = eval!(&ruby, "'☃'")?; +/// let output: ByteBuf = deserialize(&ruby, input)?; +/// assert_eq!(vec![226, 152, 131], output.into_vec()); +/// # +/// # Ok::<(), magnus::Error>(()) +/// ``` +pub fn deserialize<'i, Input, Output>(ruby: &Ruby, input: Input) -> Result +where + Input: IntoValue, + Output: Deserialize<'i>, +{ + Output::deserialize(Deserializer::new(ruby, input.into_value_with(ruby))) + .map_err(|error| error.into_magnus(ruby)) +} + +/// Deserialize a Ruby value using JSON-specific conversion rules. +/// +/// Use a `serde_json` target with `arbitrary_precision` to preserve Ruby Integer +/// values of any size. JSON conversion rejects non-finite floats, object keys +/// other than Strings and Symbols, and nesting beyond 100 containers. +/// +/// # Errors +/// +/// Returns the Ruby exception produced when the input cannot be represented by +/// `Output`. +pub fn deserialize_json<'de, Input, Output>(ruby: &Ruby, input: Input) -> Result +where + Input: IntoValue, + Output: Deserialize<'de>, +{ + Output::deserialize(Deserializer::new_json(ruby, input.into_value_with(ruby))) + .map_err(|error| error.into_magnus(ruby)) +} + +#[allow(unsafe_code)] // Magnus borrows the class name from Ruby. +fn class_name(value: Value) -> String { + // SAFETY: the deserializers hold a Ruby handle and run with the GVL. + // Copy the name before the next Ruby API call or any Ruby reentry. + unsafe { value.classname() }.into_owned() +} diff --git a/src/serde/de/number_deserializer.rs b/crates/serde-magnus/src/de/number_deserializer.rs similarity index 95% rename from src/serde/de/number_deserializer.rs rename to crates/serde-magnus/src/de/number_deserializer.rs index 5df5067..a36dda3 100644 --- a/src/serde/de/number_deserializer.rs +++ b/crates/serde-magnus/src/de/number_deserializer.rs @@ -1,9 +1,9 @@ -use ::serde::de::{ +use serde::de::{ DeserializeSeed, MapAccess, value::{BorrowedStrDeserializer, StringDeserializer}, }; -use super::super::{Error, JSON_NUMBER_TOKEN}; +use crate::{JSON_NUMBER_TOKEN, error::Error}; /// Serde map representation used by `serde_json` for arbitrary-precision numbers. pub(super) struct NumberDeserializer { diff --git a/src/serde/de/variant_deserializer.rs b/crates/serde-magnus/src/de/variant_deserializer.rs similarity index 71% rename from src/serde/de/variant_deserializer.rs rename to crates/serde-magnus/src/de/variant_deserializer.rs index c0e841a..2db3aab 100644 --- a/src/serde/de/variant_deserializer.rs +++ b/crates/serde-magnus/src/de/variant_deserializer.rs @@ -1,8 +1,8 @@ -use ::serde::de::{DeserializeSeed, Unexpected, VariantAccess}; use magnus::{RArray, RHash, Ruby, Value, value::ReprValue}; +use serde::de::{DeserializeSeed, Unexpected, VariantAccess}; -use super::{ArrayDeserializer, Deserializer, HashDeserializer, Mode}; -use crate::serde::Error; +use super::{ArrayDeserializer, Deserializer, HashDeserializer, Mode, class_name}; +use crate::error::Error; /// Serde access to the payload of a Ruby enum representation. pub(super) struct VariantDeserializer<'ruby> { @@ -36,11 +36,8 @@ impl<'de> VariantAccess<'de> for VariantDeserializer<'_> { if self.value.is_nil() { Ok(()) } else { - Err(::serde::de::Error::invalid_type( - Unexpected::Other( - // SAFETY: conversion runs while the Ruby GVL is held. - &unsafe { self.value.classname() }, - ), + Err(serde::de::Error::invalid_type( + Unexpected::Other(&class_name(self.value)), &"unit variant", )) } @@ -61,17 +58,14 @@ impl<'de> VariantAccess<'de> for VariantDeserializer<'_> { visitor: Visitor, ) -> Result where - Visitor: ::serde::de::Visitor<'de>, + Visitor: serde::de::Visitor<'de>, { if let Some(array) = RArray::from_value(self.value) { let depth = self.nested_depth()?; visitor.visit_seq(ArrayDeserializer::new(self.ruby, array, depth, self.mode)) } else { - Err(::serde::de::Error::invalid_type( - Unexpected::Other( - // SAFETY: conversion runs while the Ruby GVL is held. - &unsafe { self.value.classname() }, - ), + Err(serde::de::Error::invalid_type( + Unexpected::Other(&class_name(self.value)), &"tuple variant", )) } @@ -83,17 +77,14 @@ impl<'de> VariantAccess<'de> for VariantDeserializer<'_> { visitor: Visitor, ) -> Result where - Visitor: ::serde::de::Visitor<'de>, + Visitor: serde::de::Visitor<'de>, { if let Some(hash) = RHash::from_value(self.value) { let depth = self.nested_depth()?; visitor.visit_map(HashDeserializer::new(self.ruby, hash, depth, self.mode)?) } else { - Err(::serde::de::Error::invalid_type( - Unexpected::Other( - // SAFETY: conversion runs while the Ruby GVL is held. - &unsafe { self.value.classname() }, - ), + Err(serde::de::Error::invalid_type( + Unexpected::Other(&class_name(self.value)), &"struct variant", )) } diff --git a/crates/serde-magnus/src/error.rs b/crates/serde-magnus/src/error.rs new file mode 100644 index 0000000..94a19e8 --- /dev/null +++ b/crates/serde-magnus/src/error.rs @@ -0,0 +1,84 @@ +use std::fmt; + +/// Error produced by the local Ruby and Serde bridge. +#[derive(Debug)] +pub(crate) enum Error { + Runtime(String), + Type(String), + DuplicateField(&'static str), + Ruby(magnus::Error), +} + +impl Error { + /// Create a bridge error from a human-readable message. + pub(super) fn message(message: impl Into) -> Self { + Self::Runtime(message.into()) + } + + /// Create a type mismatch error. + pub(super) fn type_error(message: impl Into) -> Self { + Self::Type(message.into()) + } + + /// Convert this bridge error into the matching Ruby exception. + pub(super) fn into_magnus(self, ruby: &magnus::Ruby) -> magnus::Error { + match self { + Self::Runtime(message) => magnus::Error::new(ruby.exception_runtime_error(), message), + Self::Type(message) => magnus::Error::new(ruby.exception_type_error(), message), + Self::DuplicateField(field) => magnus::Error::new( + ruby.exception_runtime_error(), + format!("duplicate field `{field}`"), + ), + Self::Ruby(error) => error, + } + } +} + +impl fmt::Display for Error { + fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::Runtime(message) | Self::Type(message) => formatter.write_str(message), + Self::DuplicateField(field) => write!(formatter, "duplicate field `{field}`"), + Self::Ruby(error) => error.fmt(formatter), + } + } +} + +impl std::error::Error for Error {} + +impl serde::ser::Error for Error { + fn custom(message: T) -> Self + where + T: fmt::Display, + { + Self::message(message.to_string()) + } +} + +impl serde::de::Error for Error { + fn custom(message: T) -> Self + where + T: fmt::Display, + { + Self::message(message.to_string()) + } + + fn invalid_type( + unexpected: serde::de::Unexpected<'_>, + expected: &dyn serde::de::Expected, + ) -> Self { + Self::type_error(format!( + "invalid type: expected {expected}, got {unexpected}" + )) + } + + fn duplicate_field(field: &'static str) -> Self { + Self::DuplicateField(field) + } +} + +impl From for Error { + fn from(error: magnus::Error) -> Self { + Self::Ruby(error) + } +} diff --git a/crates/serde-magnus/src/lib.rs b/crates/serde-magnus/src/lib.rs new file mode 100644 index 0000000..bdc58c5 --- /dev/null +++ b/crates/serde-magnus/src/lib.rs @@ -0,0 +1,137 @@ +#![deny(unsafe_code)] +#![doc(test(attr(warn(unused))))] + +//! `serde_magnus` converts between Rust and Ruby data structures using [Serde] and [Magnus]. +//! +//! [Serde]: https://github.com/serde-rs/serde +//! [Magnus]: https://github.com/matsadler/magnus +//! +//! The [`serialize`] function converts from a Rust type implementing the [`serde::Serialize`] +//! trait into a Ruby equivalent. +//! +//! ``` +//! # // SAFETY: this doctest initializes Ruby once on its current thread. +//! # let ruby = unsafe { magnus::embed::init() }; +//! # +//! use serde::{Serialize, Deserialize}; +//! use magnus::{eval, Value}; +//! use serde_magnus::serialize; +//! +//! #[derive(Serialize, Deserialize, PartialEq, Debug)] +//! struct Post { +//! title: String, +//! content: String, +//! author: Author, +//! tags: Vec +//! } +//! +//! #[derive(Serialize, Deserialize, PartialEq, Debug)] +//! struct Author { +//! name: String, +//! email_address: String +//! } +//! +//! let post = Post { +//! title: "Spring carnival planning update".into(), +//! content: "Here's what's new.".into(), +//! author: Author { +//! name: "Martha".into(), +//! email_address: "martha@example.com".into() +//! }, +//! tags: vec![ +//! "carnival".into(), +//! "update".into() +//! ] +//! }; +//! +//! let post: Value = serialize(&ruby, &post)?; +//! +//! assert!(eval!( +//! &ruby, +//! r#" +//! post == { +//! title: "Spring carnival planning update", +//! content: "Here's what's new.", +//! author: { +//! name: "Martha", +//! email_address: "martha@example.com" +//! }, +//! tags: ["carnival", "update"] +//! } +//! "#, +//! post +//! )?); +//! # +//! # Ok::<(), magnus::Error>(()) +//! ``` +//! +//! [`deserialize`] converts from a Ruby value to a Rust type implementing [`serde::Deserialize`]. +//! +//! ``` +//! # use serde::Deserialize; +//! # use magnus::{eval, RHash}; +//! # +//! # // SAFETY: this doctest initializes Ruby once on its current thread. +//! # let ruby = unsafe { magnus::embed::init() }; +//! # +//! # #[derive(Deserialize, PartialEq, Debug)] +//! # struct Post { +//! # title: String, +//! # content: String, +//! # author: Author, +//! # tags: Vec +//! # } +//! # +//! # #[derive(Deserialize, PartialEq, Debug)] +//! # struct Author { +//! # name: String, +//! # email_address: String +//! # } +//! # +//! use serde_magnus::deserialize; +//! +//! let post: RHash = eval!(&ruby, r#" +//! { +//! title: "Spring carnival planning update", +//! content: "Here's what's new.", +//! author: { +//! name: "Martha", +//! email_address: "martha@example.com" +//! }, +//! tags: ["carnival", "update"] +//! } +//! "#)?; +//! +//! let post: Post = deserialize(&ruby, post)?; +//! +//! assert_eq!( +//! Post { +//! title: "Spring carnival planning update".into(), +//! content: "Here's what's new.".into(), +//! author: Author { +//! name: "Martha".into(), +//! email_address: "martha@example.com".into() +//! }, +//! tags: vec![ +//! "carnival".into(), +//! "update".into() +//! ] +//! }, +//! post +//! ); +//! # +//! # Ok::<(), magnus::Error>(()) +//! ``` + +mod de; +mod error; +pub mod options; +mod ser; +#[cfg(test)] +mod tests; + +pub use de::{deserialize, deserialize_json}; +pub use ser::serialize; + +/// Private map key used by `serde_json` for arbitrary-precision numbers. +const JSON_NUMBER_TOKEN: &str = "$serde_json::private::Number"; diff --git a/crates/serde-magnus/src/options.rs b/crates/serde-magnus/src/options.rs new file mode 100644 index 0000000..34306c7 --- /dev/null +++ b/crates/serde-magnus/src/options.rs @@ -0,0 +1,151 @@ +//! Serde-based validation and conversion of Ruby option hashes. + +use std::fmt; + +use indexmap::IndexSet; +use magnus::{Error, IntoValue, Ruby, error::ErrorType}; +use serde::de::DeserializeOwned; +use serde_ignored::Path; +use serde_path_to_error::{Deserializer as PathDeserializer, Path as ErrorPath, Track}; + +use crate::{de::Deserializer, error::Error as SerdeError}; + +/// Validate Ruby option keys without inspecting their values. +/// +/// The derived struct is the only list of accepted keys. Every option field is +/// optional, so this pass can collect unknown keys and duplicates without +/// converting or retaining any Ruby value. +/// +/// # Errors +/// +/// Returns `ArgumentError` for unknown or duplicate keys and `TypeError` for +/// keys that are neither Ruby Symbols nor Strings. +pub fn validate_option_keys(ruby: &Ruby, input: Input) -> Result<(), Error> +where + Input: IntoValue, + Output: DeserializeOwned, +{ + let value: magnus::Value = input.into_value_with(ruby); + let mut unknown = IndexSet::new(); + { + let mut callback = |path: Path<'_>| { + if let Path::Map { key, .. } = path { + unknown.insert(key); + } + }; + serde_ignored::deserialize::<_, _, Output>( + Deserializer::new_option_keys(ruby, value), + &mut callback, + ) + .map_err(|error| option_error(ruby, error, None))?; + } + + if unknown.is_empty() { + return Ok(()); + } + + let label = if unknown.len() == 1 { + "unknown option" + } else { + "unknown options" + }; + let mut message = String::from(label); + message.push(':'); + for (index, name) in unknown.into_iter().enumerate() { + if index > 0 { + message.push(','); + } + message.push(' '); + message.push(':'); + message.push_str(&name); + } + + Err(Error::new(ruby.exception_arg_error(), message)) +} + +/// Deserialize validated Ruby options and retain the failing field path. +/// +/// # Errors +/// +/// Returns the Ruby conversion error with the failing option path attached. +pub fn deserialize_options(ruby: &Ruby, input: Input) -> Result +where + Input: IntoValue, + Output: DeserializeOwned, +{ + let value = input.into_value_with(ruby); + let mut track = Track::new(); + let result = Output::deserialize(PathDeserializer::new( + Deserializer::new(ruby, value), + &mut track, + )); + + result.map_err(|error| { + let path = track.path(); + let path = path.iter().next().is_some().then_some(&path); + option_error(ruby, error, path) + }) +} + +/// Prefix a Magnus error while preserving its original Ruby exception class. +pub fn contextualize_magnus_error(err: Error, context: fmt::Arguments<'_>) -> Error { + match err.error_type() { + ErrorType::Error(class, message) => Error::new(*class, format!("{context}: {message}")), + ErrorType::Exception(exception) => Error::new( + exception.exception_class(), + format!("{context}: {exception}"), + ), + ErrorType::Jump(_) => err, + } +} + +/// Convert a Serde error into the Ruby exception expected for an option. +fn option_error(ruby: &Ruby, error: SerdeError, path: Option<&ErrorPath>) -> Error { + match error { + SerdeError::Runtime(message) => Error::new( + ruby.exception_arg_error(), + contextualize_message(path, message), + ), + SerdeError::Type(message) => Error::new( + ruby.exception_type_error(), + contextualize_message(path, message), + ), + SerdeError::DuplicateField(field) => Error::new( + ruby.exception_arg_error(), + format!("duplicate option: :{field}"), + ), + SerdeError::Ruby(error) if error.is_kind_of(ruby.exception_range_error()) => Error::new( + ruby.exception_arg_error(), + magnus_error_message(&error, path), + ), + SerdeError::Ruby(error) => match path { + Some(path) => { + contextualize_magnus_error(error, format_args!("invalid value for :{path}")) + } + None => error, + }, + } +} + +/// Prefix an error message when Serde identified the failing option path. +fn contextualize_message(path: Option<&ErrorPath>, message: String) -> String { + match path { + Some(path) => format!("invalid value for :{path}: {message}"), + None => message, + } +} + +/// Build the final Ruby error message without an intermediate context String. +fn magnus_error_message(error: &Error, path: Option<&ErrorPath>) -> String { + match (path, error.error_type()) { + (Some(path), ErrorType::Error(_, message)) => { + format!("invalid value for :{path}: {message}") + } + (Some(path), ErrorType::Exception(exception)) => { + format!("invalid value for :{path}: {exception}") + } + (Some(path), ErrorType::Jump(_)) => format!("invalid value for :{path}: {error}"), + (None, ErrorType::Error(_, message)) => message.as_ref().to_owned(), + (None, _) => error.to_string(), + } +} diff --git a/crates/serde-magnus/src/ser/enums.rs b/crates/serde-magnus/src/ser/enums.rs new file mode 100644 index 0000000..df3334e --- /dev/null +++ b/crates/serde-magnus/src/ser/enums.rs @@ -0,0 +1,11 @@ +use crate::error::Error; +use magnus::{IntoValue, Ruby, Value}; + +pub fn nest(ruby: &Ruby, variant: &'static str, data: Data) -> Result +where + Data: IntoValue, +{ + let hash = ruby.hash_new(); + hash.aset(variant, data)?; + Ok(hash.into_value_with(ruby)) +} diff --git a/src/serde/ser/map_serializer.rs b/crates/serde-magnus/src/ser/map_serializer.rs similarity index 95% rename from src/serde/ser/map_serializer.rs rename to crates/serde-magnus/src/ser/map_serializer.rs index 9923bc9..ec7d217 100644 --- a/src/serde/ser/map_serializer.rs +++ b/crates/serde-magnus/src/ser/map_serializer.rs @@ -1,8 +1,8 @@ -use ::serde::{Serialize, ser::SerializeMap}; use magnus::{IntoValue, RHash, Ruby, Value}; +use serde::{Serialize, ser::SerializeMap}; use super::Serializer; -use crate::serde::Error; +use crate::error::Error; /// Serde map serializer backed by a Ruby hash. pub(super) struct MapSerializer<'ruby> { diff --git a/crates/serde-magnus/src/ser/mod.rs b/crates/serde-magnus/src/ser/mod.rs new file mode 100644 index 0000000..feca7a2 --- /dev/null +++ b/crates/serde-magnus/src/ser/mod.rs @@ -0,0 +1,428 @@ +mod serializer; + +mod enums; +mod map_serializer; +mod seq_serializer; +mod struct_serializer; +mod struct_variant_serializer; +mod tuple_variant_serializer; + +use self::serializer::Serializer; + +use self::{ + map_serializer::MapSerializer, seq_serializer::SeqSerializer, + struct_serializer::StructSerializer, struct_variant_serializer::StructVariantSerializer, + tuple_variant_serializer::TupleVariantSerializer, +}; + +use magnus::{Error, Ruby, TryConvert}; +use serde::Serialize; + +/// Serialize Rust data to a Ruby [`Value`][`magnus::Value`]. +/// +/// # Errors +/// +/// Returns a Ruby error when creating or converting the output value fails. +/// +/// Primitive types are converted as follows: +/// +/// | Rust type | Ruby value | +/// |---------------------------|-------------------------------------| +/// | `()` | `nil` | +/// | `bool` | `true` or `false` | +/// | `i8`, `i16`, `i32`, `i64`, `i128` | An `Integer` | +/// | `u8`, `u16`, `u32`, `u64`, `u128` | An `Integer` | +/// | `f32` or `f64` | A `Float` | +/// | `char` | A `String` with UTF-8 encoding | +/// | `&str` | A `String` with UTF-8 encoding | +/// | `String` | A `String` with UTF-8 encoding | +/// | `serde_bytes::Bytes` | A `String` with ASCII-8BIT encoding | +/// +/// ``` +/// # // SAFETY: this doctest initializes Ruby once on its current thread. +/// # let ruby = unsafe { magnus::embed::init() }; +/// # +/// use magnus::{eval, Value}; +/// use serde_magnus::serialize; +/// +/// let output: Value = serialize(&ruby, &1234)?; +/// assert!(eval!(&ruby, "output == 1234", output)?); +/// +/// let output: Value = serialize(&ruby, "Hello, world!")?; +/// assert!(eval!(&ruby, "output == 'Hello, world!'", output)?); +/// # +/// # Ok::<(), magnus::Error>(()) +/// ``` +/// +/// ### `Option` +/// +/// `None` is converted to `nil`. `Some` is unwrapped. +/// +/// ``` +/// # use magnus::{eval, Value}; +/// # use serde_magnus::serialize; +/// # +/// # // SAFETY: this doctest initializes Ruby once on its current thread. +/// # let ruby = unsafe { magnus::embed::init() }; +/// # +/// let input: Option = None; +/// let output: Value = serialize(&ruby, &input)?; +/// assert!(eval!(&ruby, "output == nil", output)?); +/// +/// let input: Option = Some(1234); +/// let output: Value = serialize(&ruby, &input)?; +/// assert!(eval!(&ruby, "output == 1234", output)?); +/// # +/// # Ok::<(), magnus::Error>(()) +/// ``` +/// +/// ### `Result` +/// +/// A `Result` is converted to a `Hash` with one key, `"Ok"` or `"Err"`. +/// +/// ``` +/// # use magnus::{eval, Value}; +/// # use serde_magnus::serialize; +/// # +/// # // SAFETY: this doctest initializes Ruby once on its current thread. +/// # let ruby = unsafe { magnus::embed::init() }; +/// # +/// let input: Result = Ok(1234); +/// let output: Value = serialize(&ruby, &input)?; +/// assert!(eval!(&ruby, "output == { 'Ok' => 1234 }", output)?); +/// +/// let input: Result = Err("something went wrong"); +/// let output: Value = serialize(&ruby, &input)?; +/// assert!(eval!(&ruby, "output == { 'Err' => 'something went wrong' }", output)?); +/// # +/// # Ok::<(), magnus::Error>(()) +/// ``` +/// +/// ### Structs +/// +/// A unit struct is converted to `nil`. +/// +/// ``` +/// # use magnus::{eval, Value}; +/// # use serde_magnus::serialize; +/// # +/// # // SAFETY: this doctest initializes Ruby once on its current thread. +/// # let ruby = unsafe { magnus::embed::init() }; +/// # +/// use serde::Serialize; +/// +/// #[derive(Serialize)] +/// struct Foo; +/// +/// let output: Value = serialize(&ruby, &Foo)?; +/// assert!(eval!(&ruby, "output == nil", output)?); +/// # +/// # Ok::<(), magnus::Error>(()) +/// ``` +/// +/// A newtype struct is unwrapped. +/// +/// ``` +/// # use magnus::{eval, Value}; +/// # use serde::Serialize; +/// # use serde_magnus::serialize; +/// # +/// # // SAFETY: this doctest initializes Ruby once on its current thread. +/// # let ruby = unsafe { magnus::embed::init() }; +/// # +/// #[derive(Serialize)] +/// struct Foo(u16); +/// +/// let input = Foo(1234); +/// let output: Value = serialize(&ruby, &input)?; +/// assert!(eval!(&ruby, "output == 1234", output)?); +/// # +/// # Ok::<(), magnus::Error>(()) +/// ``` +/// +/// A tuple struct is converted to an `Array` of its fields. +/// +/// ``` +/// # use magnus::{eval, Value}; +/// # use serde::Serialize; +/// # use serde_magnus::serialize; +/// # +/// # // SAFETY: this doctest initializes Ruby once on its current thread. +/// # let ruby = unsafe { magnus::embed::init() }; +/// # +/// #[derive(Serialize)] +/// struct Foo<'a>(u16, bool, &'a str); +/// +/// let input = Foo(1234, false, "Hello, world!"); +/// let output: Value = serialize(&ruby, &input)?; +/// assert!(eval!(&ruby, "output == [1234, false, 'Hello, world!']", output)?); +/// # +/// # Ok::<(), magnus::Error>(()) +/// ``` +/// +/// A struct with named fields is converted to a `Hash` with symbol keys. +/// +/// ``` +/// # use magnus::{eval, Value}; +/// # use serde::Serialize; +/// # use serde_magnus::serialize; +/// # +/// # // SAFETY: this doctest initializes Ruby once on its current thread. +/// # let ruby = unsafe { magnus::embed::init() }; +/// # +/// #[derive(Serialize)] +/// struct Foo<'a> { +/// bar: u16, +/// baz: bool, +/// glorp: &'a str +/// } +/// +/// let input = Foo { bar: 1234, baz: false, glorp: "Hello, world!" }; +/// let output: Value = serialize(&ruby, &input)?; +/// assert!(eval!( +/// &ruby, +/// r#" +/// output == { +/// bar: 1234, +/// baz: false, +/// glorp: "Hello, world!" +/// } +/// "#, +/// output +/// )?); +/// # +/// # Ok::<(), magnus::Error>(()) +/// ``` +/// +/// ### Enums +/// +/// ``` +/// # use serde::Serialize; +/// # +/// #[derive(Serialize)] +/// enum Foo<'a> { +/// Bar, +/// Baz(u16), +/// Glorp(u16, bool, &'a str), +/// Quux { +/// frob: u16, +/// wally: bool, +/// plugh: &'a str +/// } +/// } +/// ``` +/// +/// A unit enum variant is converted to the name of the variant as a `String`. +/// +/// ``` +/// # use magnus::{eval, Value}; +/// # use serde::Serialize; +/// # use serde_magnus::serialize; +/// # +/// # // SAFETY: this doctest initializes Ruby once on its current thread. +/// # let ruby = unsafe { magnus::embed::init() }; +/// # +/// # #[derive(Serialize)] +/// # enum Foo<'a> { +/// # Bar, +/// # Baz(u16), +/// # Glorp(u16, bool, &'a str), +/// # Quux { +/// # frob: u16, +/// # wally: bool, +/// # plugh: &'a str +/// # } +/// # } +/// # +/// let output: Value = serialize(&ruby, &Foo::Bar)?; +/// assert!(eval!(&ruby, "output == 'Bar'", output)?); +/// # +/// # Ok::<(), magnus::Error>(()) +/// ``` +/// +/// All other types of enum variants are converted to a `Hash` with one key: the name of the +/// variant as a `String`. +/// +/// For a newtype enum variant, the value keyed by the variant's name is its unwrapped value. +/// +/// ``` +/// # use magnus::{eval, Value}; +/// # use serde::Serialize; +/// # use serde_magnus::serialize; +/// # +/// # // SAFETY: this doctest initializes Ruby once on its current thread. +/// # let ruby = unsafe { magnus::embed::init() }; +/// # +/// # #[derive(Serialize)] +/// # enum Foo<'a> { +/// # Bar, +/// # Baz(u16), +/// # Glorp(u16, bool, &'a str), +/// # Quux { +/// # frob: u16, +/// # wally: bool, +/// # plugh: &'a str +/// # } +/// # } +/// # +/// let input = Foo::Baz(1234); +/// let output: Value = serialize(&ruby, &input)?; +/// assert!(eval!(&ruby, "output == { 'Baz' => 1234 }", output)?); +/// # +/// # Ok::<(), magnus::Error>(()) +/// ``` +/// +/// For a tuple enum variant, the value keyed by the variant's name is an `Array`. +/// +/// ``` +/// # use magnus::{eval, Value}; +/// # use serde::Serialize; +/// # use serde_magnus::serialize; +/// # +/// # // SAFETY: this doctest initializes Ruby once on its current thread. +/// # let ruby = unsafe { magnus::embed::init() }; +/// # +/// # #[derive(Serialize)] +/// # enum Foo<'a> { +/// # Bar, +/// # Baz(u16), +/// # Glorp(u16, bool, &'a str), +/// # Quux { +/// # frob: u16, +/// # wally: bool, +/// # plugh: &'a str +/// # } +/// # } +/// # +/// let input = Foo::Glorp(1234, false, "Hello, world!"); +/// let output: Value = serialize(&ruby, &input)?; +/// assert!(eval!(&ruby, "output == { 'Glorp' => [1234, false, 'Hello, world!'] }", output)?); +/// # +/// # Ok::<(), magnus::Error>(()) +/// ``` +/// +/// For a struct enum variant, the value keyed by the variant name is a `Hash` with `Symbol` keys. +/// +/// ``` +/// # use magnus::{eval, Value}; +/// # use serde::Serialize; +/// # use serde_magnus::serialize; +/// # +/// # // SAFETY: this doctest initializes Ruby once on its current thread. +/// # let ruby = unsafe { magnus::embed::init() }; +/// # +/// # #[derive(Serialize)] +/// # enum Foo<'a> { +/// # Bar, +/// # Baz(u16), +/// # Glorp(u16, bool, &'a str), +/// # Quux { +/// # frob: u16, +/// # wally: bool, +/// # plugh: &'a str +/// # } +/// # } +/// # +/// let input = Foo::Quux { frob: 1234, wally: false, plugh: "Hello, world!" }; +/// let output: Value = serialize(&ruby, &input)?; +/// assert!(eval!( +/// &ruby, +/// r#" +/// output == { +/// "Quux" => { +/// frob: 1234, +/// wally: false, +/// plugh: "Hello, world!" +/// } +/// } +/// "#, +/// output +/// )?); +/// # +/// # Ok::<(), magnus::Error>(()) +/// ``` +/// +/// ### Compound types +/// +/// A compound type such as a tuple `(T1, T2, T3, ...)`, array `[T; N]`, or slice `[T]` is +/// converted to an `Array`. +/// +/// ``` +/// # use magnus::{eval, Value}; +/// # use serde_magnus::serialize; +/// # +/// # // SAFETY: this doctest initializes Ruby once on its current thread. +/// # let ruby = unsafe { magnus::embed::init() }; +/// # +/// let input = (123, false, "Hello, world!"); +/// let output: Value = serialize(&ruby, &input)?; +/// assert!(eval!(&ruby, "output == [123, false, 'Hello, world!']", output)?); +/// +/// let input = [123, 456, 789]; +/// let output: Value = serialize(&ruby, &input)?; +/// assert!(eval!(&ruby, "output == [123, 456, 789]", output)?); +/// # +/// # Ok::<(), magnus::Error>(()) +/// ``` +/// +/// ### Collections +/// +/// A sequence such as a `Vec`, `LinkedList`, or `HashSet` is converted to an `Array`. +/// +/// ``` +/// # use magnus::{eval, Value}; +/// # use serde_magnus::serialize; +/// # +/// # // SAFETY: this doctest initializes Ruby once on its current thread. +/// # let ruby = unsafe { magnus::embed::init() }; +/// # +/// let input = vec![123, 456, 789]; +/// let output: Value = serialize(&ruby, &input)?; +/// assert!(eval!(&ruby, "output == [123, 456, 789]", output)?); +/// # +/// # Ok::<(), magnus::Error>(()) +/// ``` +/// +/// A map such as a `HashMap` or `BTreeMap` is converted to a `Hash`. +/// +/// ``` +/// # use magnus::{eval, Value}; +/// # use serde_magnus::serialize; +/// # +/// # // SAFETY: this doctest initializes Ruby once on its current thread. +/// # let ruby = unsafe { magnus::embed::init() }; +/// # +/// use std::collections::HashMap; +/// +/// let mut input = HashMap::new(); +/// input.insert("yes", "no"); +/// input.insert("stop", "go"); +/// input.insert("high", "low"); +/// input.insert("goodbye", "hello"); +/// +/// let output: Value = serialize(&ruby, &input)?; +/// assert!(eval!( +/// &ruby, +/// r#" +/// output == { +/// "yes" => "no", +/// "stop" => "go", +/// "high" => "low", +/// "goodbye" => "hello" +/// } +/// "#, +/// output +/// )?); +/// # +/// # Ok::<(), magnus::Error>(()) +/// ``` +pub fn serialize(ruby: &Ruby, input: &Input) -> Result +where + Input: Serialize + ?Sized, + Output: TryConvert, +{ + let value = input + .serialize(Serializer::new(ruby)) + .map_err(|error| error.into_magnus(ruby))?; + Output::try_convert(value) +} diff --git a/crates/serde-magnus/src/ser/seq_serializer.rs b/crates/serde-magnus/src/ser/seq_serializer.rs new file mode 100644 index 0000000..b3bcbb5 --- /dev/null +++ b/crates/serde-magnus/src/ser/seq_serializer.rs @@ -0,0 +1,68 @@ +use super::Serializer; +use crate::error::Error; +use magnus::{IntoValue, RArray, Ruby, Value}; +use serde::{ + Serialize, + ser::{SerializeSeq, SerializeTuple, SerializeTupleStruct}, +}; + +pub(super) struct SeqSerializer<'r> { + ruby: &'r Ruby, + array: RArray, +} + +impl<'r> SeqSerializer<'r> { + pub fn new(ruby: &'r Ruby, array: RArray) -> SeqSerializer<'r> { + SeqSerializer { ruby, array } + } +} + +impl<'r> SerializeSeq for SeqSerializer<'r> { + type Ok = Value; + type Error = Error; + + fn serialize_element(&mut self, element: &Element) -> Result<(), Self::Error> + where + Element: Serialize + ?Sized, + { + self.array + .push(element.serialize(Serializer::new(self.ruby))?) + .map_err(Into::into) + } + + fn end(self) -> Result { + Ok(self.array.into_value_with(self.ruby)) + } +} + +impl<'r> SerializeTuple for SeqSerializer<'r> { + type Ok = Value; + type Error = Error; + + fn serialize_element(&mut self, element: &Element) -> Result<(), Self::Error> + where + Element: Serialize + ?Sized, + { + ::serialize_element(self, element) + } + + fn end(self) -> Result { + ::end(self) + } +} + +impl<'r> SerializeTupleStruct for SeqSerializer<'r> { + type Ok = Value; + type Error = Error; + + fn serialize_field(&mut self, field: &Field) -> Result<(), Self::Error> + where + Field: Serialize + ?Sized, + { + ::serialize_element(self, field) + } + + fn end(self) -> Result { + ::end(self) + } +} diff --git a/crates/serde-magnus/src/ser/serializer.rs b/crates/serde-magnus/src/ser/serializer.rs new file mode 100644 index 0000000..d570618 --- /dev/null +++ b/crates/serde-magnus/src/ser/serializer.rs @@ -0,0 +1,206 @@ +use magnus::{IntoValue, Ruby, Value}; +use serde::Serialize; + +use super::{ + MapSerializer, SeqSerializer, StructSerializer, StructVariantSerializer, + TupleVariantSerializer, enums::nest, +}; +use crate::error::Error; + +pub(super) struct Serializer<'r> { + ruby: &'r Ruby, +} + +impl<'r> Serializer<'r> { + pub fn new(ruby: &'r Ruby) -> Self { + Self { ruby } + } +} + +impl<'r> serde::Serializer for Serializer<'r> { + type Ok = Value; + type Error = Error; + + type SerializeSeq = SeqSerializer<'r>; + type SerializeTuple = SeqSerializer<'r>; + type SerializeTupleStruct = SeqSerializer<'r>; + type SerializeTupleVariant = TupleVariantSerializer<'r>; + type SerializeMap = MapSerializer<'r>; + type SerializeStruct = StructSerializer<'r>; + type SerializeStructVariant = StructVariantSerializer<'r>; + + fn serialize_bool(self, value: bool) -> Result { + Ok(value.into_value_with(self.ruby)) + } + + fn serialize_i8(self, value: i8) -> Result { + Ok(value.into_value_with(self.ruby)) + } + + fn serialize_i16(self, value: i16) -> Result { + Ok(value.into_value_with(self.ruby)) + } + + fn serialize_i32(self, value: i32) -> Result { + Ok(value.into_value_with(self.ruby)) + } + + fn serialize_i64(self, value: i64) -> Result { + Ok(value.into_value_with(self.ruby)) + } + + fn serialize_i128(self, value: i128) -> Result { + Ok(value.into_value_with(self.ruby)) + } + + fn serialize_u8(self, value: u8) -> Result { + Ok(value.into_value_with(self.ruby)) + } + + fn serialize_u16(self, value: u16) -> Result { + Ok(value.into_value_with(self.ruby)) + } + + fn serialize_u32(self, value: u32) -> Result { + Ok(value.into_value_with(self.ruby)) + } + + fn serialize_u64(self, value: u64) -> Result { + Ok(value.into_value_with(self.ruby)) + } + + fn serialize_u128(self, value: u128) -> Result { + Ok(value.into_value_with(self.ruby)) + } + + fn serialize_f32(self, value: f32) -> Result { + Ok(value.into_value_with(self.ruby)) + } + + fn serialize_f64(self, value: f64) -> Result { + Ok(value.into_value_with(self.ruby)) + } + + fn serialize_char(self, value: char) -> Result { + Ok(value.into_value_with(self.ruby)) + } + + fn serialize_str(self, value: &str) -> Result { + Ok(value.into_value_with(self.ruby)) + } + + fn serialize_bytes(self, value: &[u8]) -> Result { + Ok(self.ruby.str_from_slice(value).into_value_with(self.ruby)) + } + + fn serialize_none(self) -> Result { + Ok(().into_value_with(self.ruby)) + } + + fn serialize_some(self, value: &Value) -> Result + where + Value: Serialize + ?Sized, + { + value.serialize(self) + } + + fn serialize_unit(self) -> Result { + Ok(().into_value_with(self.ruby)) + } + + fn serialize_unit_struct(self, _name: &'static str) -> Result { + Ok(().into_value_with(self.ruby)) + } + + fn serialize_unit_variant( + self, + _name: &'static str, + _index: u32, + variant: &'static str, + ) -> Result { + Ok(variant.into_value_with(self.ruby)) + } + + fn serialize_newtype_struct( + self, + _name: &'static str, + value: &Value, + ) -> Result + where + Value: Serialize + ?Sized, + { + value.serialize(self) + } + + fn serialize_newtype_variant( + self, + _name: &'static str, + _index: u32, + variant: &'static str, + value: &Value, + ) -> Result + where + Value: Serialize + ?Sized, + { + nest(self.ruby, variant, value.serialize(self)?) + } + + fn serialize_seq(self, len: Option) -> Result { + Ok(SeqSerializer::new( + self.ruby, + self.ruby.ary_new_capa(len.unwrap_or(0)), + )) + } + + fn serialize_tuple(self, len: usize) -> Result { + self.serialize_seq(Some(len)) + } + + fn serialize_tuple_struct( + self, + _name: &'static str, + len: usize, + ) -> Result { + self.serialize_seq(Some(len)) + } + + fn serialize_tuple_variant( + self, + _name: &'static str, + _index: u32, + variant: &'static str, + len: usize, + ) -> Result { + Ok(TupleVariantSerializer::new( + self.ruby, + variant, + self.ruby.ary_new_capa(len), + )) + } + + fn serialize_map(self, _len: Option) -> Result { + Ok(MapSerializer::new(self.ruby, self.ruby.hash_new())) + } + + fn serialize_struct( + self, + name: &'static str, + _len: usize, + ) -> Result { + Ok(StructSerializer::new(self.ruby, name)) + } + + fn serialize_struct_variant( + self, + _name: &'static str, + _index: u32, + variant: &'static str, + _len: usize, + ) -> Result { + Ok(StructVariantSerializer::new( + self.ruby, + variant, + self.ruby.hash_new(), + )) + } +} diff --git a/src/serde/ser/struct_serializer.rs b/crates/serde-magnus/src/ser/struct_serializer.rs similarity index 93% rename from src/serde/ser/struct_serializer.rs rename to crates/serde-magnus/src/ser/struct_serializer.rs index 94d0ec4..3151045 100644 --- a/src/serde/ser/struct_serializer.rs +++ b/crates/serde-magnus/src/ser/struct_serializer.rs @@ -1,8 +1,8 @@ -use ::serde::{Serialize, ser::SerializeStruct}; use magnus::{Integer, IntoValue, RHash, RString, Ruby, Value, value::ReprValue}; +use serde::{Serialize, ser::SerializeStruct}; use super::Serializer; -use crate::serde::{Error, JSON_NUMBER_TOKEN}; +use crate::{JSON_NUMBER_TOKEN, error::Error}; /// Serde struct serializer, including `serde_json` arbitrary-precision numbers. pub(super) enum StructSerializer<'ruby> { @@ -18,13 +18,13 @@ pub(super) enum StructSerializer<'ruby> { impl<'ruby> StructSerializer<'ruby> { /// Create the serializer selected by the Serde struct name. - pub(super) fn new(ruby: &'ruby Ruby, name: &'static str, len: usize) -> Self { + pub(super) fn new(ruby: &'ruby Ruby, name: &'static str) -> Self { if name == JSON_NUMBER_TOKEN { Self::Number { ruby, value: None } } else { Self::Map { ruby, - hash: ruby.hash_new_capa(len), + hash: ruby.hash_new(), } } } diff --git a/crates/serde-magnus/src/ser/struct_variant_serializer.rs b/crates/serde-magnus/src/ser/struct_variant_serializer.rs new file mode 100644 index 0000000..20a0183 --- /dev/null +++ b/crates/serde-magnus/src/ser/struct_variant_serializer.rs @@ -0,0 +1,45 @@ +use super::{Serializer, enums::nest}; +use crate::error::Error; +use magnus::{RHash, Ruby, Value}; +use serde::{Serialize, ser::SerializeStructVariant}; + +pub(super) struct StructVariantSerializer<'r> { + ruby: &'r Ruby, + variant: &'static str, + hash: RHash, +} + +impl<'r> StructVariantSerializer<'r> { + pub fn new(ruby: &'r Ruby, variant: &'static str, hash: RHash) -> StructVariantSerializer<'r> { + StructVariantSerializer { + ruby, + variant, + hash, + } + } +} + +impl<'r> SerializeStructVariant for StructVariantSerializer<'r> { + type Ok = Value; + type Error = Error; + + fn serialize_field( + &mut self, + name: &'static str, + value: &Value, + ) -> Result<(), Self::Error> + where + Value: Serialize + ?Sized, + { + self.hash + .aset( + self.ruby.to_symbol(name), + value.serialize(Serializer::new(self.ruby))?, + ) + .map_err(Into::into) + } + + fn end(self) -> Result { + nest(self.ruby, self.variant, self.hash) + } +} diff --git a/crates/serde-magnus/src/ser/tuple_variant_serializer.rs b/crates/serde-magnus/src/ser/tuple_variant_serializer.rs new file mode 100644 index 0000000..dafe4c7 --- /dev/null +++ b/crates/serde-magnus/src/ser/tuple_variant_serializer.rs @@ -0,0 +1,38 @@ +use super::{Serializer, enums::nest}; +use crate::error::Error; +use magnus::{RArray, Ruby, Value}; +use serde::{Serialize, ser::SerializeTupleVariant}; + +pub(super) struct TupleVariantSerializer<'r> { + ruby: &'r Ruby, + variant: &'static str, + array: RArray, +} + +impl<'r> TupleVariantSerializer<'r> { + pub fn new(ruby: &'r Ruby, variant: &'static str, array: RArray) -> TupleVariantSerializer<'r> { + TupleVariantSerializer { + ruby, + variant, + array, + } + } +} + +impl<'r> SerializeTupleVariant for TupleVariantSerializer<'r> { + type Ok = Value; + type Error = Error; + + fn serialize_field(&mut self, field: &Field) -> Result<(), Self::Error> + where + Field: Serialize + ?Sized, + { + self.array + .push(field.serialize(Serializer::new(self.ruby))?) + .map_err(Into::into) + } + + fn end(self) -> Result { + nest(self.ruby, self.variant, self.array) + } +} diff --git a/src/serde/tests.rs b/crates/serde-magnus/src/tests.rs similarity index 70% rename from src/serde/tests.rs rename to crates/serde-magnus/src/tests.rs index 485ac2b..ce628c3 100644 --- a/src/serde/tests.rs +++ b/crates/serde-magnus/src/tests.rs @@ -1,9 +1,10 @@ use std::{collections::BTreeMap, fmt}; -use ::serde::{Deserialize, Serialize, de::Visitor}; use magnus::{RArray, RHash, RString, Ruby, Value, encoding::EncodingCapable, value::ReprValue}; +use serde::{Deserialize, Serialize}; +use serde_bytes::ByteBuf; -use super::{deserialize_json, deserialize_ruby, serialize}; +use super::{deserialize, deserialize_json, serialize}; #[derive(Debug, Deserialize, PartialEq, Serialize)] struct Record { @@ -30,62 +31,13 @@ enum State { Failed { message: String }, } -/// Byte sequence that exercises Serde's owned byte-buffer entry points. -#[derive(Debug, PartialEq)] -struct ByteBuffer(Vec); - -impl Serialize for ByteBuffer { - fn serialize( - &self, - serializer: Serializer, - ) -> Result - where - Serializer: ::serde::Serializer, - { - serializer.serialize_bytes(&self.0) - } -} - -impl<'de> Deserialize<'de> for ByteBuffer { - fn deserialize(deserializer: Deserializer) -> Result - where - Deserializer: ::serde::Deserializer<'de>, - { - struct ByteBufferVisitor; - - impl<'de> Visitor<'de> for ByteBufferVisitor { - type Value = ByteBuffer; - - fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { - formatter.write_str("an owned byte buffer") - } - - fn visit_bytes(self, value: &[u8]) -> Result - where - Error: ::serde::de::Error, - { - Ok(ByteBuffer(value.to_vec())) - } - - fn visit_byte_buf(self, value: Vec) -> Result - where - Error: ::serde::de::Error, - { - Ok(ByteBuffer(value)) - } - } - - deserializer.deserialize_byte_buf(ByteBufferVisitor) - } -} - /// Assert that a supported Serde value survives conversion through Ruby. fn assert_ruby_round_trip(ruby: &Ruby, input: Input) -> Result<(), magnus::Error> where Input: fmt::Debug + PartialEq + Serialize + for<'de> Deserialize<'de>, { let value: Value = serialize(ruby, &input)?; - let output: Input = deserialize_ruby(ruby, value)?; + let output: Input = deserialize(ruby, value)?; assert_eq!(input, output); Ok(()) } @@ -110,22 +62,20 @@ fn assert_scalar_conversions(ruby: &Ruby) -> Result<(), magnus::Error> { let character: RString = serialize(ruby, &'\u{2603}')?; assert_eq!("\u{2603}", character.to_string()?); assert!(character.enc_get() == ruby.utf8_encindex()); - let output: char = deserialize_ruby(ruby, character)?; + let output: char = deserialize(ruby, character)?; assert_eq!('\u{2603}', output); let string: RString = serialize(ruby, &"Hello, world!")?; assert_eq!("Hello, world!", string.to_string()?); assert!(string.enc_get() == ruby.utf8_encindex()); - assert_eq!( - "Hello, world!", - deserialize_ruby::<_, String>(ruby, string)? - ); + assert_eq!("Hello, world!", deserialize::<_, String>(ruby, string)?); - let bytes = ByteBuffer(b"\0binary\xff".to_vec()); + let bytes = ByteBuf::from(b"\0binary\xff".as_slice()); let value: RString = serialize(ruby, &bytes)?; - assert_eq!(bytes.0.as_slice(), value.to_bytes().as_ref()); + assert_eq!(bytes.as_ref(), value.to_bytes().as_ref()); assert!(value.enc_get() == ruby.ascii8bit_encindex()); - assert_eq!(bytes, deserialize_ruby(ruby, value)?); + let output: ByteBuf = deserialize(ruby, value)?; + assert_eq!(bytes, output); let none: Value = serialize(ruby, &Option::::None)?; assert!(none.is_nil()); @@ -157,7 +107,7 @@ fn assert_composite_conversions(ruby: &Ruby) -> Result<(), magnus::Error> { let value: RHash = serialize(ruby, &record)?; let count: u64 = value.aref(ruby.to_symbol("count"))?; assert_eq!(record.count, count); - assert_eq!(record, deserialize_ruby(ruby, value)?); + assert_eq!(record, deserialize(ruby, value)?); let map = BTreeMap::from([("first".to_owned(), 1_u64), ("second".to_owned(), 2)]); assert_ruby_round_trip(ruby, map)?; @@ -190,43 +140,44 @@ fn assert_integer_conversions(ruby: &Ruby) -> Result<(), magnus::Error> { let value: Value = serialize(ruby, &i128::MIN)?; let decimal: String = value.funcall("to_s", ())?; assert_eq!(i128::MIN.to_string(), decimal); - assert_eq!(i128::MIN, deserialize_ruby::<_, i128>(ruby, value)?); + assert_eq!(i128::MIN, deserialize::<_, i128>(ruby, value)?); assert_eq!(i128::MIN, deserialize_json::<_, i128>(ruby, value)?); let value: Value = serialize(ruby, &u128::MAX)?; let decimal: String = value.funcall("to_s", ())?; assert_eq!(u128::MAX.to_string(), decimal); - assert_eq!(u128::MAX, deserialize_ruby::<_, u128>(ruby, value)?); + assert_eq!(u128::MAX, deserialize::<_, u128>(ruby, value)?); assert_eq!(u128::MAX, deserialize_json::<_, u128>(ruby, value)?); let value: Value = serialize(ruby, &u64::MAX)?; - assert_eq!(u64::MAX, deserialize_ruby::<_, u64>(ruby, value)?); + assert_eq!(u64::MAX, deserialize::<_, u64>(ruby, value)?); assert_eq!(u64::MAX, deserialize_json::<_, u64>(ruby, value)?); Ok(()) } /// Verify unsupported borrowed values and malformed enum shapes return errors. fn assert_conversion_errors(ruby: &Ruby) -> Result<(), magnus::Error> { - let error = deserialize_ruby::<_, &str>(ruby, ruby.str_new("borrowed")) + let error = deserialize::<_, &str>(ruby, ruby.str_new("borrowed")) .expect_err("borrowed strings must not outlive their Ruby value"); assert_type_error(error, "expected a borrowed string"); - let error = deserialize_ruby::<_, &[u8]>(ruby, ruby.str_new("borrowed")) + let error = deserialize::<_, &[u8]>(ruby, ruby.str_new("borrowed")) .expect_err("borrowed byte slices must not outlive their Ruby value"); assert_type_error(error, "can't deserialize into byte slice"); let variants = ruby.hash_new(); variants.aset("Ready", ruby.qnil())?; variants.aset("Count", 1)?; - let error = deserialize_ruby::<_, State>(ruby, variants) + let error = deserialize::<_, State>(ruby, variants) .expect_err("an enum hash must contain exactly one variant"); assert_type_error(error, "Hash of length 2"); Ok(()) } #[test] +#[allow(unsafe_code)] fn retains_ruby_serde_conversion_surface() -> Result<(), magnus::Error> { - // SAFETY: this is the only test that initializes the embedded Ruby VM. + // SAFETY: this process initializes the embedded Ruby VM once on this thread. let ruby = unsafe { magnus::embed::init() }; assert_scalar_conversions(&ruby)?; diff --git a/src/client/body/json.rs b/src/client/body/json.rs index 644862f..a8dd993 100644 --- a/src/client/body/json.rs +++ b/src/client/body/json.rs @@ -9,8 +9,9 @@ use ::serde::{Deserialize, Serialize}; use magnus::{Error, Ruby, TryConvert, Value}; +use serde_magnus::deserialize_json; -use crate::{error::json_serialization_error, serde::deserialize_json}; +use crate::error::json_serialization_error; /// An owned JSON tree shared by request and response conversion. /// diff --git a/src/client/response.rs b/src/client/response.rs index 7144e35..3eda5ad 100644 --- a/src/client/response.rs +++ b/src/client/response.rs @@ -264,7 +264,7 @@ impl Response { let response = rb_self.response(ruby, false)?; let json = rt::block_on(ruby, response.json::())?.map_err(|err| wreq_error(ruby, err))?; - crate::serde::serialize(ruby, &json) + serde_magnus::serialize(ruby, &json) } /// Yield response body chunks to the given Ruby block. diff --git a/src/error.rs b/src/error.rs index 57c1935..55f7b92 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,12 +1,9 @@ -use std::{ - cell::{BorrowError, BorrowMutError}, - fmt, -}; +use std::cell::{BorrowError, BorrowMutError}; use magnus::{ - Error as MagnusError, RModule, Ruby, error::ErrorType, exception::ExceptionClass, prelude::*, - value::Lazy, + Error as MagnusError, RModule, Ruby, exception::ExceptionClass, prelude::*, value::Lazy, }; +use serde_magnus::options::contextualize_magnus_error; use tokio::sync::mpsc::error::SendError; 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 { ) } -/// Prefix a Magnus error while preserving its original Ruby exception class. -pub(crate) fn contextualize_magnus_error( - err: MagnusError, - context: fmt::Arguments<'_>, -) -> MagnusError { - match err.error_type() { - ErrorType::Error(class, message) => { - MagnusError::new(*class, format!("{context}: {message}")) - } - ErrorType::Exception(exception) => MagnusError::new( - exception.exception_class(), - format!("{context}: {exception}"), - ), - ErrorType::Jump(_) => err, - } -} - /// Add an option name while preserving the original Ruby exception class. pub fn option_value_error(option: &str, err: MagnusError) -> MagnusError { contextualize_magnus_error(err, format_args!("invalid value for :{option}")) diff --git a/src/lib.rs b/src/lib.rs index dc56f96..cbd7b3a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,7 +14,6 @@ mod header; mod http; mod options; mod rt; -mod serde; mod tls; use magnus::{Error, Module, Ruby, Value}; diff --git a/src/options.rs b/src/options.rs index 54095ec..a910474 100644 --- a/src/options.rs +++ b/src/options.rs @@ -1,20 +1,15 @@ //! Parsing and validation for Ruby option hashes. -use std::{fmt, marker::PhantomData}; +use std::fmt; use ::serde::{ Deserialize, Deserializer, de::{DeserializeOwned, Visitor}, }; use magnus::{Error, RHash, Ruby, TryConvert, Value, value::ReprValue}; +use serde_magnus::options::{deserialize_options, validate_option_keys}; -use crate::{ - error::{argument_error, option_value_error, type_error}, - serde, -}; - -/// Private Serde newtype used to recognize values converted by Magnus. -pub(crate) const NATIVE_OPTION_TOKEN: &str = "$wreq::private::NativeOption"; +use crate::error::{argument_error, option_value_error, type_error}; /// An option represented in the Serde schema but converted through Magnus. /// @@ -46,22 +41,29 @@ impl<'de, T> Deserialize<'de> for NativeOption { where D: Deserializer<'de>, { - struct NativeOptionVisitor(PhantomData T>); + struct NativeOptionVisitor; - impl Visitor<'_> for NativeOptionVisitor { - type Value = NativeOption; + impl<'de> Visitor<'de> for NativeOptionVisitor { + type Value = (); fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { formatter.write_str("a Ruby-native option") } - fn visit_unit(self) -> Result { - Ok(NativeOption::default()) + fn visit_none(self) -> Result { + Ok(()) + } + + fn visit_some(self, _deserializer: D) -> Result + where + D: Deserializer<'de>, + { + Ok(()) } } - deserializer - .deserialize_newtype_struct(NATIVE_OPTION_TOKEN, NativeOptionVisitor(PhantomData)) + deserializer.deserialize_option(NativeOptionVisitor)?; + Ok(Self::default()) } } @@ -114,7 +116,7 @@ impl<'ruby> Options<'ruby> { where T: DeserializeOwned, { - serde::validate_option_keys::<_, T>(self.ruby, self.hash)?; + validate_option_keys::<_, T>(self.ruby, self.hash)?; Ok(self) } @@ -128,7 +130,7 @@ impl<'ruby> Options<'ruby> { where T: DeserializeOwned, { - serde::deserialize_options(self.ruby, self.hash) + deserialize_options(self.ruby, self.hash) } /// Return whether an option key is present, including a `nil` value. diff --git a/src/serde.rs b/src/serde.rs deleted file mode 100644 index 167313f..0000000 --- a/src/serde.rs +++ /dev/null @@ -1,202 +0,0 @@ -/* -Copyright 2022 George Claghorn - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies -of the Software, and to permit persons to whom the Software is furnished to do -so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -*/ - -//! Serde integration for Magnus with JSON-specific conversion behavior. -//! -//! This module is adapted from `serde_magnus` 0.11.0 and retains its generic -//! Ruby serialization and deserialization surface. Local changes add a JSON -//! mode with arbitrary-size integer support, finite-float and object-key -//! validation, insertion-order preservation, and bounded container nesting. -//! The bridge also avoids the upstream `Ruby::get().unwrap()` error path, -//! checks iterator and map state explicitly, reports unknown option fields and -//! conversion paths, and supports `i128` and `u128` in both directions for -//! typed Rust values. -#![allow(unsafe_code)] - -mod de; -mod error; -mod ser; -#[cfg(test)] -mod tests; - -use ::serde::{Deserialize, Serialize, de::DeserializeOwned}; -use indexmap::IndexSet; -use magnus::{IntoValue, Ruby, TryConvert}; -use serde_ignored::Path; -use serde_path_to_error::{Deserializer as PathDeserializer, Track}; - -use crate::error::argument_error; - -pub(super) use error::Error; - -/// Private map key used to carry arbitrary-precision numbers through Serde. -/// -/// This mirrors the representation used by `serde_json` when its -/// `arbitrary_precision` feature is enabled. The precision tests protect this -/// integration point when `serde_json` is updated. -pub(super) const JSON_NUMBER_TOKEN: &str = "$serde_json::private::Number"; - -/// Maximum nesting accepted while converting a Ruby request value. -pub(super) const MAX_JSON_NESTING: usize = 100; - -/// Deserialize a Ruby value using native Ruby data model semantics. -/// -/// This preserves the public conversion behavior provided by the upstream -/// `serde_magnus::deserialize` function. JSON callers should use -/// [`deserialize_json`] when arbitrary-size integer precision is required. -/// -/// # Errors -/// -/// Returns the Ruby exception produced when the input cannot be represented by -/// `Output`. -#[allow(dead_code)] // Retained as part of the upstream-compatible Serde surface. -pub(crate) fn deserialize_ruby<'de, Input, Output>( - ruby: &Ruby, - input: Input, -) -> Result -where - Input: IntoValue, - Output: Deserialize<'de>, -{ - de::deserialize_ruby(ruby, input.into_value_with(ruby)).map_err(|error| error.into_magnus(ruby)) -} - -/// Serialize any Serde value into a Ruby value. -/// -/// This preserves the public conversion behavior provided by the upstream -/// `serde_magnus::serialize` function. -/// -/// # Errors -/// -/// Returns the Ruby exception produced while creating or converting the output -/// value. -pub(crate) fn serialize(ruby: &Ruby, input: &Input) -> Result -where - Input: Serialize + ?Sized, - Output: TryConvert, -{ - let value = ser::serialize(ruby, input).map_err(|error| error.into_magnus(ruby))?; - Output::try_convert(value) -} - -/// Deserialize a Ruby value using JSON-specific conversion rules. -/// -/// Unlike [`deserialize_ruby`], this preserves arbitrary-size integers and -/// rejects non-finite floats, unsupported object keys, and excessive nesting. -/// -/// # Errors -/// -/// Returns the Ruby exception produced when the input cannot be represented by -/// `Output`. -pub(crate) fn deserialize_json<'de, Input, Output>( - ruby: &Ruby, - input: Input, -) -> Result -where - Input: IntoValue, - Output: Deserialize<'de>, -{ - de::deserialize_json(ruby, input.into_value_with(ruby)).map_err(|error| error.into_magnus(ruby)) -} - -/// Validate Ruby option keys without inspecting their values. -/// -/// The derived struct is the only list of accepted keys. Every option field is -/// optional, so this pass can collect unknown keys and duplicates without -/// converting or retaining any Ruby value. -/// -/// # Errors -/// -/// Returns `ArgumentError` for unknown or duplicate keys and `TypeError` for -/// keys that are neither Ruby Symbols nor Strings. -pub(crate) fn validate_option_keys( - ruby: &Ruby, - input: Input, -) -> Result<(), magnus::Error> -where - Input: IntoValue, - Output: DeserializeOwned, -{ - let value: magnus::Value = input.into_value_with(ruby); - let mut unknown = IndexSet::new(); - { - let mut callback = |path: Path<'_>| { - if let Path::Map { key, .. } = path { - unknown.insert(key); - } - }; - serde_ignored::deserialize::<_, _, Output>( - de::Deserializer::new_option_keys(ruby, value), - &mut callback, - ) - .map_err(|error| error.into_option_magnus(ruby, None))?; - } - - if unknown.is_empty() { - return Ok(()); - } - - let label = if unknown.len() == 1 { - "unknown option" - } else { - "unknown options" - }; - let mut message = String::from(label); - message.push(':'); - for (index, name) in unknown.into_iter().enumerate() { - if index > 0 { - message.push(','); - } - message.push(' '); - message.push(':'); - message.push_str(&name); - } - - Err(argument_error(ruby, message)) -} - -/// Deserialize validated Ruby options and retain the failing field path. -/// -/// # Errors -/// -/// Returns the Ruby conversion error with the failing option path attached. -pub(crate) fn deserialize_options( - ruby: &Ruby, - input: Input, -) -> Result -where - Input: IntoValue, - Output: DeserializeOwned, -{ - let value = input.into_value_with(ruby); - let mut track = Track::new(); - let result = Output::deserialize(PathDeserializer::new( - de::Deserializer::new_ruby(ruby, value), - &mut track, - )); - - result.map_err(|error| { - let path = track.path(); - let path = path.iter().next().is_some().then_some(&path); - error.into_option_magnus(ruby, path) - }) -} diff --git a/src/serde/de.rs b/src/serde/de.rs deleted file mode 100644 index 48a9e41..0000000 --- a/src/serde/de.rs +++ /dev/null @@ -1,33 +0,0 @@ -mod array_deserializer; -mod array_enumerator; -mod deserializer; -mod enum_deserializer; -mod hash_deserializer; -mod number_deserializer; -mod variant_deserializer; - -use ::serde::Deserialize; -use magnus::{Ruby, Value}; - -use super::Error; -use array_deserializer::ArrayDeserializer; -pub(super) use deserializer::Deserializer; -use deserializer::Mode; -use hash_deserializer::HashDeserializer; -use variant_deserializer::VariantDeserializer; - -/// Deserialize one Ruby value using native Ruby data model semantics. -pub(super) fn deserialize_ruby<'de, Output>(ruby: &Ruby, value: Value) -> Result -where - Output: Deserialize<'de>, -{ - Output::deserialize(Deserializer::new_ruby(ruby, value)) -} - -/// Deserialize one Ruby value using JSON-specific conversion rules. -pub(super) fn deserialize_json<'de, Output>(ruby: &Ruby, value: Value) -> Result -where - Output: Deserialize<'de>, -{ - Output::deserialize(Deserializer::new_json(ruby, value)) -} diff --git a/src/serde/error.rs b/src/serde/error.rs deleted file mode 100644 index 80cd103..0000000 --- a/src/serde/error.rs +++ /dev/null @@ -1,146 +0,0 @@ -use std::fmt; - -use magnus::error::ErrorType; -use serde_path_to_error::Path; - -use crate::error::contextualize_magnus_error; - -/// Error produced by the local Ruby and Serde bridge. -#[derive(Debug)] -pub(crate) enum Error { - Runtime(String), - Type(String), - DuplicateField(&'static str), - Ruby(magnus::Error), -} - -impl Error { - /// Create a bridge error from a human-readable message. - pub(super) fn message(message: impl Into) -> Self { - Self::Runtime(message.into()) - } - - /// Create a type mismatch error. - pub(super) fn type_error(message: impl Into) -> Self { - Self::Type(message.into()) - } - - /// Convert this bridge error into the matching Ruby exception. - pub(super) fn into_magnus(self, ruby: &magnus::Ruby) -> magnus::Error { - match self { - Self::Runtime(message) => magnus::Error::new(ruby.exception_runtime_error(), message), - Self::Type(message) => magnus::Error::new(ruby.exception_type_error(), message), - Self::DuplicateField(field) => magnus::Error::new( - ruby.exception_runtime_error(), - format!("duplicate field `{field}`"), - ), - Self::Ruby(error) => error, - } - } - - /// Convert an option value error into Ruby's normal argument categories. - pub(super) fn into_option_magnus( - self, - ruby: &magnus::Ruby, - path: Option<&Path>, - ) -> magnus::Error { - match self { - Self::Runtime(message) => magnus::Error::new( - ruby.exception_arg_error(), - contextualize_message(path, message), - ), - Self::Type(message) => magnus::Error::new( - ruby.exception_type_error(), - contextualize_message(path, message), - ), - Self::DuplicateField(field) => magnus::Error::new( - ruby.exception_arg_error(), - format!("duplicate option: :{field}"), - ), - Self::Ruby(error) if error.is_kind_of(ruby.exception_range_error()) => { - magnus::Error::new( - ruby.exception_arg_error(), - magnus_error_message(&error, path), - ) - } - Self::Ruby(error) => match path { - Some(path) => { - contextualize_magnus_error(error, format_args!("invalid value for :{path}")) - } - None => error, - }, - } - } -} - -/// Prefix an error message when Serde identified the failing option path. -fn contextualize_message(path: Option<&Path>, message: String) -> String { - match path { - Some(path) => format!("invalid value for :{path}: {message}"), - None => message, - } -} - -/// Build the final Ruby error message without an intermediate context String. -fn magnus_error_message(error: &magnus::Error, path: Option<&Path>) -> String { - match (path, error.error_type()) { - (Some(path), ErrorType::Error(_, message)) => { - format!("invalid value for :{path}: {message}") - } - (Some(path), ErrorType::Exception(exception)) => { - format!("invalid value for :{path}: {exception}") - } - (Some(path), ErrorType::Jump(_)) => format!("invalid value for :{path}: {error}"), - (None, ErrorType::Error(_, message)) => message.as_ref().to_owned(), - (None, _) => error.to_string(), - } -} - -impl fmt::Display for Error { - fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Self::Runtime(message) | Self::Type(message) => formatter.write_str(message), - Self::DuplicateField(field) => write!(formatter, "duplicate field `{field}`"), - Self::Ruby(error) => error.fmt(formatter), - } - } -} - -impl std::error::Error for Error {} - -impl ::serde::ser::Error for Error { - fn custom(message: T) -> Self - where - T: fmt::Display, - { - Self::message(message.to_string()) - } -} - -impl ::serde::de::Error for Error { - fn custom(message: T) -> Self - where - T: fmt::Display, - { - Self::message(message.to_string()) - } - - fn invalid_type( - unexpected: ::serde::de::Unexpected<'_>, - expected: &dyn ::serde::de::Expected, - ) -> Self { - Self::type_error(format!( - "invalid type: expected {expected}, got {unexpected}" - )) - } - - fn duplicate_field(field: &'static str) -> Self { - Self::DuplicateField(field) - } -} - -impl From for Error { - fn from(error: magnus::Error) -> Self { - Self::Ruby(error) - } -} diff --git a/src/serde/ser.rs b/src/serde/ser.rs deleted file mode 100644 index 8d92909..0000000 --- a/src/serde/ser.rs +++ /dev/null @@ -1,18 +0,0 @@ -mod enums; -mod map_serializer; -mod seq_serializer; -mod serializer; -mod struct_serializer; -mod struct_variant_serializer; -mod tuple_variant_serializer; - -use ::serde::Serialize; -use magnus::{Ruby, Value}; - -use super::Error; -use serializer::Serializer; - -/// Serialize any Serde value into Ruby values. -pub(super) fn serialize(ruby: &Ruby, value: &(impl Serialize + ?Sized)) -> Result { - value.serialize(Serializer::new(ruby)) -} diff --git a/src/serde/ser/enums.rs b/src/serde/ser/enums.rs deleted file mode 100644 index abae6f2..0000000 --- a/src/serde/ser/enums.rs +++ /dev/null @@ -1,14 +0,0 @@ -use magnus::{IntoValue, Ruby, Value}; - -use crate::serde::Error; - -/// Wrap serialized enum data in the one-entry hash used by `serde_magnus`. -pub(super) fn nest( - ruby: &Ruby, - variant: &'static str, - data: impl IntoValue, -) -> Result { - let hash = ruby.hash_new(); - hash.aset(variant, data)?; - Ok(hash.into_value_with(ruby)) -} diff --git a/src/serde/ser/seq_serializer.rs b/src/serde/ser/seq_serializer.rs deleted file mode 100644 index 08a8418..0000000 --- a/src/serde/ser/seq_serializer.rs +++ /dev/null @@ -1,71 +0,0 @@ -use ::serde::{ - Serialize, - ser::{SerializeSeq, SerializeTuple, SerializeTupleStruct}, -}; -use magnus::{IntoValue, RArray, Ruby, Value}; - -use super::Serializer; -use crate::serde::Error; - -/// Serde sequence serializer backed by a Ruby array. -pub(super) struct SeqSerializer<'ruby> { - ruby: &'ruby Ruby, - array: RArray, -} - -impl<'ruby> SeqSerializer<'ruby> { - /// Create a sequence serializer for an allocated Ruby array. - pub(super) fn new(ruby: &'ruby Ruby, array: RArray) -> Self { - Self { ruby, array } - } -} - -impl SerializeSeq for SeqSerializer<'_> { - type Ok = Value; - type Error = Error; - - fn serialize_element(&mut self, element: &T) -> Result<(), Self::Error> - where - T: Serialize + ?Sized, - { - self.array - .push(element.serialize(Serializer::new(self.ruby))?) - .map_err(Into::into) - } - - fn end(self) -> Result { - Ok(self.array.into_value_with(self.ruby)) - } -} - -impl SerializeTuple for SeqSerializer<'_> { - type Ok = Value; - type Error = Error; - - fn serialize_element(&mut self, element: &T) -> Result<(), Self::Error> - where - T: Serialize + ?Sized, - { - SerializeSeq::serialize_element(self, element) - } - - fn end(self) -> Result { - SerializeSeq::end(self) - } -} - -impl SerializeTupleStruct for SeqSerializer<'_> { - type Ok = Value; - type Error = Error; - - fn serialize_field(&mut self, field: &T) -> Result<(), Self::Error> - where - T: Serialize + ?Sized, - { - SerializeSeq::serialize_element(self, field) - } - - fn end(self) -> Result { - SerializeSeq::end(self) - } -} diff --git a/src/serde/ser/serializer.rs b/src/serde/ser/serializer.rs deleted file mode 100644 index f0559fa..0000000 --- a/src/serde/ser/serializer.rs +++ /dev/null @@ -1,194 +0,0 @@ -use ::serde::Serialize; -use magnus::{IntoValue, Ruby, Value}; - -use super::super::Error; -use super::{ - enums::nest, map_serializer::MapSerializer, seq_serializer::SeqSerializer, - struct_serializer::StructSerializer, struct_variant_serializer::StructVariantSerializer, - tuple_variant_serializer::TupleVariantSerializer, -}; - -/// Implement primitive numeric serialization through Magnus's `IntoValue`. -macro_rules! impl_serialize_numbers { - ($($method:ident => $type:ty),+ $(,)?) => { - $( - fn $method(self, value: $type) -> Result { - Ok(value.into_value_with(self.ruby)) - } - )+ - }; -} - -/// Serde serializer that creates Ruby values. -pub(super) struct Serializer<'ruby> { - ruby: &'ruby Ruby, -} - -impl<'ruby> Serializer<'ruby> { - /// Create a serializer with upstream `serde_magnus` behavior. - pub(super) fn new(ruby: &'ruby Ruby) -> Self { - Self { ruby } - } -} - -impl<'ruby> ::serde::Serializer for Serializer<'ruby> { - type Ok = Value; - type Error = Error; - - type SerializeSeq = SeqSerializer<'ruby>; - type SerializeTuple = SeqSerializer<'ruby>; - type SerializeTupleStruct = SeqSerializer<'ruby>; - type SerializeTupleVariant = TupleVariantSerializer<'ruby>; - type SerializeMap = MapSerializer<'ruby>; - type SerializeStruct = StructSerializer<'ruby>; - type SerializeStructVariant = StructVariantSerializer<'ruby>; - - fn serialize_bool(self, value: bool) -> Result { - Ok(value.into_value_with(self.ruby)) - } - - impl_serialize_numbers! { - serialize_i8 => i8, - serialize_i16 => i16, - serialize_i32 => i32, - serialize_i64 => i64, - serialize_i128 => i128, - serialize_u8 => u8, - serialize_u16 => u16, - serialize_u32 => u32, - serialize_u64 => u64, - serialize_u128 => u128, - serialize_f32 => f32, - serialize_f64 => f64, - } - - fn serialize_char(self, value: char) -> Result { - Ok(value.into_value_with(self.ruby)) - } - - fn serialize_str(self, value: &str) -> Result { - Ok(value.into_value_with(self.ruby)) - } - - fn serialize_bytes(self, value: &[u8]) -> Result { - Ok(self.ruby.str_from_slice(value).into_value_with(self.ruby)) - } - - fn serialize_none(self) -> Result { - self.serialize_unit() - } - - fn serialize_some(self, value: &T) -> Result - where - T: Serialize + ?Sized, - { - value.serialize(self) - } - - fn serialize_unit(self) -> Result { - Ok(().into_value_with(self.ruby)) - } - - fn serialize_unit_struct(self, _name: &'static str) -> Result { - self.serialize_unit() - } - - fn serialize_unit_variant( - self, - _name: &'static str, - _index: u32, - variant: &'static str, - ) -> Result { - self.serialize_str(variant) - } - - fn serialize_newtype_struct( - self, - _name: &'static str, - value: &T, - ) -> Result - where - T: Serialize + ?Sized, - { - value.serialize(self) - } - - fn serialize_newtype_variant( - self, - _name: &'static str, - _index: u32, - variant: &'static str, - value: &T, - ) -> Result - where - T: Serialize + ?Sized, - { - nest( - self.ruby, - variant, - value.serialize(Serializer::new(self.ruby))?, - ) - } - - fn serialize_seq(self, len: Option) -> Result { - Ok(SeqSerializer::new( - self.ruby, - self.ruby.ary_new_capa(len.unwrap_or(0)), - )) - } - - fn serialize_tuple(self, len: usize) -> Result { - self.serialize_seq(Some(len)) - } - - fn serialize_tuple_struct( - self, - _name: &'static str, - len: usize, - ) -> Result { - self.serialize_seq(Some(len)) - } - - fn serialize_tuple_variant( - self, - _name: &'static str, - _index: u32, - variant: &'static str, - len: usize, - ) -> Result { - Ok(TupleVariantSerializer::new( - self.ruby, - variant, - self.ruby.ary_new_capa(len), - )) - } - - fn serialize_map(self, len: Option) -> Result { - Ok(MapSerializer::new( - self.ruby, - self.ruby.hash_new_capa(len.unwrap_or(0)), - )) - } - - fn serialize_struct( - self, - name: &'static str, - len: usize, - ) -> Result { - Ok(StructSerializer::new(self.ruby, name, len)) - } - - fn serialize_struct_variant( - self, - _name: &'static str, - _index: u32, - variant: &'static str, - len: usize, - ) -> Result { - Ok(StructVariantSerializer::new( - self.ruby, - variant, - self.ruby.hash_new_capa(len), - )) - } -} diff --git a/src/serde/ser/struct_variant_serializer.rs b/src/serde/ser/struct_variant_serializer.rs deleted file mode 100644 index 4557615..0000000 --- a/src/serde/ser/struct_variant_serializer.rs +++ /dev/null @@ -1,44 +0,0 @@ -use ::serde::{Serialize, ser::SerializeStructVariant}; -use magnus::{RHash, Ruby, Value}; - -use super::{Serializer, enums::nest}; -use crate::serde::Error; - -/// Serde struct-variant serializer backed by a Ruby hash. -pub(super) struct StructVariantSerializer<'ruby> { - ruby: &'ruby Ruby, - variant: &'static str, - hash: RHash, -} - -impl<'ruby> StructVariantSerializer<'ruby> { - /// Create a struct-variant serializer for an allocated Ruby hash. - pub(super) fn new(ruby: &'ruby Ruby, variant: &'static str, hash: RHash) -> Self { - Self { - ruby, - variant, - hash, - } - } -} - -impl SerializeStructVariant for StructVariantSerializer<'_> { - type Ok = Value; - type Error = Error; - - fn serialize_field(&mut self, name: &'static str, value: &T) -> Result<(), Self::Error> - where - T: Serialize + ?Sized, - { - self.hash - .aset( - self.ruby.to_symbol(name), - value.serialize(Serializer::new(self.ruby))?, - ) - .map_err(Into::into) - } - - fn end(self) -> Result { - nest(self.ruby, self.variant, self.hash) - } -} diff --git a/src/serde/ser/tuple_variant_serializer.rs b/src/serde/ser/tuple_variant_serializer.rs deleted file mode 100644 index 5c36f24..0000000 --- a/src/serde/ser/tuple_variant_serializer.rs +++ /dev/null @@ -1,41 +0,0 @@ -use ::serde::{Serialize, ser::SerializeTupleVariant}; -use magnus::{RArray, Ruby, Value}; - -use super::{Serializer, enums::nest}; -use crate::serde::Error; - -/// Serde tuple-variant serializer backed by a Ruby array. -pub(super) struct TupleVariantSerializer<'ruby> { - ruby: &'ruby Ruby, - variant: &'static str, - array: RArray, -} - -impl<'ruby> TupleVariantSerializer<'ruby> { - /// Create a tuple-variant serializer for an allocated Ruby array. - pub(super) fn new(ruby: &'ruby Ruby, variant: &'static str, array: RArray) -> Self { - Self { - ruby, - variant, - array, - } - } -} - -impl SerializeTupleVariant for TupleVariantSerializer<'_> { - type Ok = Value; - type Error = Error; - - fn serialize_field(&mut self, field: &T) -> Result<(), Self::Error> - where - T: Serialize + ?Sized, - { - self.array - .push(field.serialize(Serializer::new(self.ruby))?) - .map_err(Into::into) - } - - fn end(self) -> Result { - nest(self.ruby, self.variant, self.array) - } -} From da5f893f36add29107970b1f14fb31aebda6a3f0 Mon Sep 17 00:00:00 2001 From: gngpp Date: Thu, 19 Nov 2026 14:00:39 +0800 Subject: [PATCH 2/4] fix(ci): find libruby for Linux Rust tests --- Rakefile | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Rakefile b/Rakefile index 91f3ca2..c2c1e88 100644 --- a/Rakefile +++ b/Rakefile @@ -44,7 +44,13 @@ end desc "Run Rust tests" task :cargo_test do - sh "cargo test --workspace --locked" + test_env = {} + if RbConfig::CONFIG.fetch("host_os").start_with?("linux") + # Embedded Ruby tests need the active Ruby's shared library at startup. + test_env["LD_LIBRARY_PATH"] = [RbConfig::CONFIG.fetch("libdir"), ENV["LD_LIBRARY_PATH"]] + .compact.reject(&:empty?).join(File::PATH_SEPARATOR) + end + sh test_env, "cargo", "test", "--workspace", "--locked" end desc "Format Rust code" From 196b558a8771bf06967da8af30e67343cd7f3023 Mon Sep 17 00:00:00 2001 From: gngpp Date: Thu, 19 Nov 2026 14:16:28 +0800 Subject: [PATCH 3/4] fix(serde): preserve Ruby Hash preallocation --- crates/serde-magnus/src/ser/serializer.rs | 28 ++++++++++++++----- .../serde-magnus/src/ser/struct_serializer.rs | 6 ++-- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/crates/serde-magnus/src/ser/serializer.rs b/crates/serde-magnus/src/ser/serializer.rs index d570618..57ad519 100644 --- a/crates/serde-magnus/src/ser/serializer.rs +++ b/crates/serde-magnus/src/ser/serializer.rs @@ -1,4 +1,6 @@ -use magnus::{IntoValue, Ruby, Value}; +use std::ffi::c_long; + +use magnus::{IntoValue, RHash, Ruby, Value}; use serde::Serialize; use super::{ @@ -178,16 +180,19 @@ impl<'r> serde::Serializer for Serializer<'r> { )) } - fn serialize_map(self, _len: Option) -> Result { - Ok(MapSerializer::new(self.ruby, self.ruby.hash_new())) + fn serialize_map(self, len: Option) -> Result { + Ok(MapSerializer::new( + self.ruby, + hash_with_capacity(self.ruby, len.unwrap_or(0)), + )) } fn serialize_struct( self, name: &'static str, - _len: usize, + len: usize, ) -> Result { - Ok(StructSerializer::new(self.ruby, name)) + Ok(StructSerializer::new(self.ruby, name, len)) } fn serialize_struct_variant( @@ -195,12 +200,21 @@ impl<'r> serde::Serializer for Serializer<'r> { _name: &'static str, _index: u32, variant: &'static str, - _len: usize, + len: usize, ) -> Result { Ok(StructVariantSerializer::new( self.ruby, variant, - self.ruby.hash_new(), + hash_with_capacity(self.ruby, len), )) } } + +pub(super) fn hash_with_capacity(ruby: &Ruby, capacity: usize) -> RHash { + // Magnus passes the capacity to Ruby as a C long, which can be narrower than usize. + if c_long::try_from(capacity).is_ok() { + ruby.hash_new_capa(capacity) + } else { + ruby.hash_new() + } +} diff --git a/crates/serde-magnus/src/ser/struct_serializer.rs b/crates/serde-magnus/src/ser/struct_serializer.rs index 3151045..643427a 100644 --- a/crates/serde-magnus/src/ser/struct_serializer.rs +++ b/crates/serde-magnus/src/ser/struct_serializer.rs @@ -1,7 +1,7 @@ use magnus::{Integer, IntoValue, RHash, RString, Ruby, Value, value::ReprValue}; use serde::{Serialize, ser::SerializeStruct}; -use super::Serializer; +use super::serializer::{Serializer, hash_with_capacity}; use crate::{JSON_NUMBER_TOKEN, error::Error}; /// Serde struct serializer, including `serde_json` arbitrary-precision numbers. @@ -18,13 +18,13 @@ pub(super) enum StructSerializer<'ruby> { impl<'ruby> StructSerializer<'ruby> { /// Create the serializer selected by the Serde struct name. - pub(super) fn new(ruby: &'ruby Ruby, name: &'static str) -> Self { + pub(super) fn new(ruby: &'ruby Ruby, name: &'static str, len: usize) -> Self { if name == JSON_NUMBER_TOKEN { Self::Number { ruby, value: None } } else { Self::Map { ruby, - hash: ruby.hash_new(), + hash: hash_with_capacity(ruby, len), } } } From 5c535e34cfd99d78e8c97c5282e9da27a214d158 Mon Sep 17 00:00:00 2001 From: gngpp Date: Thu, 19 Nov 2026 14:31:23 +0800 Subject: [PATCH 4/4] chore(cargo): remove redundant homepage metadata --- Cargo.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 27033f0..15f380d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,6 @@ name = "wreq-ruby" description = "Ruby bindings for Wreq, HTTP client library in Rust." authors = ["SearchApi "] -homepage = "https://github.com/SearchApi/wreq-ruby" repository = "https://github.com/SearchApi/wreq-ruby" edition = "2024" version = "1.3.5"