Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }})
Expand Down
25 changes: 23 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
name = "wreq-ruby"
description = "Ruby bindings for Wreq, HTTP client library in Rust."
authors = ["SearchApi <support@searchapi.io>"]
homepage = "https://github.com/SearchApi/wreq-ruby"
repository = "https://github.com/SearchApi/wreq-ruby"
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
Expand Down Expand Up @@ -37,12 +40,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"
Expand Down
12 changes: 9 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,23 @@ end

desc "Run Rust tests"
task :cargo_test do
sh "cargo test"
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"
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]')"
Expand Down
1 change: 1 addition & 0 deletions crates/README.md
Original file line number Diff line number Diff line change
@@ -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.
29 changes: 29 additions & 0 deletions crates/serde-magnus/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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 <joe@jwilm.com>",
"George Claghorn <georgeclaghorn@gmail.com>",
]
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"] }
19 changes: 19 additions & 0 deletions crates/serde-magnus/LICENSE
Original file line number Diff line number Diff line change
@@ -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.
147 changes: 147 additions & 0 deletions crates/serde-magnus/README.md
Original file line number Diff line number Diff line change
@@ -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<String>
}

#[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.
Original file line number Diff line number Diff line change
@@ -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> {
Expand Down
Original file line number Diff line number Diff line change
@@ -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> {
Expand Down
Loading
Loading