use std::collections::BTreeSet;
use serde_json::Value;
pub const DTS: &str = include_str!("../../npm/devsandboxes/index.d.ts");
pub fn interface(name: &str) -> Vec<(String, String)> {
fields(name).into_iter().map(|(n, t, _)| (n, t)).collect()
}
fn fields(name: &str) -> Vec<(String, String, bool)> {
let (header, body) = block(name).unwrap_or_else(|| panic!("index.d.ts has no `interface {name}`"));
let mut out = props(body);
if let Some((_, bases)) = header.split_once("extends") {
for base in bases.split(',') {
let base = base.trim().split('<').next().unwrap_or("").trim();
if !base.is_empty() {
out.extend(fields(base));
}
}
}
out
}
pub fn prop_type(iface: &str, prop: &str) -> Option<String> {
interface(iface).into_iter().find(|(n, _)| n == prop).map(|(_, t)| t)
}
pub fn assert_matches(iface: &str, value: &Value) {
let keys: BTreeSet<&str> = value
.as_object()
.unwrap_or_else(|| panic!("{iface}: not an object: {value}"))
.keys()
.map(String::as_str)
.collect();
let all = fields(iface);
let names: BTreeSet<&str> = all.iter().map(|(n, _, _)| n.as_str()).collect();
let required: BTreeSet<&str> = all.iter().filter(|(_, _, opt)| !opt).map(|(n, _, _)| n.as_str()).collect();
let missing: Vec<_> = keys.difference(&names).collect();
let stale: Vec<_> = required.difference(&keys).collect();
assert!(
missing.is_empty() && stale.is_empty(),
"npm/devsandboxes/index.d.ts `interface {iface}` drifted from the wire: \
add {missing:?}, remove {stale:?} (wire value: {value})"
);
}
pub fn assert_variant(iface: &str, value: &Value) {
assert_matches(iface, value);
let tag = value["type"].as_str().unwrap_or_else(|| panic!("{iface}: no string `type` in {value}"));
assert_eq!(
prop_type(iface, "type").as_deref(),
Some(format!("'{tag}'").as_str()),
"npm/devsandboxes/index.d.ts `interface {iface}`: `type` must be '{tag}'"
);
}
pub fn union_members(name: &str) -> Vec<String> {
let needle = format!("export type {name} =");
let at = DTS.find(&needle).unwrap_or_else(|| panic!("index.d.ts has no `export type {name}`")) + needle.len();
let end = DTS[at..].find(';').map_or(DTS.len(), |e| at + e);
DTS[at..end].split('|').map(|m| m.trim().to_string()).filter(|m| !m.is_empty()).collect()
}
fn block(name: &str) -> Option<(&'static str, &'static str)> {
let needle = format!("interface {name}");
let mut from = 0;
let start = loop {
let at = DTS[from..].find(&needle)? + from;
let after = DTS[at + needle.len()..].chars().next()?;
if matches!(after, ' ' | '{' | '<') {
break at + needle.len();
}
from = at + needle.len();
};
let open = DTS[start..].find('{')? + start;
let mut depth = 0usize;
for (i, c) in DTS[open..].char_indices() {
match c {
'{' => depth += 1,
'}' => {
depth -= 1;
if depth == 0 {
return Some((&DTS[start..open], &DTS[open + 1..open + i]));
}
}
_ => {}
}
}
None
}
fn props(body: &str) -> Vec<(String, String, bool)> {
let mut out = Vec::new();
let mut depth = 0i32;
for line in body.lines() {
let t = line.trim();
let comment = t.starts_with('/') || t.starts_with('*');
if depth == 0 && !comment {
if let Some(p) = prop(t) {
out.push(p);
}
}
if !comment {
depth += t.matches(['{', '(', '[']).count() as i32 - t.matches(['}', ')', ']']).count() as i32;
}
}
out
}
fn prop(line: &str) -> Option<(String, String, bool)> {
let line = line.strip_prefix("readonly ").unwrap_or(line);
let (name, rest) = if let Some(q) = line.strip_prefix('\'') {
let end = q.find('\'')?;
(q[..end].to_string(), &q[end + 1..])
} else {
let end = line.find(|c: char| !(c.is_ascii_alphanumeric() || c == '_' || c == '$'))?;
(line[..end].to_string(), &line[end..])
};
if name.is_empty() {
return None;
}
let optional = rest.starts_with('?');
let rest = rest.strip_prefix('?').unwrap_or(rest);
let ty = rest.strip_prefix(':')?;
Some((name, ty.trim().trim_end_matches(';').trim().to_string(), optional))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parses_props_extends_and_quoted_names() {
let body = "\n /** doc { braces } */\n id: number;\n readonly name?: string | null;\n 'inbox.changed': InboxChanged;\n close: (x: {\n a: number;\n }) => void;\n after: boolean;\n";
let names: Vec<_> = props(body).into_iter().map(|(n, _, _)| n).collect();
assert_eq!(names, ["id", "name", "inbox.changed", "close", "after"]);
assert_eq!(props(body)[1].1, "string | null");
assert_eq!(props(body).iter().map(|p| p.2).collect::<Vec<_>>(), [false, true, false, false, false]);
assert!(interface("ThreadDetail").iter().any(|(n, _)| n == "owner_name"));
}
}