#[must_use]
pub fn split_module(src: &str) -> (&str, &str) {
match src.find("\n#[cfg(test)]") {
Some(i) => (&src[..i + 1], &src[i + 1..]),
None => (src, ""),
}
}
#[must_use]
pub fn split_regions(src: &str) -> (String, String) {
let (mut code, mut tests) = (String::new(), String::new());
let (mut in_tests, mut head) = (false, false);
for line in src.lines() {
if !in_tests && line.starts_with("#[cfg(test)]") {
(in_tests, head) = (true, true);
}
let half = if in_tests { &mut tests } else { &mut code };
half.push_str(line);
half.push('\n');
let item_line = head && !line.starts_with(['#', '/']);
head &= !item_line;
in_tests &= !region_ends(line, item_line);
}
(code, tests)
}
fn region_ends(line: &str, item_line: bool) -> bool {
line == "}" || (item_line && line.ends_with(';'))
}
#[must_use]
pub fn public_fns(src: &str) -> Vec<String> {
src.lines()
.filter_map(|l| {
let t = l.trim().strip_prefix("pub fn ")?;
Some(t.split(['(', '<']).next()?.trim().to_string())
})
.collect()
}
#[must_use]
pub fn is_called(src: &str, name: &str) -> bool {
let declares = |l: &str| {
l.contains(&format!("fn {name}(")) || l.contains(&format!("fn {name}<"))
};
src.lines()
.any(|l| l.contains(&format!("{name}(")) && !declares(l))
}
const NOT_A_CALL: [&str; 18] = [
"fn",
"if",
"for",
"while",
"match",
"let",
"return",
"assert",
"assert_eq",
"assert_ne",
"panic",
"println",
"format",
"vec",
"write",
"read",
"Some",
"Ok",
];
fn next_ident(b: &[u8], from: usize) -> Option<(usize, usize)> {
let mut i = from;
while i < b.len() && !(b[i].is_ascii_alphabetic() || b[i] == b'_') {
i = i.saturating_add(1);
}
if i >= b.len() {
return None;
}
let start = i;
while i < b.len() && (b[i].is_ascii_alphanumeric() || b[i] == b'_') {
i = i.saturating_add(1);
}
Some((start, i))
}
fn opens_call(src: &str, b: &[u8], start: usize, end: usize) -> bool {
if b.get(end) != Some(&b'(') {
return false;
}
if start > 0 && matches!(b.get(start.wrapping_sub(1)), Some(&b'.' | &b'!'))
{
return false;
}
let mut k = start;
while k > 0 && matches!(b.get(k.wrapping_sub(1)), Some(&b' ' | &b'\t')) {
k = k.saturating_sub(1);
}
k < 2 || src.get(k.saturating_sub(2)..k) != Some("fn")
}
fn closing_paren(b: &[u8], open: usize) -> usize {
let mut depth = 0usize;
for (j, c) in b.iter().enumerate().skip(open) {
if *c == b'(' {
depth = depth.saturating_add(1);
} else if *c == b')' {
depth = depth.saturating_sub(1);
if depth == 0 {
return j;
}
}
}
b.len()
}
fn call_text(src: &str, b: &[u8], start: usize, open: usize) -> String {
let end = closing_paren(b, open).saturating_add(1).min(src.len());
src.get(start..end)
.unwrap_or_default()
.split_whitespace()
.collect::<Vec<_>>()
.join(" ")
}
#[must_use]
pub fn expected_calls(test_src: &str, existing: &str) -> Vec<String> {
let b = test_src.as_bytes();
let mut out: Vec<String> = Vec::new();
let mut i = 0;
while let Some((start, end)) = next_ident(b, i) {
i = end;
let Some(name) = test_src.get(start..end) else {
continue;
};
if !opens_call(test_src, b, start, end)
|| NOT_A_CALL.contains(&name)
|| existing.contains(&format!("fn {name}"))
{
continue;
}
let call = call_text(test_src, b, start, end);
if !out.contains(&call) {
out.push(call);
}
}
out
}
fn is_signature(s: &str) -> bool {
["pub fn", "pub struct", "pub enum", "pub const"]
.iter()
.any(|k| s.starts_with(k))
}
#[derive(Default)]
struct Surface<'a> {
out: String,
in_body: bool,
pending: Vec<&'a str>,
}
impl<'a> Surface<'a> {
fn line(&mut self, line: &'a str) {
let s = line.trim();
if s.starts_with("///") {
self.doc(line);
} else if self.in_body {
self.body(line, s);
} else if is_signature(s) {
self.signature(line, s);
} else {
self.pending.clear();
}
}
fn doc(&mut self, line: &'a str) {
if self.in_body {
self.emit(line);
} else {
self.pending.push(line);
}
}
fn body(&mut self, line: &str, s: &str) {
if s == "}" {
self.in_body = false;
self.out.push_str("}\n");
} else if !s.is_empty() {
self.emit(line);
}
}
fn signature(&mut self, line: &str, s: &str) {
for d in std::mem::take(&mut self.pending) {
self.out.push_str(d);
self.out.push('\n');
}
if s.starts_with("pub fn") {
self.out
.push_str(s.split('{').next().unwrap_or(s).trim_end());
self.out.push_str(" { /* ... */ }\n");
return;
}
self.emit(line);
self.in_body = s.ends_with('{');
}
fn emit(&mut self, line: &str) {
self.out.push_str(line);
self.out.push('\n');
}
}
#[must_use]
pub fn signatures(impl_src: &str) -> String {
let mut s = Surface::default();
for line in impl_src.lines() {
s.line(line);
}
s.out
}
#[must_use]
pub fn test_decls(tests_src: &str) -> String {
let mut out = String::new();
for line in tests_src.lines() {
let s = line.trim().trim_start_matches("pub ");
let keep = s.starts_with("fn ")
|| s.starts_with("struct ")
|| s.starts_with("enum ")
|| s.starts_with("const ")
|| s.starts_with("impl ");
if keep {
let head = s.split('{').next().unwrap_or(s).trim_end();
out.push_str(head);
out.push('\n');
}
}
out
}
#[must_use]
pub fn literal_indexes(body: &str) -> Vec<(String, usize)> {
let b = body.as_bytes();
let mut seen: Vec<(String, usize)> = Vec::new();
for (i, c) in b.iter().enumerate() {
if *c != b'[' {
continue;
}
let Some(name) = indexed_binding(body, b, i) else {
continue;
};
match seen.iter_mut().find(|(n, _)| n == &name) {
Some((_, n)) => *n = n.saturating_add(1),
None => seen.push((name, 1)),
}
}
seen
}
fn indexed_binding(body: &str, b: &[u8], at: usize) -> Option<String> {
let digits: String = body
.get(at.saturating_add(1)..)?
.chars()
.take_while(char::is_ascii_digit)
.collect();
if digits.is_empty()
|| b.get(at.saturating_add(1).saturating_add(digits.len()))
!= Some(&b']')
{
return None;
}
let name: String = body
.get(..at)?
.chars()
.rev()
.take_while(|c| c.is_alphanumeric() || *c == '_')
.collect::<Vec<char>>()
.into_iter()
.rev()
.collect();
(!name.is_empty()).then_some(name)
}
#[must_use]
pub fn fn_names(src: &str) -> Vec<String> {
src.lines()
.filter_map(|l| {
let s = l.trim_start();
let r = s
.strip_prefix("pub fn ")
.or_else(|| s.strip_prefix("pub(crate) fn "))
.or_else(|| s.strip_prefix("fn "))?;
let name: String = r
.chars()
.take_while(|c| c.is_alphanumeric() || *c == '_')
.collect();
(!name.is_empty()).then_some(name)
})
.collect()
}
#[must_use]
pub fn fn_body<'a>(src: &'a str, name: &str) -> Option<&'a str> {
let decl = format!("fn {name}(");
let at = src.find(&decl)?;
let open = src.get(at..)?.find('{')?.saturating_add(at);
let b = src.as_bytes();
let (mut depth, mut i) = (0usize, open);
while i < b.len() {
match b.get(i) {
Some(&b'"' | &b'\'') => {
i = skip_literal(b, i);
continue;
}
Some(&b'{') => depth = depth.saturating_add(1),
Some(&b'}') => {
depth = depth.saturating_sub(1);
if depth == 0 {
return src.get(open..=i);
}
}
_ => {}
}
i = i.saturating_add(1);
}
src.get(open..)
}
fn skip_literal(b: &[u8], at: usize) -> usize {
let Some("e) = b.get(at) else {
return at.saturating_add(1);
};
if quote == b'\'' {
let escaped = b.get(at.saturating_add(1)) == Some(&b'\\');
let closes = b.get(at.saturating_add(2)) == Some(&b'\'');
if !escaped && !closes {
return at.saturating_add(1); }
}
let mut i = at.saturating_add(1);
while i < b.len() {
match b.get(i) {
Some(&b'\\') => i = i.saturating_add(2),
Some(c) if *c == quote => return i.saturating_add(1),
_ => i = i.saturating_add(1),
}
}
i
}
#[must_use]
pub fn markers(body: &str) -> Vec<String> {
let mut out: Vec<String> = Vec::new();
let mut rest = body;
while let Some(i) = rest.find('"') {
let Some(tail) = rest.get(i.saturating_add(1)..) else {
break;
};
let Some(end) = tail
.find('"')
.filter(|e| !tail.get(..*e).is_some_and(|s| s.ends_with('\\')))
else {
break;
};
if let Some(lit) = tail.get(..end)
&& lit.chars().count() >= 3
&& !lit.contains("{}")
&& !out.iter().any(|o| o == lit)
{
out.push(lit.to_string());
}
rest = tail.get(end.saturating_add(1)..).unwrap_or_default();
}
out.sort();
out
}
#[cfg(test)]
#[path = "tests/code.rs"]
mod tests;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ModDecl {
pub name: String,
pub is_pub: bool,
}
#[must_use]
pub fn mod_decls(src: &str) -> Vec<ModDecl> {
let mut out = Vec::new();
let mut test_only = false;
for line in src.lines().map(str::trim).filter(|l| !l.is_empty()) {
if line.starts_with("#[cfg(test)]") {
test_only = true;
continue;
}
if let Some(decl) = one_decl(line)
&& !test_only
{
out.push(decl);
}
test_only = false;
}
out
}
fn one_decl(line: &str) -> Option<ModDecl> {
let (is_pub, rest) = match line.strip_prefix("pub(") {
Some(r) => (false, r.split_once(") ").map(|(_, r)| r)?),
None => line
.strip_prefix("pub ")
.map_or((false, line), |r| (true, r)),
};
let name = rest.strip_prefix("mod ")?.strip_suffix(';')?;
(!name.contains(char::is_whitespace)).then(|| ModDecl {
name: name.to_string(),
is_pub,
})
}
#[must_use]
pub fn crate_uses(src: &str) -> Vec<String> {
let mut out: Vec<String> = src
.lines()
.map(str::trim)
.filter_map(|l| l.strip_prefix("use crate::"))
.flat_map(use_names)
.collect();
out.sort();
out.dedup();
out
}
fn use_names(rest: &str) -> Vec<String> {
let Some(group) = rest.strip_prefix('{') else {
return leading_ident(rest).into_iter().collect();
};
group
.split_once('}')
.map_or(group, |(inner, _)| inner)
.split(',')
.filter_map(|part| leading_ident(part.trim()))
.collect()
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Import {
pub module: String,
pub items: Vec<String>,
}
#[must_use]
pub fn crate_imports(src: &str) -> Vec<Import> {
src.lines()
.map(str::trim)
.filter_map(|l| l.strip_prefix("use crate::"))
.flat_map(|rest| imports_of(rest.trim_end_matches(';').trim()))
.collect()
}
fn imports_of(rest: &str) -> Vec<Import> {
let Some(group) = rest.strip_prefix('{') else {
return one_import(rest).into_iter().collect();
};
split_top(group.strip_suffix('}').unwrap_or(group))
.iter()
.filter_map(|part| one_import(part.trim()))
.collect()
}
fn one_import(path: &str) -> Option<Import> {
let module = leading_ident(path)?;
let rest = path.get(module.len()..).unwrap_or("");
let Some(tail) = rest.strip_prefix("::") else {
return Some(Import {
module,
items: vec![],
});
};
let items = match tail.strip_prefix('{') {
Some(g) => split_top(g.strip_suffix('}').unwrap_or(g))
.iter()
.filter_map(|p| last_segment(p.trim()))
.collect(),
None => last_segment(tail).into_iter().collect(),
};
Some(Import { module, items })
}
fn last_segment(path: &str) -> Option<String> {
let last = path.rsplit("::").next().unwrap_or(path).trim();
(!last.starts_with('{'))
.then(|| leading_ident(last))
.flatten()
}
fn split_top(group: &str) -> Vec<String> {
let mut out = vec![String::new()];
let mut depth = 0usize;
for c in group.chars() {
match c {
'{' => depth = depth.saturating_add(1),
'}' => depth = depth.saturating_sub(1),
',' if depth == 0 => {
out.push(String::new());
continue;
}
_ => {}
}
if let Some(last) = out.last_mut() {
last.push(c);
}
}
out
}
fn leading_ident(s: &str) -> Option<String> {
let name: String = s
.chars()
.take_while(|c| c.is_alphanumeric() || *c == '_')
.collect();
(!name.is_empty()).then_some(name)
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PubType {
pub name: String,
pub kind: String,
}
const TYPE_KINDS: [&str; 4] = ["struct", "enum", "trait", "type"];
#[must_use]
pub fn public_types(src: &str) -> Vec<PubType> {
src.lines().filter_map(|l| one_type(l.trim())).collect()
}
#[must_use]
pub fn types_in(sources: &[String]) -> Vec<PubType> {
let mut out: Vec<PubType> = Vec::new();
for src in sources {
for t in public_types(src) {
if !out.contains(&t) {
out.push(t);
}
}
}
out.sort_by(|a, b| a.name.cmp(&b.name));
out
}
fn one_type(line: &str) -> Option<PubType> {
let rest = line.strip_prefix("pub ")?;
let kind = TYPE_KINDS
.iter()
.find(|k| rest.strip_prefix(**k).is_some_and(|r| r.starts_with(' ')))?;
let name: String = rest
.get(kind.len().saturating_add(1)..)?
.chars()
.take_while(|c| c.is_alphanumeric() || *c == '_')
.collect();
(!name.is_empty()).then(|| PubType {
name,
kind: (*kind).to_string(),
})
}
#[cfg(test)]
#[path = "tests/structure.rs"]
mod structure_tests;