use std::path::Path;
use std::process::Command;
use crate::error::{GwmError, Result};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EditorType {
VsCode,
Cursor,
Zed,
}
impl EditorType {
pub fn command(&self) -> &'static str {
match self {
EditorType::VsCode => "code",
EditorType::Cursor => "cursor",
EditorType::Zed => "zed",
}
}
pub fn display_name(&self) -> &'static str {
match self {
EditorType::VsCode => "VS Code",
EditorType::Cursor => "Cursor",
EditorType::Zed => "Zed",
}
}
}
pub fn open_in_editor(editor: EditorType, path: &Path) -> Result<()> {
Command::new(editor.command())
.arg(path)
.spawn()
.map_err(GwmError::Io)?;
Ok(())
}
pub fn open_in_vscode(path: &Path) -> Result<()> {
open_in_editor(EditorType::VsCode, path)
}
pub fn open_in_cursor(path: &Path) -> Result<()> {
open_in_editor(EditorType::Cursor, path)
}
pub fn open_in_zed(path: &Path) -> Result<()> {
open_in_editor(EditorType::Zed, path)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_editor_type_command() {
assert_eq!(EditorType::VsCode.command(), "code");
assert_eq!(EditorType::Cursor.command(), "cursor");
assert_eq!(EditorType::Zed.command(), "zed");
}
#[test]
fn test_editor_type_display_name() {
assert_eq!(EditorType::VsCode.display_name(), "VS Code");
assert_eq!(EditorType::Cursor.display_name(), "Cursor");
assert_eq!(EditorType::Zed.display_name(), "Zed");
}
}