use std::path::Path;
pub fn json_ok() -> String {
r#"{"ok":true}"#.to_string()
}
pub fn json_err(msg: &str) -> String {
serde_json::json!({ "error": msg }).to_string()
}
pub fn extract_query_param(qs: &str, key: &str) -> Option<String> {
for pair in qs.split('&') {
let Some((k, v)) = pair.split_once('=') else {
continue;
};
if k == key {
return Some(percent_decode_query_component(v));
}
}
None
}
pub(crate) fn wants_refresh(qs: &str) -> bool {
extract_query_param(qs, "_refresh").is_some_and(|v| !matches!(v.as_str(), "" | "0" | "false"))
}
pub fn percent_decode_query_component(s: &str) -> String {
let mut out: Vec<u8> = Vec::with_capacity(s.len());
let b = s.as_bytes();
let mut i = 0;
while i < b.len() {
match b[i] {
b'+' => {
out.push(b' ');
i += 1;
}
b'%' if i + 2 < b.len() => {
let h1 = (b[i + 1] as char).to_digit(16);
let h2 = (b[i + 2] as char).to_digit(16);
if let (Some(a), Some(d)) = (h1, h2) {
out.push(((a << 4) | d) as u8);
i += 3;
} else {
out.push(b'%');
i += 1;
}
}
_ => {
out.push(b[i]);
i += 1;
}
}
}
String::from_utf8_lossy(&out).into_owned()
}
pub fn normalize_dashboard_demo_path(path: &str) -> String {
let trimmed = path.trim();
if trimmed.is_empty() {
return String::new();
}
let candidate = Path::new(trimmed);
if candidate.is_absolute() || lean_ctx::core::pathutil::is_windows_absolute_path(trimmed) {
return trimmed.to_string();
}
let mut p = trimmed;
while p.starts_with("./") || p.starts_with(".\\") {
p = &p[2..];
}
p.trim_start_matches(['\\', '/']).replace('\\', "/")
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DashboardProjectSource {
ExplicitFlag,
Env,
Cwd,
Request,
Unbound,
}
pub fn dashboard_project_binding() -> (Option<String>, DashboardProjectSource) {
if let Some(project) = request_project() {
return (Some(project), DashboardProjectSource::Request);
}
dashboard_project_binding_in(std::env::current_dir().ok().as_deref())
}
pub(crate) fn request_project() -> Option<String> {
lean_ctx::core::policy::runtime::REQUEST_PROJECT
.try_with(|slot| slot.borrow().clone())
.ok()
.flatten()
.map(|project| project.to_string_lossy().into_owned())
.filter(|project| !project.is_empty())
}
pub(crate) fn request_ledger_scope() -> Option<lean_ctx::core::savings_ledger::ProjectScope> {
request_project().map(|project| {
lean_ctx::core::savings_ledger::ProjectScope::for_project(Path::new(&project))
})
}
pub(crate) fn project_names() -> std::collections::HashMap<String, String> {
use lean_ctx::core::savings_ledger::project_scope::{project_key_of, project_root_of};
crate::dashboard::project_binding::project_pool()
.iter()
.filter_map(|root| {
let key = project_key_of(Path::new(root))?;
let name = project_root_of(Path::new(root))
.file_name()?
.to_string_lossy()
.into_owned();
Some((key, name))
})
.collect()
}
pub(crate) fn dashboard_project_binding_in(
cwd: Option<&Path>,
) -> (Option<String>, DashboardProjectSource) {
if let Some(explicit) = lean_ctx::core::runtime_flags::dashboard_project() {
return (
Some(explicit.trim().to_string()),
DashboardProjectSource::ExplicitFlag,
);
}
if let Ok(explicit) = std::env::var("LEAN_CTX_PROJECT_ROOT")
&& !explicit.trim().is_empty()
{
return (
Some(explicit.trim().to_string()),
DashboardProjectSource::Env,
);
}
if let Some(root) = cwd
.filter(|cwd| !lean_ctx::core::pathutil::is_broad_or_unsafe_root(cwd))
.and_then(lean_ctx_server::server::derive_project_root_from)
.filter(|root| !lean_ctx::core::pathutil::is_broad_or_unsafe_root(Path::new(root)))
{
return (Some(root), DashboardProjectSource::Cwd);
}
(None, DashboardProjectSource::Unbound)
}
pub fn detect_project_root_for_dashboard() -> String {
dashboard_project_binding().0.unwrap_or_default()
}