use crate::config::{Contract, HtlConfig, RequireFields};
use anyhow::Result;
use std::path::{Path, PathBuf};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Resolved {
pub dir: String,
pub type_path: String,
pub require_fields: RequireFields,
pub module: Option<String>,
pub exclude: Vec<String>,
pub dts: Option<String>,
pub enforced_by: Option<String>,
pub declared_in: PathBuf,
pub declared_at: usize,
}
impl Resolved {
pub fn dirs(&self, root: &Path) -> Vec<PathBuf> {
let mut acc = vec![root.to_path_buf()];
for seg in self.dir.split('/').filter(|s| !s.is_empty() && *s != ".") {
let mut next = Vec::new();
for base in &acc {
if seg == "*" {
if let Ok(rd) = std::fs::read_dir(base) {
let mut subs: Vec<PathBuf> = rd
.flatten()
.map(|e| e.path())
.filter(|p| p.is_dir() && !crate::is_skipped_dir(p, &[]))
.collect();
subs.sort();
next.extend(subs);
}
} else {
let p = base.join(seg);
if p.is_dir() {
next.push(p);
}
}
}
acc = next;
}
acc
}
pub fn applies_to(&self, module: &str) -> bool {
if self
.type_path
.split_once('.')
.is_some_and(|(m, _)| m == module)
{
return false;
}
if self.exclude.iter().any(|e| e == module) {
return false;
}
match &self.module {
Some(only) => only == module,
None => true,
}
}
}
struct Marker {
dir: Option<String>,
module: Option<String>,
exclude: Option<Vec<String>>,
dts: Option<String>,
}
pub fn resolve(root: &Path, cfg: &HtlConfig) -> (Vec<Resolved>, Vec<String>) {
let mut out = Vec::new();
let mut problems = Vec::new();
for file in scan_targets(root, cfg) {
let Ok(src) = std::fs::read_to_string(&file) else {
continue;
};
if !src.contains("---@contract") {
continue;
}
match read_file(&file, &src, cfg) {
Ok(found) => out.extend(found),
Err(msgs) => problems.extend(msgs),
}
}
out.sort_by_key(|c| crate::is_declaration(&c.declared_in));
let mut kept: Vec<Resolved> = Vec::with_capacity(out.len());
for c in out {
if !kept.iter().any(|k| same_record(root, k, &c)) {
kept.push(c);
}
}
let out = kept;
for i in 0..out.len() {
if let Some(j) = out[..i].iter().position(|c| c.dir == out[i].dir) {
problems.push(format!(
"{}:{}:1: {} claims directory {:?}, which {} already claims at {}:{} \
[htl contract]",
out[i].declared_in.display(),
out[i].declared_at,
out[i].type_path,
out[i].dir,
out[j].type_path,
out[j].declared_in.display(),
out[j].declared_at,
));
}
}
(out, problems)
}
pub fn dts_target(root: &Path, c: &Resolved) -> Option<PathBuf> {
let module = c.type_path.split_once('.')?.0;
Some(match &c.dts {
Some(p) => crate::config::resolve_path(root, p),
None => root.join("types").join(format!("{module}.d.tl")),
})
}
fn same_record(root: &Path, earlier: &Resolved, later: &Resolved) -> bool {
if earlier.dir != later.dir {
return false;
}
if crate::same_file(&earlier.declared_in, &later.declared_in) {
return earlier.type_path == later.type_path;
}
dts_target(root, earlier).is_some_and(|t| crate::same_file(&t, &later.declared_in))
}
pub fn publish(root: &Path, contracts: &[Resolved]) -> (Vec<(PathBuf, bool)>, Vec<String>) {
let mut written = Vec::new();
let mut problems = Vec::new();
let mut targets: Vec<(PathBuf, &Resolved)> = Vec::new();
for c in contracts {
let Some(target) = dts_target(root, c) else {
continue;
};
if crate::same_file(&target, &c.declared_in) {
continue;
}
match targets.iter().find(|(t, _)| crate::same_file(t, &target)) {
Some((_, first)) if !crate::same_file(&first.declared_in, &c.declared_in) => problems
.push(format!(
"{}:{}:1: {} publishes to {}, where {} is already published from {} \
[htl contract]",
c.declared_in.display(),
c.declared_at,
c.type_path,
target.display(),
first.type_path,
first.declared_in.display(),
)),
Some(_) => {}
None => targets.push((target, c)),
}
}
for (target, c) in targets {
let Ok(src) = std::fs::read_to_string(&c.declared_in) else {
continue;
};
let src = contracts
.iter()
.filter(|o| crate::same_file(&o.declared_in, &c.declared_in))
.fold(src, |s, o| self_contained_marker(&s, o));
let text = match declaration_of(&src) {
Ok(t) => t,
Err(msgs) => {
problems.extend(msgs.into_iter().map(|m| {
format!(
"{}:{m} publishing {} to {} [htl contract]",
c.declared_in.display(),
c.type_path,
target.display()
)
}));
continue;
}
};
match crate::write_if_changed(&target, &text) {
Ok(w) => written.push((target, w)),
Err(e) => problems.push(format!(
"{}:1:1: writing {}: {e} [htl contract]",
c.declared_in.display(),
target.display()
)),
}
}
(written, problems)
}
fn self_contained_marker(src: &str, c: &Resolved) -> String {
let mut args = format!("{:?}", c.dir);
if let Some(m) = &c.module {
args.push_str(&format!(", module = {m:?}"));
}
if !c.exclude.is_empty() {
args.push_str(&format!(", exclude = {:?}", c.exclude.join(" ")));
}
let want = format!("---@contract({args})");
let mut lines: Vec<String> = src.lines().map(str::to_string).collect();
for i in [
c.declared_at.saturating_sub(1),
c.declared_at.saturating_sub(2),
] {
let Some(line) = lines.get_mut(i) else {
continue;
};
let Some(at) = line.find("---@contract") else {
continue;
};
let tail = &line[at + "---@contract".len()..];
let rest = match tail.split_once(')') {
Some((_, after)) if tail.trim_start().starts_with('(') => after.to_string(),
_ => tail.to_string(),
};
*line = format!("{}{want}{rest}", &line[..at]);
break;
}
let mut out = lines.join("\n");
out.push('\n');
out
}
struct Implementation {
span: (usize, usize),
doc: Vec<String>,
field: Option<(Vec<String>, String)>,
}
pub fn declaration_of(src: &str) -> Result<String, Vec<String>> {
let lines: Vec<&str> = src.lines().collect();
let mut problems = Vec::new();
let mut found: Vec<Implementation> = Vec::new();
let mut i = 0;
while i < lines.len() {
let Some(kind) = function_start(lines[i]) else {
i += 1;
continue;
};
let mut first = i;
while first > 0 && lines[first - 1].trim_start().starts_with("--") {
first -= 1;
}
let Some(end) = body_end(&lines, i) else {
problems.push(format!(
"{}:1: this function has no `end` at its own indentation, so its body \
cannot be told from what follows:",
i + 1
));
break;
};
match kind {
FnKind::Local => found.push(Implementation {
span: (first, end),
doc: Vec::new(),
field: None,
}),
FnKind::Exported => match signature(&lines, i) {
Ok((path, field)) => found.push(Implementation {
span: (first, end),
doc: lines[first..i]
.iter()
.map(|l| l.trim().to_string())
.collect(),
field: Some((path, field)),
}),
Err(e) => problems.push(format!("{}:1: {e}:", i + 1)),
},
}
i = end + 1;
}
if !problems.is_empty() {
return Err(problems);
}
if found.is_empty() {
return Ok(src.to_string());
}
let mut out: Vec<Option<String>> = lines.iter().map(|l| Some(l.to_string())).collect();
for imp in &found {
let Some((path, field)) = &imp.field else {
continue;
};
match record_close(&lines, path) {
Some(at) => {
let name = field.split(':').next().unwrap_or_default();
if declares_field(&lines, at, name) {
continue;
}
let indent = " ".repeat(indent_of(lines[at]) + 3);
let existing = out[at].take().unwrap_or_default();
let doc: String = imp.doc.iter().map(|l| format!("{indent}{l}\n")).collect();
out[at] = Some(format!("{doc}{indent}{field}\n{existing}"));
}
None => problems.push(format!(
"{}:1: nothing declares a record {} for this function to be a field of:",
imp.span.0 + 1,
path.join(".")
)),
}
}
if !problems.is_empty() {
return Err(problems);
}
for imp in &found {
let (first, last) = imp.span;
for l in out.iter_mut().take(last + 1).skip(first) {
*l = None;
}
if (first == 0 || lines[first - 1].trim().is_empty())
&& let Some(after) = out.get_mut(last + 1)
&& after.as_deref().is_some_and(|l| l.trim().is_empty())
{
*after = None;
}
}
let mut text: String = out
.into_iter()
.flatten()
.collect::<Vec<_>>()
.join("\n")
.trim_end()
.to_string();
text.push('\n');
Ok(text)
}
enum FnKind {
Exported,
Local,
}
fn function_start(line: &str) -> Option<FnKind> {
let t = line.trim_start();
if t.starts_with("local function ") {
Some(FnKind::Local)
} else if t.starts_with("function ") {
Some(FnKind::Exported)
} else {
None
}
}
fn body_end(lines: &[&str], i: usize) -> Option<usize> {
let base = indent_of(lines[i]);
(i + 1..lines.len()).find(|&j| {
let t = lines[j].trim_start();
(t == "end" || t.starts_with("end ") || t.starts_with("end-"))
&& indent_of(lines[j]) <= base
})
}
fn signature(lines: &[&str], i: usize) -> Result<(Vec<String>, String), String> {
let head = lines[i].trim_start().strip_prefix("function ").unwrap();
let (name, rest) = head
.split_once('(')
.ok_or("a function with no parameter list")?;
let method = name.contains(':');
let mut path: Vec<String> = name
.split(['.', ':'])
.map(|s| s.trim().to_string())
.collect();
let field = path.pop().filter(|f| !f.is_empty()).ok_or("no name")?;
if path.is_empty() {
return Err("a function on no module table".into());
}
let mut sig = rest.to_string();
let mut depth = 1i32 + count(rest);
let mut j = i;
while depth > 0 {
j += 1;
let next = *lines.get(j).ok_or("a parameter list that never closes")?;
depth += count(next);
sig.push('\n');
sig.push_str(next);
}
let sig = sig.trim_end();
let self_arg = if !method {
String::new()
} else if sig.trim_start().starts_with(')') {
format!("self: {}", path.last().unwrap())
} else {
format!("self: {}, ", path.last().unwrap())
};
Ok((path, format!("{field}: function({self_arg}{sig}")))
}
fn count(line: &str) -> i32 {
let code = line.split("--").next().unwrap_or(line);
code.chars().filter(|c| *c == '(').count() as i32
- code.chars().filter(|c| *c == ')').count() as i32
}
fn declares_field(lines: &[&str], close: usize, name: &str) -> bool {
let base = indent_of(lines[close]);
for j in (0..close).rev() {
let t = lines[j].trim_start();
if indent_of(lines[j]) <= base
&& (t.starts_with("record ") || t.starts_with("local record "))
{
return false;
}
if t.strip_prefix(name)
.is_some_and(|r| r.trim_start().starts_with(':'))
{
return true;
}
}
false
}
fn record_close(lines: &[&str], path: &[String]) -> Option<usize> {
let mut from = 0usize;
let mut to = lines.len();
for name in path {
let at = (from..to).find(|&j| record_name(lines[j]).as_deref() == Some(name.as_str()))?;
let base = indent_of(lines[at]);
to = (at + 1..to)
.find(|&j| lines[j].trim_start().starts_with("end") && indent_of(lines[j]) <= base)?;
from = at + 1;
}
Some(to)
}
fn scan_targets(root: &Path, cfg: &HtlConfig) -> Vec<PathBuf> {
let mut out = Vec::new();
for dir in cfg.search_paths(root) {
let Ok(entries) = std::fs::read_dir(&dir) else {
continue;
};
let mut here: Vec<PathBuf> = Vec::new();
for e in entries.flatten() {
let p = e.path();
if p.is_file() && is_teal(&p) {
here.push(p);
} else if p.is_dir() && !crate::is_skipped_dir(&p, &[]) {
for name in ["init.tl", "init.d.tl"] {
let init = p.join(name);
if init.is_file() {
here.push(init);
}
}
}
}
here.sort();
out.extend(here);
}
out.dedup();
out
}
fn is_teal(p: &Path) -> bool {
p.file_name()
.and_then(|s| s.to_str())
.is_some_and(|n| n.ends_with(".tl"))
}
fn module_name(file: &Path) -> Option<String> {
let stem = file.file_name()?.to_str()?.trim_end_matches(".tl");
let stem = stem.strip_suffix(".d").unwrap_or(stem);
if stem == "init" {
return Some(file.parent()?.file_name()?.to_str()?.to_string());
}
Some(stem.to_string())
}
fn read_file(file: &Path, src: &str, cfg: &HtlConfig) -> Result<Vec<Resolved>, Vec<String>> {
let lines: Vec<&str> = src.lines().collect();
let Some(module) = module_name(file) else {
return Ok(Vec::new());
};
let mut out = Vec::new();
let mut problems = Vec::new();
for (i, line) in lines.iter().enumerate() {
let Some(record) = record_name(line) else {
continue;
};
let Some(marker) = marker_on(&lines, i, "contract") else {
continue;
};
let marker = match parse_marker(&marker) {
Ok(m) => m,
Err(e) => {
problems.push(format!(
"{}:{}:1: {e} [htl contract]",
file.display(),
i + 1
));
continue;
}
};
let Some(path) = type_path(&lines, i, &module, &record) else {
problems.push(format!(
"{}:{}:1: {record} is the module {module} returns, not a type inside it: \
a contract type is written as <module>.<Type>, so declare it as a record \
within one [htl contract]",
file.display(),
i + 1
));
continue;
};
let dir = match (marker.dir, cfg.contract.as_slice()) {
(Some(d), _) => d,
(None, [one]) => one.dir.clone(),
(None, []) => {
problems.push(format!(
"{}:{}:1: ---@contract names no directory and htl.toml declares none: \
write ---@contract(\"<dir>\") here, or a [[contract]] dir = \"<dir>\" \
in htl.toml [htl contract]",
file.display(),
i + 1
));
continue;
}
(None, many) => {
problems.push(format!(
"{}:{}:1: ---@contract names no directory and htl.toml declares {}: \
write the directory on the marker [htl contract]",
file.display(),
i + 1,
many.len()
));
continue;
}
};
let inherited: Option<&Contract> = cfg.contract.iter().find(|c| c.dir == dir);
out.push(Resolved {
dir,
type_path: path,
require_fields: required_fields(&lines, i),
module: marker
.module
.or_else(|| inherited.and_then(|c| c.module.clone())),
exclude: marker
.exclude
.or_else(|| inherited.map(|c| c.exclude.clone()))
.unwrap_or_default(),
dts: marker.dts,
enforced_by: inherited.and_then(|c| c.enforced_by.clone()),
declared_in: file.to_path_buf(),
declared_at: i + 1,
});
}
for (i, line) in lines.iter().enumerate() {
if !line.contains("---@contract")
|| record_name(line).is_some()
|| lines.get(i + 1).and_then(|l| record_name(l)).is_some()
{
continue;
}
problems.push(format!(
"{}:{}:1: ---@contract is not on a record declaration [htl contract]",
file.display(),
i + 1
));
}
if problems.is_empty() {
Ok(out)
} else {
Err(problems)
}
}
fn marker_on(lines: &[&str], i: usize, name: &str) -> Option<String> {
let needle = format!("---@{name}");
let above = i
.checked_sub(1)
.and_then(|p| lines.get(p))
.filter(|l| l.trim_start().starts_with("---"));
for line in [lines.get(i), above].into_iter().flatten() {
if let Some(rest) = line.split(&needle).nth(1) {
if rest
.chars()
.next()
.is_none_or(|c| !c.is_alphanumeric() && c != '_')
{
let mine = rest.split("---@").next().unwrap_or(rest);
return Some(mine.trim().to_string());
}
}
}
None
}
fn parse_marker(rest: &str) -> Result<Marker, String> {
let mut m = Marker {
dir: None,
module: None,
exclude: None,
dts: None,
};
if rest.is_empty() {
return Ok(m);
}
let Some(args) = rest.strip_prefix('(').and_then(|r| r.split(')').next()) else {
return Err(format!(
"---@contract takes no arguments or a parenthesised list, got {rest:?}"
));
};
for (n, arg) in args.split(',').map(str::trim).enumerate() {
if arg.is_empty() {
continue;
}
match arg.split_once('=').map(|(k, v)| (k.trim(), v.trim())) {
Some(("module", v)) => m.module = Some(unquote(v)?),
Some(("exclude", v)) => {
m.exclude = Some(unquote(v)?.split_whitespace().map(str::to_string).collect())
}
Some(("dts", v)) => m.dts = Some(unquote(v)?),
Some((k, _)) => return Err(format!("---@contract has no {k:?} argument")),
None if n == 0 => m.dir = Some(unquote(arg)?),
None => return Err(format!("---@contract: {arg:?} is not <name> = <value>")),
}
}
Ok(m)
}
fn unquote(v: &str) -> Result<String, String> {
let t = v.trim();
t.strip_prefix('"')
.and_then(|t| t.strip_suffix('"'))
.map(str::to_string)
.ok_or_else(|| format!("---@contract: {v:?} is not a quoted string"))
}
fn record_name(line: &str) -> Option<String> {
let after = line.split("record").nth(1)?;
let name: String = after
.trim_start()
.chars()
.take_while(|c| c.is_alphanumeric() || *c == '_')
.collect();
(!name.is_empty()).then_some(name)
}
fn indent_of(line: &str) -> usize {
line.len() - line.trim_start().len()
}
fn type_path(lines: &[&str], i: usize, module: &str, record: &str) -> Option<String> {
let mut names = vec![record.to_string()];
let mut depth = indent_of(lines[i]);
for line in lines[..i].iter().rev() {
if line.trim().is_empty() {
continue;
}
let d = indent_of(line);
if d < depth
&& let Some(n) = record_name(line)
{
names.push(n);
depth = d;
}
}
names.pop()?;
if names.is_empty() {
return None;
}
names.reverse();
Some(format!("{module}.{}", names.join(".")))
}
fn required_fields(lines: &[&str], i: usize) -> RequireFields {
let base = indent_of(lines[i]);
let mut names = Vec::new();
for j in i + 1..lines.len() {
let line = lines[j];
if line.trim_start().starts_with("end") && indent_of(line) <= base {
break;
}
let name: String = line
.trim_start()
.chars()
.take_while(|c| c.is_alphanumeric() || *c == '_')
.collect();
if name.is_empty()
|| !line.trim_start()[name.len()..]
.trim_start()
.starts_with(':')
{
continue;
}
if marker_on(lines, j, "required").is_some() {
names.push(name);
}
}
RequireFields::Named(names)
}