use crate::args::{CodecArg, parse_codec};
use embark_format::{CodecId, CryptoId};
use proc_macro2::Span;
use syn::parse::{Parse, ParseStream};
use syn::{LitStr, Token};
pub(crate) enum CipherArg {
ChaCha,
Aes,
}
pub(crate) struct CryptArgs {
pub path: LitStr,
pub codec: Option<CodecArg>,
pub cipher: Option<CipherArg>,
pub runtime_key: Option<Span>,
}
impl CryptArgs {
pub(crate) fn codec(&self) -> CodecArg {
self.codec.unwrap_or(CodecArg::Fixed(CodecId::Deflate))
}
pub(crate) fn crypto_id(&self) -> CryptoId {
match self.cipher {
None | Some(CipherArg::ChaCha) => CryptoId::ChaCha20Poly1305,
Some(CipherArg::Aes) => CryptoId::Aes256Gcm,
}
}
}
impl Parse for CryptArgs {
fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
let path: LitStr = input.parse()?;
let mut codec = None;
let mut cipher = None;
let mut runtime_key = None;
while input.peek(Token![,]) {
let _: Token![,] = input.parse()?;
let key: syn::Ident = input.parse()?;
let _: Token![=] = input.parse()?;
if key == "codec" {
let val: syn::Ident = input.parse()?;
let name = val.to_string();
codec = Some(parse_codec(&name).ok_or_else(|| {
syn::Error::new(val.span(), format!("unknown codec `{name}`"))
})?);
} else if key == "cipher" {
let val: syn::Ident = input.parse()?;
cipher = Some(match val.to_string().as_str() {
"chacha" => CipherArg::ChaCha,
"aes" => {
if !cfg!(feature = "aes") {
return Err(syn::Error::new(
val.span(),
"cipher = aes requires the `aes` feature enabled on `embark`",
));
}
CipherArg::Aes
}
other => {
return Err(syn::Error::new(
val.span(),
format!("unknown cipher `{other}`"),
));
}
});
} else if key == "key" {
let val: syn::Ident = input.parse()?;
if val != "runtime" {
return Err(syn::Error::new(val.span(), "expected `runtime`"));
}
runtime_key = Some(val.span());
} else {
return Err(syn::Error::new(
key.span(),
"expected `codec`, `cipher`, or `key`",
));
}
}
Ok(CryptArgs {
path,
codec,
cipher,
runtime_key,
})
}
}