pub mod backup;
mod claude_cli;
pub mod jsonedit;
pub mod named;
pub mod places;
pub mod read;
pub mod report;
mod search;
mod search_files;
pub mod servers;
pub mod text;
mod toml_key;
pub mod undo;
mod writes;
pub mod moves;
pub use moves::run;
pub fn claude_cli_hint() -> &'static str {
claude_cli::AFTER_FAILURE
}
pub use search::Mention;
use crate::clients::{
context::Context,
naming::{self, NEW, OLD},
receipt::{self, Book},
targets,
};
use places::Container;
use serde_json::Value;
use std::path::PathBuf;
pub fn static_client(client: &str) -> &'static str {
naming::CLIENTS
.into_iter()
.find(|c| *c == client)
.expect("a client the rename knows")
}
#[derive(Clone)]
pub struct Step {
pub container: Container,
pub entry: Value,
pub remove_only: bool,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Stop {
Taken(PathBuf, String),
Differ(PathBuf, String),
Unreadable(PathBuf),
Receipt(PathBuf),
Shared(PathBuf, &'static str),
Unwritable(PathBuf, &'static str),
Held,
}
pub struct Plan {
pub client: &'static str,
pub label: &'static str,
pub steps: Vec<Step>,
pub stops: Vec<Stop>,
pub found: search::Found,
pub hosted: Vec<(PathBuf, String)>,
}
impl Plan {
pub fn wanted(&self) -> bool {
!self.steps.is_empty() || self.stops.iter().any(|s| matches!(s, Stop::Unreadable(_)))
}
pub fn by_itself(&self) -> bool {
self.wanted() && self.stops.is_empty() && !self.found.blocks()
}
pub fn by_command(&self) -> bool {
self.wanted() && self.stops.iter().all(|s| *s == Stop::Held)
}
}
fn equal(old: &Value, new: &Value) -> bool {
let (Some(old), Some(new)) = (old.as_object(), new.as_object()) else {
return false;
};
old.iter().all(|(k, v)| new.get(k) == Some(v))
&& new
.iter()
.all(|(k, v)| old.contains_key(k) || (k == "type" && v == "stdio"))
}
pub fn ours(container: &Container, entry: &Value, context: &Context) -> bool {
let book = Book::of(container.client, context);
receipt::owns_in(&book, &container.file, &container.pointer(NEW), entry)
}
pub fn plan(client: &str, context: &Context) -> Plan {
let client = static_client(client);
let label = targets::mcp(client, context, "user").label;
let mut plan = Plan {
client,
label,
steps: vec![],
stops: vec![],
found: search::Found::default(),
hosted: vec![],
};
if !installed(client, context) || !naming::shipped(client) {
return plan;
}
let receipt_path = receipt::path(client, context);
if receipt::load(client, context).is_err() {
plan.stops.push(Stop::Receipt(receipt_path));
}
if receipt::held(client, context) {
plan.stops.push(Stop::Held);
}
for container in places::containers(client, context) {
let Ok(entries) = container.entries() else {
if places::contains_old(&container.file) {
plan.stops.push(Stop::Unreadable(container.file.clone()));
}
continue;
};
let new = entries.new;
if let Some(new) = &new
&& !ours(&container, new, context)
{
plan.stops
.push(Stop::Taken(container.file.clone(), container.pointer(NEW)));
}
let Some(old) = entries.old else {
continue;
};
if old.get("url").is_some() {
plan.hosted
.push((container.file.clone(), container.pointer(OLD)));
continue;
}
let remove_only = match &new {
Some(new) if ours(&container, new, context) && equal(&old, new) => true,
Some(new) if ours(&container, new, context) => {
plan.stops
.push(Stop::Differ(container.file.clone(), container.pointer(OLD)));
continue;
}
Some(_) => false,
None => false,
};
plan.steps.push(Step {
container,
entry: old,
remove_only,
});
}
let taken: Vec<PathBuf> = plan
.stops
.iter()
.filter_map(|s| match s {
Stop::Taken(path, _) => Some(path.clone()),
_ => None,
})
.collect();
for step in plan
.steps
.iter()
.filter(|s| !taken.contains(&s.container.file))
{
if let Err(key) = writes::check(step, OLD, NEW) {
plan.stops
.push(Stop::Unwritable(step.container.file.clone(), key));
}
}
if plan.wanted() {
plan.found = search::find(client, context);
}
plan
}
pub fn plan_all(context: &Context, forced: bool) -> Vec<Plan> {
let mut plans: Vec<Plan> = naming::CLIENTS.iter().map(|c| plan(c, context)).collect();
let moving = |plan: &Plan| {
if forced {
plan.by_command()
} else {
plan.by_itself()
}
};
for _ in 0..2 {
let state: Vec<(&'static str, bool, bool)> = plans
.iter()
.map(|p| (p.client, moving(p), installed(p.client, context)))
.collect();
for plan in &mut plans {
let shared: Vec<PathBuf> = plan
.steps
.iter()
.filter(|s| s.container.shared())
.map(|s| s.container.file.clone())
.collect();
for file in shared {
for (other, moves, present) in &state {
let reads = ["claude-code", "muse-code"].contains(other);
let stop = Stop::Shared(file.clone(), other);
if *other != plan.client
&& reads
&& *present
&& !moves
&& !plan.stops.contains(&stop)
{
plan.stops.push(stop);
}
}
}
}
}
plans
}
pub fn installed(client: &str, context: &Context) -> bool {
context.directory(client).is_dir()
}