Skip to main content

zvariant/dbus/
mod.rs

1mod de;
2pub(crate) use de::*;
3mod ser;
4pub use ser::*;
5
6use crate::{Error, Result, Signature};
7
8/// Reject a signature that carries a GVariant maybe type before it reaches the D-Bus codec.
9///
10/// The maybe type has no D-Bus wire representation, so a signature containing one (whether given
11/// statically or read from a variant on the wire) is invalid input rather than an internal
12/// invariant. It becomes reachable when `zvariant_utils/gvariant` is enabled — including
13/// transitively via a co-located `zgvariant` — so this must return an error, not panic.
14pub(crate) fn reject_maybe(signature: &Signature) -> Result<()> {
15    if signature.contains_maybe() {
16        return Err(maybe_not_in_dbus());
17    }
18
19    Ok(())
20}
21
22/// Reject a maybe type in a signature carried as a `g` or `v` value's string form.
23///
24/// This is the hot path (every variant on the wire), so it avoids re-parsing the string into a
25/// `Signature`: `m` is the maybe type constructor and appears nowhere else in the signature
26/// grammar, so its presence is an exact test for a maybe type.
27pub(crate) fn reject_maybe_in_signature_str(bytes: &[u8]) -> Result<()> {
28    if bytes.contains(&b'm') {
29        return Err(maybe_not_in_dbus());
30    }
31
32    Ok(())
33}
34
35fn maybe_not_in_dbus() -> Error {
36    Error::Message(
37        "GVariant `Maybe` types are not valid in the D-Bus format; use the `zgvariant` crate \
38         for GVariant serialization"
39            .to_owned(),
40    )
41}