feat: custom option getter
Signed-off-by: Nikolaos Karaolidis <nick@karaolidis.com>
This commit is contained in:
62
crates/ptp/macro/Cargo.lock
generated
Normal file
62
crates/ptp/macro/Cargo.lock
generated
Normal file
@@ -0,0 +1,62 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "byteorder"
|
||||
version = "1.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.101"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "89ae43fd86e4158d6db51ad8e2b80f313af9cc74f5c0e03ccb87de09998732de"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ptp_cursor"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"byteorder",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ptp_macro"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"byteorder",
|
||||
"proc-macro2",
|
||||
"ptp_cursor",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.41"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ce25767e7b499d1b604768e7cde645d14cc8584231ea6b295e9c9eb22c02e1d1"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.106"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ede7c438028d4436d71104916910f5bb611972c5cfd7f89b8300a8186e6fada6"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f63a545481291138910575129486daeaf8ac54aee4387fe7906919f7830c7d9d"
|
14
crates/ptp/macro/Cargo.toml
Normal file
14
crates/ptp/macro/Cargo.toml
Normal file
@@ -0,0 +1,14 @@
|
||||
[package]
|
||||
name = "ptp_macro"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[lib]
|
||||
proc-macro = true
|
||||
|
||||
[dependencies]
|
||||
byteorder = "1.5.0"
|
||||
proc-macro2 = "1.0.101"
|
||||
quote = "1.0.41"
|
||||
syn = { version = "2.0.106", features = ["full"] }
|
||||
ptp_cursor = { path = "../cursor" }
|
253
crates/ptp/macro/src/lib.rs
Normal file
253
crates/ptp/macro/src/lib.rs
Normal file
@@ -0,0 +1,253 @@
|
||||
use proc_macro::TokenStream;
|
||||
use quote::quote;
|
||||
use syn::{Data, DeriveInput, Expr, Fields, parse_macro_input, punctuated::Punctuated};
|
||||
|
||||
#[proc_macro_derive(PtpSerialize)]
|
||||
pub fn derive_ptp_serialize(input: TokenStream) -> TokenStream {
|
||||
let input = parse_macro_input!(input as DeriveInput);
|
||||
let name = &input.ident;
|
||||
|
||||
let expanded = match &input.data {
|
||||
Data::Struct(s) => match &s.fields {
|
||||
Fields::Named(named) => {
|
||||
let fields = &named.named;
|
||||
|
||||
let write_fields = fields.iter().map(|f| {
|
||||
let name = &f.ident;
|
||||
quote! {
|
||||
self.#name.try_write_ptp(buf)?;
|
||||
}
|
||||
});
|
||||
|
||||
quote! {
|
||||
impl ptp_cursor::PtpSerialize for #name {
|
||||
fn try_into_ptp(&self) -> std::io::Result<Vec<u8>> {
|
||||
let mut buf = Vec::new();
|
||||
self.try_write_ptp(&mut buf)?;
|
||||
Ok(buf)
|
||||
}
|
||||
|
||||
fn try_write_ptp(&self, buf: &mut Vec<u8>) -> std::io::Result<()> {
|
||||
#(#write_fields)*
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Fields::Unnamed(unnamed) => {
|
||||
let fields = &unnamed.unnamed;
|
||||
|
||||
let write_fields = (0..fields.len()).map(|i| {
|
||||
let idx = syn::Index::from(i);
|
||||
quote! { self.#idx.try_write_ptp(buf)?; }
|
||||
});
|
||||
|
||||
quote! {
|
||||
impl ptp_cursor::PtpSerialize for #name {
|
||||
fn try_into_ptp(&self) -> std::io::Result<Vec<u8>> {
|
||||
let mut buf = Vec::new();
|
||||
self.try_write_ptp(&mut buf)?;
|
||||
Ok(buf)
|
||||
}
|
||||
|
||||
fn try_write_ptp(&self, buf: &mut Vec<u8>) -> std::io::Result<()> {
|
||||
#(#write_fields)*
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Fields::Unit => {
|
||||
quote! {
|
||||
impl ptp_cursor::PtpSerialize for #name {
|
||||
fn try_into_ptp(&self) -> std::io::Result<Vec<u8>> {
|
||||
Ok(Vec::new())
|
||||
}
|
||||
|
||||
fn try_write_ptp(&self, _buf: &mut Vec<u8>) -> std::io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
Data::Enum(_) => {
|
||||
let repr_ty = input
|
||||
.attrs
|
||||
.iter()
|
||||
.find_map(|attr| {
|
||||
if attr.path().is_ident("repr") {
|
||||
attr.parse_args_with(
|
||||
Punctuated::<Expr, syn::Token![,]>::parse_separated_nonempty,
|
||||
)
|
||||
.ok()
|
||||
.and_then(|args| args.into_iter().next())
|
||||
.and_then(|expr| match expr {
|
||||
Expr::Path(path) => path.path.get_ident().cloned(),
|
||||
_ => None,
|
||||
})
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.expect("Enums must have a #[repr(T)] attribute for PtpSerialize");
|
||||
|
||||
quote! {
|
||||
impl ptp_cursor::PtpSerialize for #name
|
||||
where
|
||||
#name: Clone + Copy + TryFrom<#repr_ty> + Into<#repr_ty>
|
||||
{
|
||||
fn try_into_ptp(&self) -> std::io::Result<Vec<u8>> {
|
||||
let mut buf = Vec::new();
|
||||
self.try_write_ptp(&mut buf)?;
|
||||
Ok(buf)
|
||||
}
|
||||
|
||||
fn try_write_ptp(&self, buf: &mut Vec<u8>) -> std::io::Result<()> {
|
||||
let discriminant: #repr_ty = (*self).into();
|
||||
discriminant.try_write_ptp(buf)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
unimplemented!("PtpSerialize cannot be automatically derived for unions")
|
||||
}
|
||||
};
|
||||
|
||||
expanded.into()
|
||||
}
|
||||
|
||||
#[proc_macro_derive(PtpDeserialize)]
|
||||
pub fn derive_ptp_deserialize(input: TokenStream) -> TokenStream {
|
||||
let input = parse_macro_input!(input as DeriveInput);
|
||||
let name = &input.ident;
|
||||
|
||||
let expanded = match &input.data {
|
||||
Data::Struct(s) => match &s.fields {
|
||||
Fields::Named(named) => {
|
||||
let fields = &named.named;
|
||||
|
||||
let read_fields = fields.iter().map(|f| {
|
||||
let name = &f.ident;
|
||||
let ty = &f.ty;
|
||||
quote! {
|
||||
#name: <#ty>::try_read_ptp(cur)?
|
||||
}
|
||||
});
|
||||
|
||||
quote! {
|
||||
impl ptp_cursor::PtpDeserialize for #name {
|
||||
fn try_from_ptp(buf: &[u8]) -> std::io::Result<Self> {
|
||||
use ptp_cursor::Read;
|
||||
|
||||
let mut cur = std::io::Cursor::new(buf);
|
||||
let val = Self::try_read_ptp(&mut cur)?;
|
||||
cur.expect_end()?;
|
||||
Ok(val)
|
||||
}
|
||||
|
||||
fn try_read_ptp<R: ptp_cursor::Read>(cur: &mut R) -> std::io::Result<Self> {
|
||||
Ok(Self { #(#read_fields),* })
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Fields::Unnamed(unnamed) => {
|
||||
let fields = &unnamed.unnamed;
|
||||
|
||||
let read_fields = fields.iter().map(|f| {
|
||||
let ty = &f.ty;
|
||||
quote! { <#ty>::try_read_ptp(cur)? }
|
||||
});
|
||||
|
||||
quote! {
|
||||
impl ptp_cursor::PtpDeserialize for #name {
|
||||
fn try_from_ptp(buf: &[u8]) -> std::io::Result<Self> {
|
||||
use ptp_cursor::Read;
|
||||
|
||||
let mut cur = std::io::Cursor::new(buf);
|
||||
let val = Self::try_read_ptp(&mut cur)?;
|
||||
cur.expect_end()?;
|
||||
Ok(val)
|
||||
}
|
||||
|
||||
fn try_read_ptp<R: ptp_cursor::Read>(cur: &mut R) -> std::io::Result<Self> {
|
||||
Ok(Self(#(#read_fields),*))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Fields::Unit => {
|
||||
quote! {
|
||||
impl ptp_cursor::PtpDeserialize for #name {
|
||||
fn try_from_ptp(buf: &[u8]) -> std::io::Result<Self> {
|
||||
use ptp_cursor::Read;
|
||||
|
||||
let mut cur = std::io::Cursor::new(buf);
|
||||
cur.expect_end()?;
|
||||
Ok(Self)
|
||||
}
|
||||
|
||||
fn try_read_ptp<R: ptp_cursor::Read>(_cur: &mut R) -> std::io::Result<Self> {
|
||||
Ok(Self)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
Data::Enum(_) => {
|
||||
let repr_ty = input
|
||||
.attrs
|
||||
.iter()
|
||||
.find_map(|attr| {
|
||||
if attr.path().is_ident("repr") {
|
||||
attr.parse_args_with(
|
||||
Punctuated::<Expr, syn::Token![,]>::parse_separated_nonempty,
|
||||
)
|
||||
.ok()
|
||||
.and_then(|args| args.into_iter().next())
|
||||
.and_then(|expr| match expr {
|
||||
Expr::Path(path) => path.path.get_ident().cloned(),
|
||||
_ => None,
|
||||
})
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.expect("Enums must have a #[repr(T)] attribute for PtpDeserialize");
|
||||
|
||||
quote! {
|
||||
impl ptp_cursor::PtpDeserialize for #name
|
||||
where
|
||||
#name: Clone + Copy + TryFrom<#repr_ty> + Into<#repr_ty>
|
||||
{
|
||||
fn try_from_ptp(buf: &[u8]) -> std::io::Result<Self> {
|
||||
use ptp_cursor::Read;
|
||||
|
||||
let mut cur = std::io::Cursor::new(buf);
|
||||
let val = Self::try_read_ptp(&mut cur)?;
|
||||
cur.expect_end()?;
|
||||
Ok(val)
|
||||
}
|
||||
|
||||
fn try_read_ptp<R: ptp_cursor::Read>(cur: &mut R) -> std::io::Result<Self> {
|
||||
let discriminant = <#repr_ty>::try_read_ptp(cur)?;
|
||||
discriminant.try_into().map_err(|_| {
|
||||
std::io::Error::new(
|
||||
std::io::ErrorKind::InvalidData,
|
||||
format!("Invalid discriminant for {}: {:?}", stringify!(#name), discriminant)
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
unimplemented!("PtpDeserialize cannot be automatically derived for unions")
|
||||
}
|
||||
};
|
||||
|
||||
expanded.into()
|
||||
}
|
Reference in New Issue
Block a user