Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement Signature and Marshal for params::Variant #90

Merged
merged 1 commit into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion rustbus/src/params/types.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::signature;
use crate::{signature, wire::marshal::traits::SignatureBuffer, Marshal, Signature, Unmarshal};

/// The Types a message can have as parameters
/// There are From<T> impls for most of the Base ones
Expand Down Expand Up @@ -213,3 +213,42 @@ impl<'a, 'e> Container<'a, 'e> {
signature::Type::Container(sig)
}
}

impl Signature for Variant<'_, '_> {
fn signature() -> signature::Type {
signature::Type::Container(signature::Container::Variant)
}
fn alignment() -> usize {
Variant::signature().get_alignment()
}
#[inline]
fn sig_str(s_buf: &mut SignatureBuffer) {
s_buf.push_static("v");
}
fn has_sig(sig: &str) -> bool {
sig.starts_with('v')
}
}
impl Marshal for Variant<'_, '_> {
fn marshal(
&self,
ctx: &mut crate::wire::marshal::MarshalContext,
) -> Result<(), crate::wire::errors::MarshalError> {
let mut sig = String::new();
self.sig.to_str(&mut sig);
if sig.len() > 255 {
let sig_err = crate::signature::Error::SignatureTooLong;
return Err(sig_err.into());
}
debug_assert!(crate::params::validation::validate_signature(&sig).is_ok());
crate::wire::util::write_signature(&sig, ctx.buf);
crate::wire::marshal::container::marshal_param(&self.value, ctx)
}
}
impl<'buf, 'fds> Unmarshal<'buf, 'fds> for Variant<'buf, 'fds> {
fn unmarshal(
ctx: &mut crate::wire::unmarshal::UnmarshalContext<'fds, 'buf>,
) -> crate::wire::unmarshal::UnmarshalResult<Self> {
crate::wire::unmarshal::container::unmarshal_variant(ctx)
}
}
12 changes: 12 additions & 0 deletions rustbus/src/tests/verify_marshalling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,4 +296,16 @@ fn verify_variant_marshalling() {
// signature ++ padding ++ 32u8
assert_eq!(ctx.buf, &[1, b'y', 0, 32]);
ctx.buf.clear();

let param = crate::params::Param::Base(crate::params::Base::Byte(16));
let v = crate::params::Variant {
sig: crate::signature::Type::Base(crate::signature::Base::Byte),
value: param,
};

v.marshal(ctx).unwrap();

// signature ++ padding ++ 16u8
assert_eq!(ctx.buf, &[1, b'y', 0, 16]);
ctx.buf.clear();
}
Loading