use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use crate::domain::index::LanguageId;
use crate::live_index::LiveIndex;
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, JsonSchema)]
#[schemars(rename = "Rewrite")]
pub struct StructuralRewrite {
pub pattern: String,
pub template: String,
#[serde(default)]
pub language: Option<String>,
#[serde(default)]
pub path_prefix: Option<String>,
#[serde(default)]
pub glob: Option<String>,
#[serde(default)]
pub limit: Option<u32>,
}
pub const DEFAULT_REWRITE_LIMIT: usize = 100;
pub const MAX_REWRITE_LIMIT: usize = 1000;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RewrittenFile {
pub path: String,
pub language: LanguageId,
pub original: Vec<u8>,
pub replacement: Vec<u8>,
pub sites: usize,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RewritePlan {
pub files: Vec<RewrittenFile>,
pub withheld: AdmissionWithheld,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct AdmissionWithheld {
pub policy: usize,
pub size: usize,
}
impl AdmissionWithheld {
fn suffix(self) -> String {
let mut parts = Vec::new();
if self.policy > 0 {
parts.push(format!(
"{} in-scope file(s) withheld by admission policy",
self.policy
));
}
if self.size > 0 {
parts.push(format!(
"{} in-scope file(s) over the size threshold",
self.size
));
}
if parts.is_empty() {
return String::new();
}
format!("; not searched: {}", parts.join(" and "))
}
}
impl RewritePlan {
pub fn sites(&self) -> usize {
self.files.iter().map(|file| file.sites).sum()
}
pub fn summaries(&self, dry_run: bool) -> Vec<String> {
self.files
.iter()
.map(|file| {
let summary = format!(
"{} — rewrote {} structural match(es) ({} → {} bytes)",
file.path,
file.sites,
file.original.len(),
file.replacement.len()
);
if dry_run {
format!("[DRY RUN] Would {summary}")
} else {
summary
}
})
.collect()
}
pub fn evidence(&self) -> String {
format!(
"{} structural match(es) across {} file(s){}",
self.sites(),
self.files.len(),
self.withheld.suffix()
)
}
}
pub fn refusal(message: impl std::fmt::Display) -> String {
format!("Error: structural rewrite refused: {message}. No files were modified.")
}
pub fn plan_structural_rewrite(
index: &LiveIndex,
request: &StructuralRewrite,
language: Option<LanguageId>,
) -> Result<RewritePlan, String> {
if request.pattern.trim().is_empty() {
return Err(refusal("`pattern` is empty"));
}
let limit = match request.limit {
None => DEFAULT_REWRITE_LIMIT,
Some(0) => return Err(refusal("`limit` must be at least 1")),
Some(limit) if limit as usize > MAX_REWRITE_LIMIT => {
return Err(refusal(format_args!(
"`limit` {limit} exceeds the maximum of {MAX_REWRITE_LIMIT}"
)));
}
Some(limit) => limit as usize,
};
let scope = Scope::new(request, language)?;
let withheld = scope.admission_withheld(index);
let mut candidates: Vec<(&String, &crate::live_index::store::IndexedFile)> = index
.all_files()
.filter(|(path, file)| scope.contains(path, Some(file.language)))
.collect();
candidates.sort_by(|left, right| left.0.cmp(right.0));
let mut compiled = std::collections::HashMap::new();
let mut first_compile_error: Option<String> = None;
let mut searched = 0usize;
let mut files = Vec::new();
let mut sites = 0usize;
for (path, file) in candidates {
let is_tsx = LanguageId::is_tsx_path(path);
let pattern = compiled.entry((file.language, is_tsx)).or_insert_with(|| {
crate::parsing::ast_grep::compile_structural_pattern(
&request.pattern,
&file.language,
is_tsx,
)
});
let pattern = match pattern.as_ref() {
Ok(pattern) => pattern,
Err(error) => {
first_compile_error.get_or_insert_with(|| error.clone());
continue;
}
};
let Ok(source) = std::str::from_utf8(&file.content) else {
continue;
};
searched += 1;
let (rewritten, found) = crate::parsing::ast_grep::structural_rewrite_with_compiled(
source,
pattern,
&request.template,
)
.map_err(|error| refusal(format_args!("{path}: {error}")))?;
if found == 0 {
continue;
}
sites += found;
if sites > limit {
return Err(refusal(format_args!(
"more than {limit} match(es) in scope; narrow `path_prefix`, `glob` or `language`, or raise `limit`"
)));
}
files.push(RewrittenFile {
path: path.clone(),
language: file.language,
original: file.content.clone(),
replacement: rewritten.into_bytes(),
sites: found,
});
}
if searched == 0 {
return Err(refusal(match first_compile_error {
Some(error) => error,
None => format!(
"no indexed source file with a structural grammar is in scope{}",
withheld.suffix()
),
}));
}
if files.is_empty() {
return Err(refusal(format_args!(
"the pattern matched nothing in {searched} file(s) in scope{}",
withheld.suffix()
)));
}
refuse_syntax_regressions(&files)?;
Ok(RewritePlan { files, withheld })
}
struct Scope {
prefix: crate::live_index::search::PathScope,
glob: Option<globset::GlobMatcher>,
language: Option<LanguageId>,
}
impl Scope {
fn new(request: &StructuralRewrite, language: Option<LanguageId>) -> Result<Self, String> {
let prefix = scope_prefix(request.path_prefix.as_deref())?;
let glob = match request.glob.as_deref().filter(|glob| !glob.is_empty()) {
None => None,
Some(glob) => Some(
globset::GlobBuilder::new(glob)
.literal_separator(true)
.build()
.map_err(|error| refusal(format_args!("invalid `glob` '{glob}': {error}")))?
.compile_matcher(),
),
};
if let Some(language) = language
&& crate::parsing::ast_grep::SgLang::from_language_id(&language, false).is_none()
{
return Err(refusal(format_args!(
"{} has no structural grammar",
language.name()
)));
}
Ok(Self {
prefix,
glob,
language,
})
}
fn contains(&self, path: &str, file_language: Option<LanguageId>) -> bool {
let Some(file_language) = file_language else {
return false;
};
self.prefix.matches(path)
&& self.glob.as_ref().is_none_or(|glob| glob.is_match(path))
&& self
.language
.is_none_or(|language| file_language == language)
&& crate::parsing::ast_grep::SgLang::from_language_id(&file_language, false).is_some()
}
fn admission_withheld(&self, index: &LiveIndex) -> AdmissionWithheld {
use crate::domain::{FileDisposition, HardSkipReason, MetadataOnlyReason};
let mut withheld = AdmissionWithheld::default();
for entry in &index.manifest_entries {
let Some(path) = entry.path.normalized_utf8.as_deref() else {
continue;
};
if !self.contains(path, LanguageId::from_path(path)) {
continue;
}
match &entry.disposition {
FileDisposition::MetadataOnly {
reason:
MetadataOnlyReason::SensitivePath { .. }
| MetadataOnlyReason::SensitiveContent { .. },
} => withheld.policy += 1,
FileDisposition::MetadataOnly {
reason: MetadataOnlyReason::OversizedData,
}
| FileDisposition::HardSkip {
reason: HardSkipReason::PerFileCeiling,
} => withheld.size += 1,
_ => {}
}
}
withheld
}
}
pub fn paths_in_scope(
index: &LiveIndex,
request: &StructuralRewrite,
language: Option<LanguageId>,
) -> Result<Vec<String>, String> {
let scope = Scope::new(request, language)?;
let mut paths: Vec<String> = index
.all_files()
.filter(|(path, file)| scope.contains(path, Some(file.language)))
.map(|(path, _)| path.clone())
.collect();
paths.sort();
Ok(paths)
}
pub fn withheld_in_scope(
index: &LiveIndex,
request: &StructuralRewrite,
language: Option<LanguageId>,
) -> Result<std::collections::BTreeMap<String, String>, String> {
let scope = Scope::new(request, language)?;
Ok(index
.withheld_since_restore()
.iter()
.filter(|(path, _)| scope.contains(path, LanguageId::from_path(path)))
.map(|(path, why)| (path.clone(), why.clone()))
.collect())
}
fn scope_prefix(prefix: Option<&str>) -> Result<crate::live_index::search::PathScope, String> {
let Some(prefix) = prefix.map(str::trim).filter(|prefix| !prefix.is_empty()) else {
return Ok(crate::live_index::search::PathScope::any());
};
let normalized = prefix.replace('\\', "/");
let normalized = normalized.trim_start_matches("./");
let escapes = std::path::Path::new(prefix).is_absolute()
|| normalized.starts_with('/')
|| normalized.split('/').any(|part| part == "..");
if escapes {
return Err(refusal(format_args!(
"`path_prefix` '{prefix}' is outside the repository; pass a repository-relative path"
)));
}
Ok(crate::live_index::search::PathScope::prefix(normalized))
}
fn refuse_syntax_regressions(files: &[RewrittenFile]) -> Result<(), String> {
let mut broken = Vec::new();
for file in files {
let is_tsx = LanguageId::is_tsx_path(&file.path);
let count = |bytes: &[u8]| {
std::str::from_utf8(bytes)
.map_err(|_| "invalid UTF-8".to_string())
.and_then(|source| {
crate::parsing::syntax_error_count(source, &file.language, is_tsx)
})
};
let before = count(&file.original)
.map_err(|error| refusal(format_args!("{}: {error}", file.path)))?;
let after = count(&file.replacement)
.map_err(|error| refusal(format_args!("{}: {error}", file.path)))?;
if after > before {
broken.push(format!(
"{} ({before} → {after} syntax error node(s))",
file.path
));
}
}
if broken.is_empty() {
return Ok(());
}
Err(refusal(format_args!(
"the template would break the syntax of {}",
broken.join(", ")
)))
}