use proc_macro2::TokenStream;
use quote::format_ident;
use quote::quote;
use syn::Error;
use syn::Ident;
use syn::LitStr;
use syn::Path;
use syn::Result;
#[derive(Clone, Copy)]
#[must_use]
pub(crate) struct Sensitivity {
runtime_variant: &'static str,
}
impl Sensitivity {
pub(crate) fn parse(literal: &LitStr, type_name: &Ident, field_name: &str) -> Result<Self> {
match literal.value().as_str() {
"low" => Ok(Self { runtime_variant: "Low" }),
"medium" => Ok(Self {
runtime_variant: "Medium",
}),
"high" => Ok(Self {
runtime_variant: "High",
}),
"secret" => Ok(Self {
runtime_variant: "Secret",
}),
value => Err(Error::new_spanned(
literal,
format!(
"Redact derive for `{type_name}` field `{field_name}` has unknown level \
`{value}`; use one of `low`, `medium`, `high`, or `secret`",
),
)),
}
}
#[must_use]
#[inline]
pub(crate) fn runtime_tokens(&self, runtime: &Path) -> TokenStream {
let variant = format_ident!("{}", self.runtime_variant);
quote!(#runtime::Sensitivity::#variant)
}
}