diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0c2a87e..87f0077 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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" diff --git a/Cargo.toml b/Cargo.toml index 44fc33b..56d60c9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 "] repository = "https://github.com/georgeclaghorn/serde-magnus" homepage = "https://github.com/georgeclaghorn/serde-magnus" @@ -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"] } diff --git a/README.md b/README.md index 1cf0206..9ed6600 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/de/deserializer.rs b/src/de/deserializer.rs index 590f0bf..5e44625 100644 --- a/src/de/deserializer.rs +++ b/src/de/deserializer.rs @@ -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}; @@ -124,7 +125,7 @@ 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(), )); } @@ -132,7 +133,7 @@ impl<'i> serde::Deserializer<'i> for Deserializer { 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 { diff --git a/src/de/hash_deserializer.rs b/src/de/hash_deserializer.rs index be90b70..d8ac607 100644 --- a/src/de/hash_deserializer.rs +++ b/src/de/hash_deserializer.rs @@ -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; diff --git a/src/de/mod.rs b/src/de/mod.rs index 364967e..018f34a 100644 --- a/src/de/mod.rs +++ b/src/de/mod.rs @@ -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. /// @@ -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>(()) @@ -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>(()) @@ -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>(()) @@ -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>(()) @@ -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>(()) @@ -107,11 +106,11 @@ use std::ops::Deref; /// # let _cleanup = unsafe { magnus::embed::init() }; /// # /// let input: Value = eval!("nil")?; -/// let output: Option = deserialize(&input)?; +/// let output: Option = deserialize(input)?; /// assert_eq!(None, output); /// /// let input: Value = eval!("1234")?; -/// let output: Option = deserialize(&input)?; +/// let output: Option = deserialize(input)?; /// assert_eq!(Some(1234), output); /// # /// # Ok::<(), magnus::Error>(()) @@ -126,11 +125,11 @@ use std::ops::Deref; /// # let _cleanup = unsafe { magnus::embed::init() }; /// # /// let input: Value = eval!("{ 'Ok' => 1234 }")?; -/// let output: Result = deserialize(&input)?; +/// let output: Result = deserialize(input)?; /// assert_eq!(Ok(1234), output); /// /// let input: Value = eval!("{ 'Err' => 'something went wrong' }")?; -/// let output: Result = deserialize(&input)?; +/// let output: Result = deserialize(input)?; /// assert_eq!(Err("something went wrong".into()), output); /// # /// # Ok::<(), magnus::Error>(()) @@ -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>(()) @@ -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>(()) @@ -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>(()) @@ -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 @@ -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>(()) @@ -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>(()) @@ -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>(()) @@ -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 @@ -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>(()) @@ -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>(()) @@ -377,7 +376,7 @@ use std::ops::Deref; /// # let _cleanup = unsafe { magnus::embed::init() }; /// # /// let input: Value = eval!("[123, 456, 789]")?; -/// let output: Vec = deserialize(&input)?; +/// let output: Vec = deserialize(input)?; /// assert_eq!(vec![123, 456, 789], output); /// # /// # Ok::<(), magnus::Error>(()) @@ -401,7 +400,7 @@ use std::ops::Deref; /// "goodbye" => "hello" /// } /// "#)?; -/// let output: HashMap = deserialize(&input)?; +/// let output: HashMap = deserialize(input)?; /// assert_eq!(4, output.len()); /// assert_eq!(Some(&String::from("no")), output.get("yes")); /// # @@ -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!""#, @@ -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>(()) @@ -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", @@ -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 where - Input: Deref, + 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) } diff --git a/src/de/variant_deserializer.rs b/src/de/variant_deserializer.rs index c690de0..d2631d0 100644 --- a/src/de/variant_deserializer.rs +++ b/src/de/variant_deserializer.rs @@ -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 { diff --git a/src/ser/enums.rs b/src/ser/enums.rs index 469d0bb..106adcc 100644 --- a/src/ser/enums.rs +++ b/src/ser/enums.rs @@ -1,12 +1,11 @@ use crate::error::Error; -use magnus::{RHash, Value}; -use std::ops::Deref; +use magnus::{IntoValue, RHash, Value}; pub fn nest(variant: &'static str, data: Data) -> Result where - Data: Deref, + Data: IntoValue, { let hash = RHash::new(); - hash.aset(variant, *data)?; - Ok(*hash) + hash.aset(variant, data)?; + Ok(hash.into_value()) } diff --git a/src/ser/map_serializer.rs b/src/ser/map_serializer.rs index b395382..17cebaa 100644 --- a/src/ser/map_serializer.rs +++ b/src/ser/map_serializer.rs @@ -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 { @@ -12,7 +15,7 @@ impl MapSerializer { pub fn new(hash: RHash) -> MapSerializer { MapSerializer { hash, - key: Value::default(), + key: qnil().as_value(), } } } @@ -39,6 +42,6 @@ impl SerializeMap for MapSerializer { } fn end(self) -> Result { - Ok(*self.hash) + Ok(self.hash.into_value()) } } diff --git a/src/ser/mod.rs b/src/ser/mod.rs index b3ff2cb..3e22732 100644 --- a/src/ser/mod.rs +++ b/src/ser/mod.rs @@ -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) } diff --git a/src/ser/seq_serializer.rs b/src/ser/seq_serializer.rs index 03d2a48..5ef9d1c 100644 --- a/src/ser/seq_serializer.rs +++ b/src/ser/seq_serializer.rs @@ -1,6 +1,6 @@ use super::Serializer; use crate::error::Error; -use magnus::{RArray, Value}; +use magnus::{IntoValue, RArray, Value}; use serde::{ ser::{SerializeSeq, SerializeTuple, SerializeTupleStruct}, Serialize, @@ -30,7 +30,7 @@ impl SerializeSeq for SeqSerializer { } fn end(self) -> Result { - Ok(*self.array) + Ok(self.array.into_value()) } } diff --git a/src/ser/serializer.rs b/src/ser/serializer.rs index 6338d21..3cbeadb 100644 --- a/src/ser/serializer.rs +++ b/src/ser/serializer.rs @@ -1,4 +1,4 @@ -use magnus::{RArray, RHash, RString, Value}; +use magnus::{IntoValue, RArray, RHash, RString, Value}; use serde::Serialize; use super::{ @@ -22,63 +22,63 @@ impl serde::Serializer for Serializer { type SerializeStructVariant = StructVariantSerializer; fn serialize_bool(self, value: bool) -> Result { - Ok(value.into()) + Ok(value.into_value()) } fn serialize_i8(self, value: i8) -> Result { - Ok(value.into()) + Ok(value.into_value()) } fn serialize_i16(self, value: i16) -> Result { - Ok(value.into()) + Ok(value.into_value()) } fn serialize_i32(self, value: i32) -> Result { - Ok(value.into()) + Ok(value.into_value()) } fn serialize_i64(self, value: i64) -> Result { - Ok(value.into()) + Ok(value.into_value()) } fn serialize_u8(self, value: u8) -> Result { - Ok(value.into()) + Ok(value.into_value()) } fn serialize_u16(self, value: u16) -> Result { - Ok(value.into()) + Ok(value.into_value()) } fn serialize_u32(self, value: u32) -> Result { - Ok(value.into()) + Ok(value.into_value()) } fn serialize_u64(self, value: u64) -> Result { - Ok(value.into()) + Ok(value.into_value()) } fn serialize_f32(self, value: f32) -> Result { - Ok(value.into()) + Ok(value.into_value()) } fn serialize_f64(self, value: f64) -> Result { - Ok(value.into()) + Ok(value.into_value()) } fn serialize_char(self, value: char) -> Result { - Ok(value.into()) + Ok(value.into_value()) } fn serialize_str(self, value: &str) -> Result { - Ok(value.into()) + Ok(value.into_value()) } fn serialize_bytes(self, value: &[u8]) -> Result { - Ok(*RString::from_slice(value)) + Ok(RString::from_slice(value).into_value()) } fn serialize_none(self) -> Result { - Ok(().into()) + Ok(().into_value()) } fn serialize_some(self, value: &Value) -> Result @@ -89,11 +89,11 @@ impl serde::Serializer for Serializer { } fn serialize_unit(self) -> Result { - Ok(().into()) + Ok(().into_value()) } fn serialize_unit_struct(self, _name: &'static str) -> Result { - Ok(().into()) + Ok(().into_value()) } fn serialize_unit_variant( @@ -102,7 +102,7 @@ impl serde::Serializer for Serializer { _index: u32, variant: &'static str, ) -> Result { - Ok(variant.into()) + Ok(variant.into_value()) } fn serialize_newtype_struct( @@ -126,7 +126,7 @@ impl serde::Serializer for Serializer { where Value: Serialize + ?Sized, { - nest(variant, &value.serialize(self)?) + nest(variant, value.serialize(self)?) } fn serialize_seq(self, len: Option) -> Result { diff --git a/src/ser/struct_serializer.rs b/src/ser/struct_serializer.rs index 2aaf126..9fddaee 100644 --- a/src/ser/struct_serializer.rs +++ b/src/ser/struct_serializer.rs @@ -1,6 +1,6 @@ use super::Serializer; use crate::error::Error; -use magnus::{RHash, Symbol, Value}; +use magnus::{IntoValue, RHash, Symbol, Value}; use serde::{ser::SerializeStruct, Serialize}; pub struct StructSerializer { @@ -31,6 +31,6 @@ impl SerializeStruct for StructSerializer { } fn end(self) -> Result { - Ok(*self.hash) + Ok(self.hash.into_value()) } } diff --git a/tests/deserializing_booleans.rs b/tests/deserializing_booleans.rs index 882fd74..9aac572 100644 --- a/tests/deserializing_booleans.rs +++ b/tests/deserializing_booleans.rs @@ -1,14 +1,17 @@ -use magnus::{Error, QFALSE, QTRUE}; +use magnus::{ + value::{qfalse, qtrue, ReprValue}, + Error, +}; use serde_magnus::deserialize; #[test] fn test_deserializing_booleans() -> Result<(), Error> { let _cleanup = unsafe { magnus::embed::init() }; - let output: bool = deserialize(QTRUE)?; + let output: bool = deserialize(qtrue().as_value())?; assert_eq!(true, output); - let output: bool = deserialize(QFALSE)?; + let output: bool = deserialize(qfalse().as_value())?; assert_eq!(false, output); Ok(()) diff --git a/tests/deserializing_options.rs b/tests/deserializing_options.rs index 75695fc..377a10f 100644 --- a/tests/deserializing_options.rs +++ b/tests/deserializing_options.rs @@ -1,11 +1,11 @@ -use magnus::{Error, Integer, QNIL}; +use magnus::{value::qnil, Error, Integer, IntoValue}; use serde_magnus::deserialize; #[test] fn test_deserializing_options() -> Result<(), Error> { let _cleanup = unsafe { magnus::embed::init() }; - let output: Option = deserialize(QNIL)?; + let output: Option = deserialize(qnil().into_value())?; assert_eq!(None, output); let output: Option = deserialize(Integer::from_u64(123))?; diff --git a/tests/deserializing_structs.rs b/tests/deserializing_structs.rs index 34a93fd..e58948e 100644 --- a/tests/deserializing_structs.rs +++ b/tests/deserializing_structs.rs @@ -1,4 +1,4 @@ -use magnus::{eval, Error, Integer, RArray, RHash, QNIL}; +use magnus::{eval, value::qnil, Error, Integer, IntoValue, RArray, RHash}; use serde::Deserialize; use serde_magnus::deserialize; @@ -20,7 +20,7 @@ struct D { fn test_deserializing_structs() -> Result<(), Error> { let _cleanup = unsafe { magnus::embed::init() }; - assert_eq!(A, deserialize(QNIL)?); + assert_eq!(A, deserialize(qnil().into_value())?); let input = Integer::from_u64(123); let output: B = deserialize(input)?; diff --git a/tests/deserializing_tuples.rs b/tests/deserializing_tuples.rs index e592817..300139a 100644 --- a/tests/deserializing_tuples.rs +++ b/tests/deserializing_tuples.rs @@ -1,4 +1,8 @@ -use magnus::{eval, Error, RArray, QNIL}; +use magnus::{ + eval, + value::{qnil, ReprValue}, + Error, RArray, +}; use serde_magnus::deserialize; #[test] @@ -6,7 +10,7 @@ fn test_deserializing_tuples() -> Result<(), Error> { let _cleanup = unsafe { magnus::embed::init() }; // From nil - assert_eq!((), deserialize(QNIL)?); + assert_eq!((), deserialize(qnil().as_value())?); // From a homogeneous array let input: RArray = eval!("[ 123, 456, 789 ]")?; diff --git a/tests/serializing_options.rs b/tests/serializing_options.rs index 22e96b1..daeae5e 100644 --- a/tests/serializing_options.rs +++ b/tests/serializing_options.rs @@ -1,4 +1,4 @@ -use magnus::{Error, Integer, Value}; +use magnus::{value::ReprValue, Error, Integer, Value}; use serde_magnus::serialize; #[test] diff --git a/tests/serializing_structs.rs b/tests/serializing_structs.rs index 06ead1d..cce0771 100644 --- a/tests/serializing_structs.rs +++ b/tests/serializing_structs.rs @@ -1,4 +1,4 @@ -use magnus::{eval, Error, Integer, RArray, RHash, Value}; +use magnus::{eval, value::ReprValue, Error, Integer, RArray, RHash, Value}; use serde::Serialize; use serde_magnus::serialize; diff --git a/tests/serializing_tuples.rs b/tests/serializing_tuples.rs index 97ddb28..4d59e29 100644 --- a/tests/serializing_tuples.rs +++ b/tests/serializing_tuples.rs @@ -1,4 +1,4 @@ -use magnus::{eval, Error, RArray, Value}; +use magnus::{eval, value::ReprValue, Error, RArray, Value}; use serde_magnus::serialize; #[test]