pub mod aider;
pub mod claude;
pub mod codex;
pub mod continue_editor;
pub mod copilot;
pub mod cursor;
pub mod gemini;
pub mod generic_mcp;
pub mod jetbrains;
pub mod kiro;
pub mod vscode;
pub mod windsurf;
pub mod zed;
use anyhow::{Context, Result};
use std::path::{Path, PathBuf};
pub fn write_user_file(path: &Path, content: &str) -> Result<()> {
std::fs::write(path, content).with_context(|| format!("Failed to write {}", path.display()))
}
pub trait EditorSetup {
fn setup(&self, myc_path: &str) -> Result<()>;
fn uninstall(&self) -> Result<()>;
#[allow(dead_code)]
fn config_path(&self) -> &PathBuf;
}
#[allow(dead_code)]
pub fn get_home_dir() -> Result<PathBuf> {
dirs::home_dir().ok_or_else(|| anyhow::anyhow!("Could not find home directory"))
}
#[allow(dead_code)]
pub fn get_config_dir() -> Result<PathBuf> {
dirs::config_dir().ok_or_else(|| anyhow::anyhow!("Could not find config directory"))
}
pub fn merge_mcp_servers(
config: &mut serde_json::Value,
myc_path: &str,
server_key: &str,
) -> Result<()> {
let obj = config
.as_object_mut()
.ok_or_else(|| anyhow::anyhow!("Invalid config format"))?;
let servers = obj
.entry(server_key.to_string())
.or_insert_with(|| serde_json::json!({}));
let servers_obj = servers
.as_object_mut()
.ok_or_else(|| anyhow::anyhow!("Invalid {} object", server_key))?;
servers_obj.insert(
"myceliums".to_string(),
serde_json::json!({
"command": myc_path,
"args": ["mcp"]
}),
);
Ok(())
}
pub fn remove_mcp_servers(config: &mut serde_json::Value, server_key: &str) -> Result<()> {
let obj = config
.as_object_mut()
.ok_or_else(|| anyhow::anyhow!("Invalid config format"))?;
if let Some(servers) = obj.get_mut(server_key).and_then(|s| s.as_object_mut()) {
servers.remove("myceliums");
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn write_user_file_writes_content() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("config.json");
write_user_file(&path, "{\"ok\":true}").unwrap();
assert_eq!(std::fs::read_to_string(&path).unwrap(), "{\"ok\":true}");
}
#[test]
fn write_user_file_reports_failure_and_leaves_config_untouched() {
let dir = tempfile::tempdir().unwrap();
let config_path = dir.path().join("config.json");
let original = "{\"mcpServers\":{\"myceliums\":{}}}";
std::fs::write(&config_path, original).unwrap();
let unwritable = config_path.join("nested.json");
let result = write_user_file(&unwritable, "");
assert!(result.is_err(), "write under a non-directory should error");
assert_eq!(
std::fs::read_to_string(&config_path).unwrap(),
original,
"user config must be left untouched on write failure"
);
}
}