use std::collections::HashMap;
use std::sync::Arc;
use mlua::Lua;
use nitr_core::{Error, Result};
use super::AppDef;
use super::compile::route_site;
use super::options::site_label;
use crate::openapi::doc::{ApiDoc, RouteDoc};
use crate::openapi::{AppMeta, RouteMeta, default_operation_id, to_openapi_path};
use crate::validation::{InputEnv, InputSchemas};
pub(super) fn collect(
lua: &Lua,
def: &AppDef,
inputs: &[Option<Arc<InputSchemas>>],
env: &InputEnv,
) -> Result<AppMeta> {
let api = match &def.api_doc {
Some((table, site)) => Some(
ApiDoc::parse(table, &format!("app:doc ({})", site_label(site)))
.map_err(|err| Error::Script(err.to_string()))?,
),
None => None,
};
let mut routes = Vec::with_capacity(def.routes.len());
let mut ids: HashMap<String, (usize, bool)> = HashMap::new();
for (i, route) in def.routes.iter().enumerate() {
let site = route_site(route);
for (path, key) in &env.reserved {
if &route.path == path || route.path.starts_with(&format!("{path}/")) {
return Err(Error::Script(format!(
"{site} is reserved by {key} = \"{path}\": the document is served there, \
before the router"
)));
}
}
let doc = match &route.doc {
Some(table) => Some(
RouteDoc::parse(lua, table, &site, api.as_ref())
.map_err(|err| Error::Script(err.to_string()))?,
),
None => None,
};
to_openapi_path(&route.path).map_err(|msg| Error::Script(format!("{site}: {msg}")))?;
let hidden = doc.as_ref().is_some_and(|d| d.hidden);
let explicit = doc.as_ref().and_then(|d| d.operation_id.clone());
let mut operation_id = explicit
.clone()
.unwrap_or_else(|| default_operation_id(&route.method, &route.path));
if !hidden {
if let Some(&(first, first_explicit)) = ids.get(&operation_id) {
if explicit.is_some() || first_explicit {
return Err(Error::Script(format!(
"operation id `{operation_id}` is claimed twice\n --> {} (first here)\n --> {} (again here)\n \
set `doc = {{ operation_id = \"...\" }}` on one of them",
site_label(&def.routes[first].site),
site_label(&route.site),
)));
}
let base = operation_id.clone();
let mut n = 2;
while ids.contains_key(&format!("{base}_{n}")) {
n += 1;
}
operation_id = format!("{base}_{n}");
}
ids.insert(operation_id.clone(), (i, explicit.is_some()));
}
routes.push(RouteMeta {
method: route.method.clone(),
path: route.path.clone(),
doc,
input: inputs.get(i).cloned().flatten(),
operation_id,
});
}
Ok(AppMeta { api, routes })
}