use ingraph_codegen::{prefix::EmissionPrefix, prelude::AliasOptions};
use quote::ToTokens as _;
use crate::{
attribute::{self, set_once},
errors::Errors,
value,
};
#[derive(Clone, Copy, PartialEq, Eq)]
pub(crate) enum Depth {
Sized,
Unsized(&'static str),
}
impl Depth {
const fn knobs(self) -> &'static str {
match self {
Self::Sized => "`prefix = \"…\"` and `depth = N`",
Self::Unsized(_) => "`prefix = \"…\"`",
}
}
const fn words(self) -> &'static [&'static str] {
match self {
Self::Sized => &["prefix", "depth"],
Self::Unsized(_) => &["prefix"],
}
}
}
pub(crate) fn block(
option: &syn::meta::ParseNestedMeta<'_>,
settled: &mut AliasOptions,
seen: &mut Option<syn::Path>,
scope: &str,
sizes: Depth,
errors: &mut Errors,
) -> syn::Result<()> {
set_once(
seen,
option.path.clone(),
&option.path,
&format!("`alias(…)` is declared more than once in {scope} — one block, one namespace"),
errors,
);
let mut prefix: Option<syn::LitStr> = None;
let mut depth: Option<syn::LitInt> = None;
let walked = attribute::options(option, |entry| {
if entry.path.is_ident("prefix") {
match value::literal(&entry, PREFIX_VALUE) {
Ok(written) => set_once(
&mut prefix,
written,
&entry.path,
&format!("`prefix` is declared more than once in `alias(…)` in {scope} — one namespace"),
errors,
),
Err(e) => errors.push(e),
}
return Ok(());
}
if entry.path.is_ident("depth") {
if let Depth::Unsized(subject) = sizes {
errors.push(attribute::unknown(
&entry,
&format!(
"`depth` is the LENGTH of an alias table, and {subject} owns no table to mint one \
for — what `alias(prefix = \"…\")` moves here are the words the statement INVENTS, \
which are one name each rather than one per nesting level"
),
&[],
));
return Ok(());
}
match value::integer(&entry, DEPTH_VALUE) {
Ok(written) => set_once(
&mut depth,
written,
&entry.path,
&format!("`depth` is declared more than once in `alias(…)` in {scope} — one length"),
errors,
),
Err(e) => errors.push(e),
}
return Ok(());
}
errors.push(attribute::unknown(
&entry,
&format!(
"unknown entry in `alias(…)` in {scope} — it takes {knobs} and nothing else",
knobs = sizes.knobs(),
),
sizes.words(),
));
Ok(())
});
*settled = settle(&prefix, depth.as_ref(), option, scope, sizes, errors);
walked
}
const PREFIX_VALUE: &str = "`prefix` must be the namespace every alias over this row is minted in, \
in a string literal";
const DEPTH_VALUE: &str =
"`depth` must be how many nesting depths this row's aliases are minted for, as a plain integer";
const TOO_DEEP: &str = "`depth` is past what any alias table could hold — the deepest entry's own \
digits have to fit inside the sixty-three bytes every shipped engine keeps \
of an identifier, beside the prefix and the table's own name";
fn settle(
prefix: &Option<syn::LitStr>,
depth: Option<&syn::LitInt>,
at: &syn::meta::ParseNestedMeta<'_>,
scope: &str,
sizes: Depth,
errors: &mut Errors,
) -> AliasOptions {
if prefix.is_none() && depth.is_none() {
errors.push(syn::Error::new_spanned(
at.path.to_token_stream(),
format!(
"`alias(…)` in {scope} names neither knob — it takes {knobs}, and a block that states \
nothing leaves both at the framework's own answers, which is what omitting it already says",
knobs = sizes.knobs(),
),
));
return AliasOptions::default();
}
let minted = depth.and_then(|written| match written.base10_parse::<usize>() {
Ok(depth) => Some(depth),
Err(_) => {
errors.push(syn::Error::new_spanned(written, TOO_DEEP));
None
}
});
let settled = AliasOptions {
prefix: prefix.as_ref().map(syn::LitStr::value),
depth: minted,
};
let Some(why) = ingraph_codegen::alias::refused(
settled.prefix_in(&EmissionPrefix::default()),
settled.depth(),
) else {
return settled;
};
match (prefix, depth) {
(Some(written), _) => errors.push(syn::Error::new_spanned(written, why)),
(None, Some(written)) => errors.push(syn::Error::new_spanned(written, why)),
(None, None) => errors.push(syn::Error::new_spanned(at.path.to_token_stream(), why)),
}
AliasOptions::default()
}
pub(crate) struct Fence {
prefixes: Vec<String>,
}
impl Default for Fence {
fn default() -> Self {
Self {
prefixes: vec![EmissionPrefix::default().mark().to_owned()],
}
}
}
impl Fence {
pub(crate) fn over<'a>(declared: impl IntoIterator<Item = Option<&'a str>>) -> Self {
let mut prefixes: Vec<String> = Vec::new();
let mut defaults = false;
for prefix in declared {
match prefix {
Some(prefix) if !prefixes.iter().any(|held| held == prefix) => {
prefixes.push(prefix.to_owned());
}
Some(_) => {}
None => defaults = true,
}
}
if defaults || prefixes.is_empty() {
prefixes.push(EmissionPrefix::default().mark().to_owned());
}
Self { prefixes }
}
pub(crate) fn caught(&self, name: &str) -> Option<&str> {
self
.prefixes
.iter()
.find(|prefix| name.starts_with(prefix.as_str()))
.map(String::as_str)
}
}