Skip to content

Commit

Permalink
Merge pull request #1113 from Fi3/AddSupportForExtensionTypes
Browse files Browse the repository at this point in the history
Add support for data types defined by sv2 extensions
  • Loading branch information
Fi3 authored Aug 19, 2024
2 parents 69d9077 + 8e50099 commit 98be997
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 31 deletions.
6 changes: 3 additions & 3 deletions protocols/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion protocols/v2/binary-sv2/binary-sv2/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "binary_sv2"
version = "1.0.1"
version = "1.1.0"
authors = ["fi3 <[email protected]>"]
edition = "2018"
description = "Sv2 data format"
Expand Down
2 changes: 1 addition & 1 deletion protocols/v2/binary-sv2/no-serde-sv2/codec/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "binary_codec_sv2"
version = "1.0.0"
version = "1.1.0"
authors = ["fi3 <[email protected]>"]
edition = "2018"
description = "Sv2 data format"
Expand Down
8 changes: 4 additions & 4 deletions protocols/v2/binary-sv2/no-serde-sv2/codec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ use std::io::{Error as E, ErrorKind};
mod codec;
mod datatypes;
pub use datatypes::{
PubKey, Seq0255, Seq064K, ShortTxId, Signature, Str0255, Sv2Option, U32AsRef, B016M, B0255,
B032, B064K, U24, U256,
PubKey, Seq0255, Seq064K, ShortTxId, Signature, Str0255, Sv2DataType, Sv2Option, U32AsRef,
B016M, B0255, B032, B064K, U24, U256,
};

pub use crate::codec::{
decodable::Decodable,
decodable::{Decodable, GetMarker},
encodable::{Encodable, EncodableField},
GetSize, SizeHint,
Fixed, GetSize, SizeHint,
};

#[allow(clippy::wrong_self_convention)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "derive_codec_sv2"
version = "1.0.0"
version = "1.1.0"
authors = ["fi3 <[email protected]>"]
edition = "2018"
description = "Derive macro for Sv2 binary format serializer and deserializer"
Expand Down
59 changes: 40 additions & 19 deletions protocols/v2/binary-sv2/no-serde-sv2/derive_codec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@ extern crate proc_macro;
use core::iter::FromIterator;
use proc_macro::{Group, TokenStream, TokenTree};

fn is_already_sized(item: TokenStream) -> bool {
let stream = item.into_iter();

for next in stream {
if let TokenTree::Group(g) = next.clone() {
if g.delimiter() == proc_macro::Delimiter::Bracket {
for t in g.stream().into_iter() {
if let TokenTree::Ident(i) = t {
if i.to_string() == "already_sized" {
return true;
}
}
}
}
}
}
false
}
fn remove_attributes(item: TokenStream) -> TokenStream {
let stream = item.into_iter();
let mut is_attribute = false;
Expand Down Expand Up @@ -356,8 +374,9 @@ fn get_static_generics(gen: &str) -> &str {
}
}

#[proc_macro_derive(Encodable)]
#[proc_macro_derive(Encodable, attributes(already_sized))]
pub fn encodable(item: TokenStream) -> TokenStream {
let is_already_sized = is_already_sized(item.clone());
let parsed_struct = get_struct_properties(item);
let fields = parsed_struct.fields.clone();

Expand Down Expand Up @@ -392,6 +411,23 @@ pub fn encodable(item: TokenStream) -> TokenStream {
"<'decoder>".to_string()
};

let get_size = if is_already_sized {
String::new()
} else {
format!(
"
impl{} GetSize for {}{} {{
fn get_size(&self) -> usize {{
let mut size = 0;
{}
size
}}
}}
",
impl_generics, parsed_struct.name, parsed_struct.generics, sizes
)
};

let result = format!(
"mod impl_parse_encodable_{} {{
Expand All @@ -408,14 +444,7 @@ pub fn encodable(item: TokenStream) -> TokenStream {
}}
}}
impl{} GetSize for {}{} {{
fn get_size(&self) -> usize {{
let mut size = 0;
{}
size
}}
}}
{}
}}",
// imports
Expand All @@ -428,16 +457,8 @@ pub fn encodable(item: TokenStream) -> TokenStream {
parsed_struct.name,
parsed_struct.generics,
field_into_decoded_field,
// impl Encodable for Struct
//impl{} Encodable<'decoder> for {}{} {{}}
//impl_generics,
//parsed_struct.name,
//parsed_struct.generics,
// impl GetSize for Struct
impl_generics,
parsed_struct.name,
parsed_struct.generics,
sizes,
// impl get_size
get_size,
);
//println!("{}", result);

Expand Down
4 changes: 2 additions & 2 deletions roles/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 98be997

Please sign in to comment.