use crate::Signature;
#[derive(Debug, Clone, PartialEq)]
pub enum TypeDef {
SimpleType(SimpleType),
Enum(Enum),
Struct(Struct),
Mapping(Mapping),
}
impl TypeDef {
pub fn name(&self) -> &str {
match self {
TypeDef::SimpleType(t) => t.name(),
TypeDef::Enum(e) => e.name(),
TypeDef::Struct(s) => s.name(),
TypeDef::Mapping(m) => m.name(),
}
}
pub fn docstring(&self) -> Option<&str> {
match self {
TypeDef::SimpleType(t) => t.docstring(),
TypeDef::Enum(e) => e.docstring(),
TypeDef::Struct(s) => s.docstring(),
TypeDef::Mapping(m) => m.docstring(),
}
}
pub fn signature(&self) -> zvariant::Signature {
match self {
TypeDef::SimpleType(t) => t.ty().inner().clone(),
TypeDef::Enum(e) => e.ty().inner().clone(),
TypeDef::Struct(s) => s.signature(),
TypeDef::Mapping(m) => m.signature(),
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct SimpleType {
pub(crate) name: String,
pub(crate) ty: Signature,
pub(crate) docstring: Option<String>,
}
impl SimpleType {
pub fn name(&self) -> &str {
&self.name
}
pub fn ty(&self) -> &Signature {
&self.ty
}
pub fn docstring(&self) -> Option<&str> {
self.docstring.as_deref()
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Enum {
pub(crate) name: String,
pub(crate) ty: Signature,
pub(crate) values: Vec<EnumValue>,
pub(crate) docstring: Option<String>,
}
impl Enum {
pub fn name(&self) -> &str {
&self.name
}
pub fn ty(&self) -> &Signature {
&self.ty
}
pub fn values(&self) -> &[EnumValue] {
&self.values
}
pub fn docstring(&self) -> Option<&str> {
self.docstring.as_deref()
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct EnumValue {
pub(crate) suffix: String,
pub(crate) value: String,
pub(crate) docstring: Option<String>,
}
impl EnumValue {
pub fn suffix(&self) -> &str {
&self.suffix
}
pub fn value(&self) -> &str {
&self.value
}
pub fn docstring(&self) -> Option<&str> {
self.docstring.as_deref()
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Struct {
pub(crate) name: String,
pub(crate) members: Vec<Member>,
pub(crate) docstring: Option<String>,
}
impl Struct {
pub fn name(&self) -> &str {
&self.name
}
pub fn members(&self) -> &[Member] {
&self.members
}
pub fn docstring(&self) -> Option<&str> {
self.docstring.as_deref()
}
pub fn signature(&self) -> zvariant::Signature {
zvariant::Signature::structure(
self.members
.iter()
.map(|m| m.ty().inner().clone())
.collect::<Vec<_>>(),
)
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Member {
pub(crate) name: String,
pub(crate) ty: Signature,
pub(crate) tp_type: Option<String>,
pub(crate) docstring: Option<String>,
}
impl Member {
pub fn name(&self) -> &str {
&self.name
}
pub fn ty(&self) -> &Signature {
&self.ty
}
pub fn tp_type(&self) -> Option<&str> {
self.tp_type.as_deref()
}
pub fn docstring(&self) -> Option<&str> {
self.docstring.as_deref()
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Mapping {
pub(crate) name: String,
pub(crate) key: Member,
pub(crate) value: Member,
pub(crate) docstring: Option<String>,
}
impl Mapping {
pub fn name(&self) -> &str {
&self.name
}
pub fn key(&self) -> &Member {
&self.key
}
pub fn value(&self) -> &Member {
&self.value
}
pub fn docstring(&self) -> Option<&str> {
self.docstring.as_deref()
}
pub fn signature(&self) -> zvariant::Signature {
zvariant::Signature::dict(
self.key.ty().inner().clone(),
self.value.ty().inner().clone(),
)
}
}
pub(crate) fn is_basic(ty: &Signature) -> bool {
use zvariant::Signature as S;
match ty.inner() {
S::U8
| S::Bool
| S::I16
| S::U16
| S::I32
| S::U32
| S::I64
| S::U64
| S::F64
| S::Str
| S::Signature
| S::ObjectPath => true,
#[cfg(unix)]
S::Fd => true,
_ => false,
}
}