Skip to content

Commit

Permalink
Merge pull request #1 from OneSignal/update-magnus
Browse files Browse the repository at this point in the history
Update to Magnus 0.6.2 (latest)
  • Loading branch information
jwilm authored Oct 19, 2023
2 parents cb9135a + bede9c2 commit 56a4854
Show file tree
Hide file tree
Showing 20 changed files with 96 additions and 91 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ jobs:
runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
rustup-toolchain:
- stable
- "1.51"
- "1.65"
ruby-version:
- "2.6"
- "2.7"
- "3.0"
- "3.1"
- "3.2"
Expand Down
10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "serde_magnus"
version = "0.2.2"
description = "Serde integration for Magnus"
edition = "2018"
rust-version = "1.51"
rust-version = "1.65"
authors = ["George Claghorn <[email protected]>"]
repository = "https://github.com/georgeclaghorn/serde-magnus"
homepage = "https://github.com/georgeclaghorn/serde-magnus"
Expand All @@ -16,12 +16,12 @@ exclude = [".gitignore", ".github"]

[dependencies]
serde = "1.0, <= 1.0.156"
magnus = "0.5"
rb-sys = "0.9, <= 0.9.71"
rb-sys-build = "0.9, <= 0.9.71"
magnus = "0.6.2"
rb-sys = "0.9, <= 0.9.81"
rb-sys-build = "0.9, <= 0.9.81"
tap = "1.0"

