-
-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
glib: Add optional support for serialization and deserialization with…
… serde This feature is gated as `serde` Supports both serialization and deserialization: - glib::ByteArray - glib::Bytes - glib::GString Supports serialization only: - glib::GStr - glib::StrV Collection types are also supported as long as the type parameters implement the necessary traits: - glib::Slice<T: TransparentType + ..> - glib::PtrSlice<T: TransparentPtrType + ..> - glib::List<T: TransparentPtrType + ..> - glib::SList<T: TransparentPtrType + ..>
- Loading branch information
Showing
12 changed files
with
474 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Submodule gir
updated
73 files
Submodule gir-files
updated
20 files
+0 −7 | .github/dependabot.yml | |
+4 −4 | .github/workflows/CI.yml | |
+2 −2 | .github/workflows/regenerate_scheduled.yml | |
+4 −39 | Atk-1.0.gir | |
+525 −789 | GLib-2.0.gir | |
+130 −369 | GObject-2.0.gir | |
+8 −13 | Gdk-3.0.gir | |
+39 −612 | Gdk-4.0.gir | |
+8 −8 | GdkPixbuf-2.0.gir | |
+1 −32 | GdkWayland-4.0.gir | |
+9 −9 | GdkWin32-4.0.gir | |
+321 −794 | Gio-2.0.gir | |
+0 −17 | Graphene-1.0.gir | |
+20 −2,415 | Gsk-4.0.gir | |
+14 −14 | Gtk-3.0.gir | |
+448 −2,944 | Gtk-4.0.gir | |
+209 −2,894 | HarfBuzz-0.0.gir | |
+6 −6 | Pango-1.0.gir | |
+0 −1 | cairo-1.0.gir | |
+0 −6 | fix.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Take a look at the license at the top of the repository in the LICENSE file. | ||
|
||
use serde::Deserializer; | ||
|
||
use super::*; | ||
|
||
use crate::ByteArray; | ||
|
||
serialize_impl!(ByteArray, Bytes(b) => b); | ||
|
||
deserialize_impl! { | ||
ByteArray, | ||
"a sequence of bytes", | ||
Deserializer::deserialize_seq => match impl { | ||
Bytes(b) => Ok(ByteArray::from(b)), | ||
ByteBuf(buf) => Ok(ByteArray::from(buf.as_slice())), | ||
Seq(s) => { | ||
// See https://docs.rs/serde/1.0.159/src/serde/de/impls.rs.html#1038 | ||
// and https://docs.rs/serde/1.0.159/src/serde/private/size_hint.rs.html#13 | ||
let mut bytes = Vec::with_capacity(min(s.size_hint().unwrap_or(0), 4096)); | ||
|
||
while let Some(byte) = s.next_element()? { | ||
bytes.push(byte) | ||
} | ||
|
||
Ok(ByteArray::from(bytes.as_slice())) | ||
}, | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::{gformat, ByteArray}; | ||
|
||
#[test] | ||
fn serialization() { | ||
let json = match serde_json::to_value(ByteArray::from( | ||
gformat!("Lorem ipsum dolor sit amet").as_bytes(), | ||
)) { | ||
Ok(v) => Some(v), | ||
Err(_) => None, | ||
}; | ||
|
||
assert_ne!(json, None); | ||
} | ||
|
||
#[test] | ||
fn deserialization() { | ||
let json_str = r#"[76,111,114,101,109,32,105,112,115,117,109,32,100,111,108,111,114,32,115,105,116,32,97,109,101]"#; | ||
|
||
serde_json::from_str::<ByteArray>(json_str).unwrap(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Take a look at the license at the top of the repository in the LICENSE file. | ||
|
||
use serde::Deserializer; | ||
|
||
use super::*; | ||
|
||
use crate::Bytes; | ||
|
||
serialize_impl!(Bytes, Bytes(b) => b); | ||
|
||
deserialize_impl! { | ||
Bytes, | ||
"a sequence of bytes", | ||
Deserializer::deserialize_seq => match impl { | ||
Bytes(b) => Ok(Bytes::from_owned(b.to_owned())), | ||
ByteBuf(buf) => Ok(Bytes::from_owned(buf)), | ||
Seq(s) => { | ||
let mut bytes = Vec::with_capacity(min(s.size_hint().unwrap_or(0), 4096)); | ||
|
||
while let Some(byte) = s.next_element()? { | ||
bytes.push(byte) | ||
} | ||
|
||
Ok(Bytes::from_owned(bytes)) | ||
}, | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::{gformat, Bytes}; | ||
|
||
#[test] | ||
fn serialization() { | ||
let json = match serde_json::to_value(Bytes::from_owned( | ||
gformat!("Lorem ipsum dolor sit amet").into_bytes(), | ||
)) { | ||
Ok(v) => Some(v), | ||
Err(_) => None, | ||
}; | ||
|
||
assert_ne!(json, None); | ||
} | ||
|
||
#[test] | ||
fn deserialization() { | ||
let json_str = r#"[76,111,114,101,109,32,105,112,115,117,109,32,100,111,108,111,114,32,115,105,116,32,97,109,101]"#; | ||
|
||
serde_json::from_str::<Bytes>(json_str).unwrap(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
// Take a look at the license at the top of the repository in the LICENSE file. | ||
|
||
use super::*; | ||
|
||
use crate::{ | ||
translate::{TransparentPtrType, TransparentType}, | ||
List, PtrSlice, SList, Slice, StrV, | ||
}; | ||
|
||
serialize_impl!(Slice<T: TransparentType>, Sequence(iter) => iter); | ||
|
||
deserialize_impl! { | ||
Slice<T: TransparentType>, | ||
"a sequence of GLib transparent values", | ||
Deserializer::deserialize_seq => match impl { | ||
Seq(s) => { | ||
let mut slice = Slice::with_capacity(min(s.size_hint().unwrap_or(0), 4096)); | ||
|
||
while let Some(item) = s.next_element()? { | ||
slice.push(item) | ||
} | ||
|
||
Ok(slice) | ||
}, | ||
} | ||
} | ||
|
||
serialize_impl!(PtrSlice<T: TransparentPtrType>, Sequence(iter) => iter); | ||
|
||
deserialize_impl! { | ||
PtrSlice<T: TransparentPtrType>, | ||
"a sequence of GLib transparent pointer values", | ||
Deserializer::deserialize_seq => match impl { | ||
Seq(s) => { | ||
let mut slice = PtrSlice::with_capacity(min(s.size_hint().unwrap_or(0), 4096)); | ||
|
||
while let Some(item) = s.next_element()? { | ||
slice.push(item) | ||
} | ||
|
||
Ok(slice) | ||
}, | ||
} | ||
} | ||
|
||
serialize_impl!(List<T: TransparentPtrType>, Sequence(iter) => iter.iter()); | ||
|
||
deserialize_impl! { | ||
List<T: TransparentPtrType>, | ||
"a sequence of GLib transparent pointer values", | ||
Deserializer::deserialize_seq => match impl { | ||
Seq(s) => { | ||
let mut list = List::new(); | ||
|
||
while let Some(item) = s.next_element()? { | ||
list.push_front(item) | ||
} | ||
list.reverse(); | ||
|
||
Ok(list) | ||
}, | ||
} | ||
} | ||
|
||
serialize_impl!(SList<T: TransparentPtrType>, Sequence(iter) => iter.iter()); | ||
|
||
deserialize_impl! { | ||
SList<T: TransparentPtrType>, | ||
"a sequence of GLib transparent pointer values", | ||
Deserializer::deserialize_seq => match impl { | ||
Seq(s) => { | ||
let mut list = SList::new(); | ||
|
||
while let Some(item) = s.next_element()? { | ||
list.push_front(item) | ||
} | ||
list.reverse(); | ||
|
||
Ok(list) | ||
}, | ||
} | ||
} | ||
|
||
serialize_impl!(StrV, Sequence(iter) => iter); | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use serde_json::json; | ||
|
||
use crate::{gformat, Bytes, List, PtrSlice, SList, Slice}; | ||
|
||
#[test] | ||
fn serialization() { | ||
let bytes = gformat!("Lorem ipsum dolor sit amet").into_bytes(); | ||
|
||
let slice = Slice::from([ | ||
Bytes::from_owned(bytes[..].to_vec()), | ||
Bytes::from_owned(bytes[1..].to_vec()), | ||
Bytes::from_owned(bytes[2..].to_vec()), | ||
Bytes::from_owned(bytes[3..].to_vec()), | ||
]); | ||
|
||
let ptr_slice = PtrSlice::from([ | ||
Bytes::from_owned(bytes[..].to_vec()), | ||
Bytes::from_owned(bytes[1..].to_vec()), | ||
Bytes::from_owned(bytes[2..].to_vec()), | ||
Bytes::from_owned(bytes[3..].to_vec()), | ||
]); | ||
|
||
let mut list = List::<Bytes>::new(); | ||
list.push_front(Bytes::from_owned(bytes[..].to_vec())); | ||
list.push_front(Bytes::from_owned(bytes[1..].to_vec())); | ||
list.push_front(Bytes::from_owned(bytes[2..].to_vec())); | ||
list.push_front(Bytes::from_owned(bytes[3..].to_vec())); | ||
list.reverse(); | ||
|
||
let mut slist = SList::<Bytes>::new(); | ||
slist.push_front(Bytes::from_owned(bytes[..].to_vec())); | ||
slist.push_front(Bytes::from_owned(bytes[1..].to_vec())); | ||
slist.push_front(Bytes::from_owned(bytes[2..].to_vec())); | ||
slist.push_front(Bytes::from_owned(bytes[3..].to_vec())); | ||
slist.reverse(); | ||
|
||
assert_eq!(json!(&slice), json!(&list)); | ||
assert_eq!(json!(&slice), json!(&slist)); | ||
assert_eq!(json!(&ptr_slice), json!(&list)); | ||
assert_eq!(json!(&ptr_slice), json!(&slist)); | ||
assert_eq!(json!(&slice), json!(&ptr_slice)); | ||
assert_eq!(json!(&list), json!(&slist)); | ||
} | ||
|
||
#[test] | ||
fn deserialization() { | ||
let json_str = r#" | ||
[ | ||
[76,111,114,101,109,32,105,112,115,117,109,32,100,111,108,111,114,32,115,105,116,32,97,109,101], | ||
[111,114,101,109,32,105,112,115,117,109,32,100,111,108,111,114,32,115,105,116,32,97,109,101], | ||
[114,101,109,32,105,112,115,117,109,32,100,111,108,111,114,32,115,105,116,32,97,109,101], | ||
[101,109,32,105,112,115,117,109,32,100,111,108,111,114,32,115,105,116,32,97,109,101] | ||
]"#; | ||
serde_json::from_str::<Slice<Bytes>>(json_str).unwrap(); | ||
serde_json::from_str::<PtrSlice<Bytes>>(json_str).unwrap(); | ||
serde_json::from_str::<List<Bytes>>(json_str).unwrap(); | ||
serde_json::from_str::<SList<Bytes>>(json_str).unwrap(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Take a look at the license at the top of the repository in the LICENSE file. | ||
|
||
use super::*; | ||
|
||
use crate::{gformat, GStr, GString, GStringPtr}; | ||
use serde::de; | ||
|
||
serialize_impl!(GStr, str(s) => s.as_str()); | ||
|
||
serialize_impl!(GString, str(s) => s.as_str()); | ||
|
||
deserialize_impl! { | ||
GString, | ||
"a valid UTF-8 string", | ||
Deserializer::deserialize_string => match impl { | ||
str(s) => Ok(gformat!("{s}")), | ||
String(s) => GString::from_string_checked(s).map_err(|e| de::Error::custom(e)), | ||
} | ||
} | ||
|
||
serialize_impl!(GStringPtr, str(s) => s.to_str()); | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::{translate::ToGlibPtr, GString, StrV}; | ||
use serde_json::json; | ||
|
||
use crate::gformat; | ||
|
||
#[test] | ||
fn serialization() { | ||
let gstring = gformat!("Lorem ipsum dolor sit amet"); | ||
let gstr = gstring.as_gstr(); | ||
let gstringptr = | ||
&unsafe { StrV::from_glib_none(vec![gstring.to_owned()].to_glib_none().0) }[0]; | ||
|
||
assert_eq!(json!(&gstring), json!(gstr)); | ||
assert_eq!(json!(&gstring), json!(gstringptr)); | ||
assert_eq!(json!(gstr), json!(gstringptr)); | ||
} | ||
|
||
#[test] | ||
fn deserialization() { | ||
let json_str = r#""Lorem ipsum dolor sit amet""#; | ||
|
||
serde_json::from_str::<GString>(json_str).unwrap(); | ||
} | ||
} |
Oops, something went wrong.