diff options
author | metamuffin <metamuffin@disroot.org> | 2023-01-14 18:04:16 +0100 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2023-01-14 18:04:16 +0100 |
commit | f452df18749b13f9d83a6ea679361d195b4a9ae1 (patch) | |
tree | 04897eef044ebed319949a0cdbd04232f0dce98c /ebml_derive/src/lib.rs | |
parent | 6c023ddeaa0894813fc74038af7568c2d867c052 (diff) | |
download | jellything-f452df18749b13f9d83a6ea679361d195b4a9ae1.tar jellything-f452df18749b13f9d83a6ea679361d195b4a9ae1.tar.bz2 jellything-f452df18749b13f9d83a6ea679361d195b4a9ae1.tar.zst |
seeking and broken writing
Diffstat (limited to 'ebml_derive/src/lib.rs')
-rw-r--r-- | ebml_derive/src/lib.rs | 57 |
1 files changed, 50 insertions, 7 deletions
diff --git a/ebml_derive/src/lib.rs b/ebml_derive/src/lib.rs index fc8af7d..056071d 100644 --- a/ebml_derive/src/lib.rs +++ b/ebml_derive/src/lib.rs @@ -43,8 +43,7 @@ pub fn define_ebml(ts: TokenStream) -> TokenStream { .iter() .map(|e| { let name = &e.name; - let mut path = e.path.clone(); - path.reverse(); + let path = e.path.clone(); if e.global { quote! { Self::#name(_) => None } } else { @@ -52,14 +51,48 @@ pub fn define_ebml(ts: TokenStream) -> TokenStream { } }) .collect::<Vec<_>>(); + let id_match = tags + .iter() + .map(|Tag { id, name, .. }| { + quote! { Self::#name(_) => #id } + }) + .collect::<Vec<_>>(); let parse_match = tags .iter() - .map(|Tag { id, name, .. }| { - quote! { #id => Self::#name(crate::ValueFromBuf::from_buf(data)?) } + .filter_map( + |Tag { + id, name, r#type, .. + }| { + if let Some(_) = r#type { + Some(quote! { #id => Self::#name(crate::ReadValue::from_buf(data)?) }) + } else { + None + } + }, + ) + .collect::<Vec<_>>(); + let write_match = tags + .iter() + .filter_map(|Tag { name, .. }| { + Some(quote! { Self::#name(v) => v.write_to(w) }) }) .collect::<Vec<_>>(); - let master_match = tags + let cons_master_match = tags + .iter() + .filter_map( + |Tag { + id, name, r#type, .. + }| { + if let None = r#type { + Some(quote! { #id => Self::#name(kind) }) + } else { + None + } + }, + ) + .collect::<Vec<_>>(); + let is_master_match = tags .iter() .map(|Tag { id, r#type, .. }| match r#type { None => quote!(#id => true), @@ -69,8 +102,9 @@ pub fn define_ebml(ts: TokenStream) -> TokenStream { quote! { use crate::Master; + use crate::WriteValue; - #[derive(Debug)] + #[derive(Debug, PartialEq, Clone)] pub enum MatroskaTag { #(#enum_variants),* } @@ -79,12 +113,21 @@ pub fn define_ebml(ts: TokenStream) -> TokenStream { pub fn path(&self) -> Option<&'static [u64]> { match self { #(#path_match),* } } + pub fn id(&self) -> u64 { + match self { #(#id_match),* } + } pub fn is_master(id: u64) -> anyhow::Result<bool> { - Ok(match id { #(#master_match),*, _ => anyhow::bail!("unknown id") }) + Ok(match id { #(#is_master_match),*, _ => anyhow::bail!("unknown id") }) + } + pub fn construct_master(id: u64, kind: Master) -> anyhow::Result<Self> { + Ok(match id { #(#cons_master_match),*, _ => anyhow::bail!("unknown id") }) } pub fn parse(id: u64, data: &[u8]) -> anyhow::Result<Self> { Ok(match id { #(#parse_match),*, _ => anyhow::bail!("unknown id or master") }) } + pub fn write(&self, w: &mut Vec<u8>) -> anyhow::Result<()> { + match self { #(#write_match),* } + } } } .into() |