[dev-dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_bytes = "0.11"
magnus = { version = "0.5", features = ["embed"] }
magnus = { version = "0.6.2", features = ["embed"] }
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ assert_eq!(

## Requirements

`serde_magnus` requires Rust 1.51+ and Ruby 2.6+.
`serde_magnus` requires Rust 1.65+ and Ruby 3.0+.

## License

Expand Down
7 changes: 4 additions & 3 deletions src/de/deserializer.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use magnus::{
exception,
value::{Qfalse, Qtrue},
value::{qnil, Qfalse, Qtrue, ReprValue},
Fixnum, Float, RArray, RBignum, RHash, RString, Symbol, Value,
};

use serde::forward_to_deserialize_any;

use super::{ArrayDeserializer, EnumDeserializer, HashDeserializer};
Expand Down Expand Up @@ -124,15 +125,15 @@ impl<'i> serde::Deserializer<'i> for Deserializer {
if let Some(variant) = RString::from_value(self.value) {
return visitor.visit_enum(EnumDeserializer::new(
variant.to_string()?,
Value::default(),
qnil().as_value(),
));
}

if let Some(hash) = RHash::from_value(self.value) {
if hash.len() == 1 {
let keys: RArray = hash.funcall("keys", ())?;
let key: String = keys.entry(0)?;
let value = hash.get(key.as_str()).unwrap_or_default();
let value = hash.get(key.as_str()).unwrap_or_else(|| qnil().as_value());

return visitor.visit_enum(EnumDeserializer::new(key, value));
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/de/hash_deserializer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{array_enumerator::ArrayEnumerator, Deserializer};
use crate::error::Error;
use magnus::{exception, RHash};
use magnus::{exception, value::ReprValue, RHash};
use serde::de::{DeserializeSeed, MapAccess};
use std::iter::Peekable;

Expand Down
57 changes: 28 additions & 29 deletions src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ use self::{
hash_deserializer::HashDeserializer, variant_deserializer::VariantDeserializer,
};

use magnus::{Error, Value};
use magnus::{Error, IntoValue};
use serde::Deserialize;
use std::ops::Deref;

/// Deserialize a Ruby [`Value`][`magnus::Value`] to Rust.
///
Expand All @@ -32,7 +31,7 @@ use std::ops::Deref;
/// use serde_magnus::deserialize;
///
/// let input: Value = eval!("nil")?;
/// let output: () = deserialize(&input)?;
/// let output: () = deserialize(input)?;
/// assert_eq!((), output);
/// #
/// # Ok::<(), magnus::Error>(())
Expand All @@ -47,7 +46,7 @@ use std::ops::Deref;
/// # let _cleanup = unsafe { magnus::embed::init() };
/// #
/// let input: Value = eval!("true")?;
/// let output: bool = deserialize(&input)?;
/// let output: bool = deserialize(input)?;
/// assert_eq!(true, output);
/// #
/// # Ok::<(), magnus::Error>(())
Expand All @@ -62,7 +61,7 @@ use std::ops::Deref;
/// # let _cleanup = unsafe { magnus::embed::init() };
/// #
/// let input: Value = eval!("1234")?;
/// let output: i64 = deserialize(&input)?;
/// let output: i64 = deserialize(input)?;
/// assert_eq!(1234, output);
/// #
/// # Ok::<(), magnus::Error>(())
Expand All @@ -77,7 +76,7 @@ use std::ops::Deref;
/// # let _cleanup = unsafe { magnus::embed::init() };
/// #
/// let input: Value = eval!("3.14")?;
/// let output: f64 = deserialize(&input)?;
/// let output: f64 = deserialize(input)?;
/// assert_eq!(3.14, output);
/// #
/// # Ok::<(), magnus::Error>(())
Expand All @@ -92,7 +91,7 @@ use std::ops::Deref;
/// # let _cleanup = unsafe { magnus::embed::init() };
/// #
/// let input: Value = eval!(r#""Hello, world!""#)?;
/// let output: String = deserialize(&input)?;
/// let output: String = deserialize(input)?;
/// assert_eq!("Hello, world!", output);
/// #
/// # Ok::<(), magnus::Error>(())
Expand All @@ -107,11 +106,11 @@ use std::ops::Deref;
/// # let _cleanup = unsafe { magnus::embed::init() };
/// #
/// let input: Value = eval!("nil")?;
/// let output: Option<i64> = deserialize(&input)?;
/// let output: Option<i64> = deserialize(input)?;
/// assert_eq!(None, output);
///
/// let input: Value = eval!("1234")?;
/// let output: Option<i64> = deserialize(&input)?;
/// let output: Option<i64> = deserialize(input)?;
/// assert_eq!(Some(1234), output);
/// #
/// # Ok::<(), magnus::Error>(())
Expand All @@ -126,11 +125,11 @@ use std::ops::Deref;
/// # let _cleanup = unsafe { magnus::embed::init() };
/// #
/// let input: Value = eval!("{ 'Ok' => 1234 }")?;
/// let output: Result<i64, String> = deserialize(&input)?;
/// let output: Result<i64, String> = deserialize(input)?;
/// assert_eq!(Ok(1234), output);
///
/// let input: Value = eval!("{ 'Err' => 'something went wrong' }")?;
/// let output: Result<i64, String> = deserialize(&input)?;
/// let output: Result<i64, String> = deserialize(input)?;
/// assert_eq!(Err("something went wrong".into()), output);
/// #
/// # Ok::<(), magnus::Error>(())
Expand All @@ -152,7 +151,7 @@ use std::ops::Deref;
/// struct Foo;
///
/// let input: Value = eval!("nil")?;
/// let output: Foo = deserialize(&input)?;
/// let output: Foo = deserialize(input)?;
/// assert_eq!(Foo, output);
/// #
/// # Ok::<(), magnus::Error>(())
Expand All @@ -172,7 +171,7 @@ use std::ops::Deref;
/// struct Foo(u16);
///
/// let input: Value = eval!("1234")?;
/// let output: Foo = deserialize(&input)?;
/// let output: Foo = deserialize(input)?;
/// assert_eq!(Foo(1234), output);
/// #
/// # Ok::<(), magnus::Error>(())
Expand All @@ -192,7 +191,7 @@ use std::ops::Deref;
/// struct Foo(u16, bool, String);
///
/// let input: Value = eval!("[123, true, 'Hello, world!']")?;
/// let output: Foo = deserialize(&input)?;
/// let output: Foo = deserialize(input)?;
/// assert_eq!(Foo(123, true, "Hello, world!".into()), output);
/// #
/// # Ok::<(), magnus::Error>(())
Expand All @@ -215,7 +214,7 @@ use std::ops::Deref;
/// }
///
/// let input: Value = eval!("{ bar: 1234, baz: true, glorp: 'Hello, world!' }")?;
/// let output: Foo = deserialize(&input)?;
/// let output: Foo = deserialize(input)?;
/// assert_eq!(
/// Foo { bar: 1234, baz: true, glorp: "Hello, world!".into() },
/// output
Expand Down Expand Up @@ -254,7 +253,7 @@ use std::ops::Deref;
/// # enum Foo { Bar }
/// #
/// let input: Value = eval!("'Bar'")?;
/// let output: Foo = deserialize(&input)?;
/// let output: Foo = deserialize(input)?;
/// assert_eq!(Foo::Bar, output);
/// #
/// # Ok::<(), magnus::Error>(())
Expand All @@ -273,7 +272,7 @@ use std::ops::Deref;
/// # enum Foo { Baz(u16) }
/// #
/// let input: Value = eval!("{ 'Baz' => 1234 }")?;
/// let output: Foo = deserialize(&input)?;
/// let output: Foo = deserialize(input)?;
/// assert_eq!(Foo::Baz(1234), output);
/// #
/// # Ok::<(), magnus::Error>(())
Expand All @@ -292,7 +291,7 @@ use std::ops::Deref;
/// # enum Foo { Glorp(u16, bool, String) }
/// #
/// let input: Value = eval!("{ 'Glorp' => [123, true, 'Hello, world!'] }")?;
/// let output: Foo = deserialize(&input)?;
/// let output: Foo = deserialize(input)?;
/// assert_eq!(Foo::Glorp(123, true, "Hello, world!".into()), output);
/// #
/// # Ok::<(), magnus::Error>(())
Expand Down Expand Up @@ -325,7 +324,7 @@ use std::ops::Deref;
/// }
/// }
/// "#)?;
/// let output: Foo = deserialize(&input)?;
/// let output: Foo = deserialize(input)?;
/// assert_eq!(
/// Foo::Quux { frob: 1234, wally: true, plugh: "Hello, world!".into() },
/// output
Expand All @@ -345,7 +344,7 @@ use std::ops::Deref;
/// # let _cleanup = unsafe { magnus::embed::init() };
/// #
/// let input: Value = eval!("[123, true, 'Hello, world!']")?;
/// let output: (i16, bool, String) = deserialize(&input)?;
/// let output: (i16, bool, String) = deserialize(input)?;
/// assert_eq!((123, true, "Hello, world!".into()), output);
/// #
/// # Ok::<(), magnus::Error>(())
Expand All @@ -360,7 +359,7 @@ use std::ops::Deref;
/// # let _cleanup = unsafe { magnus::embed::init() };
/// #
/// let input: Value = eval!("[123, 456, 789]")?;
/// let output: [i64; 3] = deserialize(&input)?;
/// let output: [i64; 3] = deserialize(input)?;
/// assert_eq!([123, 456, 789], output);
/// #
/// # Ok::<(), magnus::Error>(())
Expand All @@ -377,7 +376,7 @@ use std::ops::Deref;
/// # let _cleanup = unsafe { magnus::embed::init() };
/// #
/// let input: Value = eval!("[123, 456, 789]")?;
/// let output: Vec<u64> = deserialize(&input)?;
/// let output: Vec<u64> = deserialize(input)?;
/// assert_eq!(vec![123, 456, 789], output);
/// #
/// # Ok::<(), magnus::Error>(())
Expand All @@ -401,7 +400,7 @@ use std::ops::Deref;
/// "goodbye" => "hello"
/// }
/// "#)?;
/// let output: HashMap<String, String> = deserialize(&input)?;
/// let output: HashMap<String, String> = deserialize(input)?;
/// assert_eq!(4, output.len());
/// assert_eq!(Some(&String::from("no")), output.get("yes"));
/// #
Expand All @@ -428,7 +427,7 @@ use std::ops::Deref;
/// # let _cleanup = unsafe { magnus::embed::init() };
/// #
/// let input: Value = eval!("'Hello, world!'")?;
/// let output: Result<&str, Error> = deserialize(&input);
/// let output: Result<&str, Error> = deserialize(input);
/// assert!(output.is_err());
/// assert_eq!(
/// r#"TypeError: invalid type: expected a borrowed string, got string "Hello, world!""#,
Expand All @@ -447,7 +446,7 @@ use std::ops::Deref;
/// # let _cleanup = unsafe { magnus::embed::init() };
/// #
/// let input: Value = eval!("'Hello, world!'")?;
/// let output: String = deserialize(&input)?;
/// let output: String = deserialize(input)?;
/// assert_eq!("Hello, world!", output);
/// #
/// # Ok::<(), magnus::Error>(())
Expand All @@ -464,7 +463,7 @@ use std::ops::Deref;
/// # let _cleanup = unsafe { magnus::embed::init() };
/// #
/// let input: Value = eval!("'☃'")?;
/// let output: Result<&[u8], Error> = deserialize(&input);
/// let output: Result<&[u8], Error> = deserialize(input);
/// assert!(output.is_err());
/// assert_eq!(
/// "TypeError: can't deserialize into byte slice",
Expand All @@ -485,15 +484,15 @@ use std::ops::Deref;
/// use serde_bytes::ByteBuf;
///
/// let input: Value = eval!("'☃'")?;
/// let output: ByteBuf = deserialize(&input)?;
/// let output: ByteBuf = deserialize(input)?;
/// assert_eq!(vec![226, 152, 131], output.into_vec());
/// #
/// # Ok::<(), magnus::Error>(())
/// ```
pub fn deserialize<'i, Input, Output>(input: Input) -> Result<Output, Error>
where
Input: Deref<Target = Value>,
Input: IntoValue,
Output: Deserialize<'i>,
{
Output::deserialize(Deserializer::new(*input)).map_err(Into::into)
Output::deserialize(Deserializer::new(input.into_value())).map_err(Into::into)
}
2 changes: 1 addition & 1 deletion src/de/variant_deserializer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{ArrayDeserializer, Deserializer, HashDeserializer};
use crate::error::Error;
use magnus::{RArray, RHash, Value};
use magnus::{value::ReprValue, RArray, RHash, Value};
use serde::de::{DeserializeSeed, Unexpected, VariantAccess};

pub struct VariantDeserializer {
Expand Down
9 changes: 4 additions & 5 deletions src/ser/enums.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use crate::error::Error;
use magnus::{RHash, Value};
use std::ops::Deref;
use magnus::{IntoValue, RHash, Value};

pub fn nest<Data>(variant: &'static str, data: Data) -> Result<Value, Error>
where
Data: Deref<Target = Value>,
Data: IntoValue,
{
let hash = RHash::new();
hash.aset(variant, *data)?;
Ok(*hash)
hash.aset(variant, data)?;
Ok(hash.into_value())
}
9 changes: 6 additions & 3 deletions src/ser/map_serializer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use super::Serializer;
use crate::error::Error;
use magnus::{RHash, Value};
use magnus::{
value::{qnil, ReprValue},
IntoValue, RHash, Value,
};
use serde::{ser::SerializeMap, Serialize};

pub struct MapSerializer {
Expand All @@ -12,7 +15,7 @@ impl MapSerializer {
pub fn new(hash: RHash) -> MapSerializer {
MapSerializer {
hash,
key: Value::default(),
key: qnil().as_value(),
}
}
}
Expand All @@ -39,6 +42,6 @@ impl SerializeMap for MapSerializer {
}

fn end(self) -> Result<Self::Ok, Self::Error> {
Ok(*self.hash)
Ok(self.hash.into_value())
}
}
5 changes: 1 addition & 4 deletions src/ser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,5 @@ where
Input: Serialize + ?Sized,
Output: TryConvert,
{
input
.serialize(Serializer)?
.try_convert()
.map_err(Into::into)
TryConvert::try_convert(input.serialize(Serializer)?).map_err(Into::into)
}
Loading

0 comments on commit 56a4854

Please sign in to comment.