use crate::command::{CommandExecutor, CommandOutput, GitCommand};
use crate::error::Result;
use async_trait::async_trait;
#[derive(Debug, Clone, Default)]
pub struct SwitchCommand {
pub executor: CommandExecutor,
pub target: Option<String>,
pub create: Option<String>,
pub force_create: Option<String>,
pub detach: bool,
pub discard_changes: bool,
pub track: bool,
pub no_track: bool,
pub orphan: Option<String>,
}
impl SwitchCommand {
#[must_use]
pub fn new() -> Self {
Self::default()
}
pub fn target(&mut self, t: impl Into<String>) -> &mut Self {
self.target = Some(t.into());
self
}
pub fn create(&mut self, name: impl Into<String>) -> &mut Self {
self.create = Some(name.into());
self
}
pub fn force_create(&mut self, name: impl Into<String>) -> &mut Self {
self.force_create = Some(name.into());
self
}
pub fn detach(&mut self) -> &mut Self {
self.detach = true;
self
}
pub fn discard_changes(&mut self) -> &mut Self {
self.discard_changes = true;
self
}
pub fn track(&mut self) -> &mut Self {
self.track = true;
self
}
pub fn no_track(&mut self) -> &mut Self {
self.no_track = true;
self
}
pub fn orphan(&mut self, name: impl Into<String>) -> &mut Self {
self.orphan = Some(name.into());
self
}
}
#[async_trait]
impl GitCommand for SwitchCommand {
type Output = CommandOutput;
fn get_executor(&self) -> &CommandExecutor {
&self.executor
}
fn get_executor_mut(&mut self) -> &mut CommandExecutor {
&mut self.executor
}
fn build_command_args(&self) -> Vec<String> {
let mut args = vec!["switch".to_string()];
if self.detach {
args.push("--detach".into());
}
if self.discard_changes {
args.push("--discard-changes".into());
}
if self.track {
args.push("--track".into());
}
if self.no_track {
args.push("--no-track".into());
}
if let Some(o) = &self.orphan {
args.push("--orphan".into());
args.push(o.clone());
}
if let Some(b) = &self.create {
args.push("-c".into());
args.push(b.clone());
}
if let Some(b) = &self.force_create {
args.push("-C".into());
args.push(b.clone());
}
if let Some(t) = &self.target {
args.push(t.clone());
}
args
}
async fn execute(&self) -> Result<CommandOutput> {
self.execute_raw().await
}
}