Skip to content

Commit

Permalink
Exposes the formatting macros.
Browse files Browse the repository at this point in the history
This allows users to create their own encoding wrappers.

The provided documentation points the user to existing uses within
yew, but does not provide any complete additional example, lest the
example itself becomes a burden to maintain.
  • Loading branch information
ctm committed Jan 19, 2020
1 parent 1abef04 commit f7fa139
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion src/format/macros.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
//! Contains macro for wrapping serde format.
//! Contains three macros for wrapping serde format. Collectively they
//! allow you to define your own text and binary wrappers.
#[macro_export]
/// This macro is used for a format that can be encoded as Text. It
/// is used in conjunction with a type definition for a tuple struct
/// with one (publically accessible) element of a generic type. Since
/// any type that can be encoded as Text can also be encoded as Binary,
/// it should be used with the binary_format macro. See json.rs as
/// an example.
macro_rules! text_format {
($type:ident based on $format:ident) => {
impl<'a, T> Into<$crate::format::Text> for $type<&'a T>
Expand All @@ -25,6 +33,27 @@ macro_rules! text_format {
};
}

#[macro_export]
/// This macro is used for a format that can be encoded as Binary. It
/// is used in conjunction with a type definition for a tuple struct
/// with one (publicly accessible) element of a generic type. Not
/// all types that can be encoded as Binary can be encoded as Text.
/// As such, this macro should be paired with the text_format macro
/// where such an encoding works (e.g., JSON), or with the
/// text_format_is_an_error macro for binary-only formats (e.g.,
/// CBOR). See json.rs as an example of an encoding that supports
/// both Text and Binary and cbor.js as an example of a Binary-only
/// encoding.
///
/// In addition to the "based on" form of this macro used in json.rs
/// and cbor.rs, you can use the three parameter second form to supply
/// your own serialization and deserialization helpers as the second
/// and third parameters, e.g.,
/// ```ignore
/// binary_format!(MyEncoding, self::to_vec, self::from_slice)
/// ```
/// Which creates the encoding `MyEncoding` using the hypothetical
/// `to_vec` and `from_slice` methods you've written in the same module.
macro_rules! binary_format {
($type:ident based on $format:ident) => {
binary_format!($type, $format::to_vec, $format::from_slice);
Expand Down Expand Up @@ -53,6 +82,13 @@ macro_rules! binary_format {
};
}

#[macro_export]
/// This macro is used for a format that can be encoded as Binary but
/// can't be encoded as Text. It is used in conjunction with a type
/// definition for a tuple struct with one (publically accessible)
/// element of a generic type. This macro should be paired with the
/// binary_format macro that defines the binary-only format (e.g.,
/// CBOR). See cbor.rs as an example.
macro_rules! text_format_is_an_error {
($type:ident) => {
use $crate::format::FormatError;
Expand Down

0 comments on commit f7fa139

Please sign in to comment.