use serde::ser::{Serialize, SerializeTupleStruct, Serializer};
use crate::{Signature, Type, Value};
#[derive(Debug, Default, Clone, PartialEq)]
pub struct StructureBuilder<'a>(Vec<Value<'a>>);
impl<'a> StructureBuilder<'a> {
pub fn new() -> Self {
Self::default()
}
pub fn add_field<T>(self, field: T) -> Self
where
T: Type + Into<Value<'a>>,
{
self.append_field(Value::new(field))
}
pub fn append_field<'e: 'a>(mut self, field: Value<'e>) -> Self {
self.0.push(field);
self
}
pub fn build(self) -> Structure<'a> {
let signature = create_signature_from_fields(&self.0);
Structure {
fields: self.0,
signature,
}
}
pub(crate) fn build_with_signature<'s: 'a>(self, signature: Signature<'s>) -> Structure<'a> {
Structure {
fields: self.0,
signature,
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Structure<'a> {
fields: Vec<Value<'a>>,
signature: Signature<'a>,
}
impl<'a> Structure<'a> {
pub fn fields(&self) -> &[Value<'a>] {
&self.fields
}
#[deprecated(
since = "2.3.0",
note = "Please use `StructureBuilder` to create a `Structure` instead."
)]
pub fn new() -> Self {
Self::default()
}
#[deprecated(
since = "2.3.0",
note = "Please use `StructureBuilder` to create a `Structure` instead."
)]
pub fn add_field<T>(self, field: T) -> Self
where
T: Type + Into<Value<'a>>,
{
#[allow(deprecated)]
self.append_field(Value::new(field))
}
#[deprecated(
since = "2.3.0",
note = "Please use `StructureBuilder` to create a `Structure` instead."
)]
pub fn append_field<'e: 'a>(mut self, field: Value<'e>) -> Self {
self.fields.push(field);
self.signature = create_signature_from_fields(&self.fields);
self
}
pub fn signature(&self) -> Signature<'static> {
self.signature.to_owned()
}
pub fn full_signature(&self) -> &Signature {
&self.signature
}
pub(crate) fn to_owned(&self) -> Structure<'static> {
Structure {
fields: self.fields.iter().map(|v| v.to_owned()).collect(),
signature: self.signature.to_owned(),
}
}
}
impl<'a> Default for Structure<'a> {
fn default() -> Self {
let signature = Signature::from_str_unchecked("()");
Self {
fields: vec![],
signature,
}
}
}
impl<'a> Serialize for Structure<'a> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut structure =
serializer.serialize_tuple_struct("zvariant::Structure", self.fields.len())?;
for field in &self.fields {
field.serialize_value_as_tuple_struct_field(&mut structure)?;
}
structure.end()
}
}
macro_rules! tuple_impls {
($($len:expr => ($($n:tt $name:ident)+))+) => {
$(
impl<'a, $($name),+> From<($($name),+,)> for Structure<'a>
where
$($name: Type + Into<Value<'a>>,)+
{
#[inline]
fn from(value: ($($name),+,)) -> Self {
StructureBuilder::new()
$(
.add_field(value. $n)
)+
.build()
}
}
impl<'a, $($name),+> std::convert::TryFrom<Structure<'a>> for ($($name),+,)
where
$($name: std::convert::TryFrom<Value<'a>>,)+
{
type Error = crate::Error;
fn try_from(mut s: Structure<'a>) -> core::result::Result<Self, Self::Error> {
Ok((
$(
s.fields.remove(0).downcast::<$name>().ok_or(crate::Error::IncorrectType)?,
)+
))
}
}
)+
}
}
tuple_impls! {
1 => (0 T0)
2 => (0 T0 1 T1)
3 => (0 T0 1 T1 2 T2)
4 => (0 T0 1 T1 2 T2 3 T3)
5 => (0 T0 1 T1 2 T2 3 T3 4 T4)
6 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5)
7 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6)
8 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7)
9 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8)
10 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9)
11 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10)
12 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11)
13 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12)
14 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13)
15 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14)
16 => (0 T0 1 T1 2 T2 3 T3 4 T4 5 T5 6 T6 7 T7 8 T8 9 T9 10 T10 11 T11 12 T12 13 T13 14 T14 15 T15)
}
fn create_signature_from_fields(fields: &[Value<'_>]) -> Signature<'static> {
let mut signature = String::with_capacity(255);
signature.push('(');
for field in fields {
signature.push_str(&field.value_signature());
}
signature.push(')');
Signature::from_string_unchecked(signature)
}