Skip to content

Commit

Permalink
Make tag(write_value) optional.
Browse files Browse the repository at this point in the history
  • Loading branch information
wojciech-graj committed Oct 22, 2024
1 parent 1993aa4 commit 76ec665
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 13 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# v0.5.1
- Make `write_value` field in `#[protocol(tag(...))]` only required when deriving `ProtocolWrite`
# v0.5.0
- Split `Protocol` into `ProtocolRead` and `ProtocolWrite`
- Split `ExternallyLengthPrefixed` into `TaggedRead` and `UntaggedWrite`
Expand Down
14 changes: 5 additions & 9 deletions bin-proto-derive/src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub enum Tag {
External(syn::Expr),
Prepend {
typ: syn::Type,
write_value: syn::Expr,
write_value: Option<syn::Expr>,
},
}

Expand Down Expand Up @@ -241,14 +241,10 @@ impl TryFrom<&[syn::Attribute]> for Attrs {
}
}
}
match (typ, write_value) {
(Some(typ), Some(value)) => {
attribs.tag = Some(Tag::Prepend {
typ,
write_value: value,
});
}
_ => return Err(Error::new(list.span(), "Tag lacks type or value.")),
if let Some(typ) = typ {
attribs.tag = Some(Tag::Prepend { typ, write_value });
} else {
return Err(Error::new(list.span(), "Tag lacks type."));
}
}
_ => return Err(Error::new(meta_list.span(), "unrecognised attribute")),
Expand Down
11 changes: 9 additions & 2 deletions bin-proto-derive/src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pub mod trait_impl;

use crate::attr::{Attrs, Tag};
use proc_macro2::TokenStream;
use syn::spanned::Spanned;
use syn::{spanned::Spanned, Error};

pub fn reads(fields: &syn::Fields, attrs: &Attrs) -> (TokenStream, TokenStream) {
match *fields {
Expand Down Expand Up @@ -129,13 +129,20 @@ fn write(field: &syn::Field, field_name: &TokenStream) -> TokenStream {
),
Tag::Prepend {
typ,
write_value: value,
write_value: Some(value),
} => quote!(
{
<#typ as ::bin_proto::ProtocolWrite<_>>::write(&{#value}, __io_writer, __byte_order, __ctx)?;
::bin_proto::UntaggedWrite::write(#field_ref, __io_writer, __byte_order, __ctx)?
}
),
Tag::Prepend {
typ: _,
write_value: None,
} => {
return Error::new(field.span(), "Tag must specify 'write_value'")
.to_compile_error();
}
}
} else {
quote!(
Expand Down
5 changes: 3 additions & 2 deletions bin-proto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,16 @@ pub use self::tagged::{TaggedRead, UntaggedWrite};
/// }
/// ```
///
/// ## `#[protocol(tag(type = "<type>", write_value = "<expr>"))]`
/// ## `#[protocol(tag(type = "<type>"[, write_value = "<expr>"]?))]`
/// - Applies to: `impl TaggedRead` or `impl UntaggedWrite`
/// - `<type>`: tag's type
/// - `<expr>`: arbitrary expression. Fields in parent container should be
/// prefixed with `self`.
///
/// Specify tag of field. The tag represents a length prefix for variable-length
/// fields, and a boolean for `Option`. The tag is placed directly before the
/// field.
/// field. The `write_value` only has to be specified when deriving
/// `ProtocolWrite`.
///
/// ```
/// # use bin_proto::{ProtocolRead, ProtocolWrite};
Expand Down
6 changes: 6 additions & 0 deletions bin-proto/tests/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ pub struct WithElementsLength {
pub data: Vec<u32>,
}

#[derive(ProtocolRead, Debug, PartialEq, Eq)]
pub struct OptionalWriteValue {
#[protocol(tag(type = "u8"))]
pub data: Vec<u32>,
}

#[derive(ProtocolRead, ProtocolWrite, Debug, PartialEq, Eq)]
pub struct WithElementsLengthAuto {
#[protocol(write_value = "self.data.len() as u32")]
Expand Down

0 comments on commit 76ec665

Please sign in to comment.