use std::marker::PhantomData;
use std::path::Path;
use crate::CommandError;
use crate::commit_ish::{CommitIsh, NoTarget, WithTarget};
#[must_use]
pub fn new() -> Checkout<'static, NoTarget> {
Checkout {
repo_path: None,
state: NoTarget,
_phantom: PhantomData,
}
}
#[derive(Debug)]
pub struct Checkout<'a, S> {
repo_path: Option<&'a Path>,
state: S,
_phantom: PhantomData<&'a ()>,
}
impl<'a, S> Checkout<'a, S> {
#[must_use]
pub fn repo_path(mut self, path: &'a Path) -> Self {
self.repo_path = Some(path);
self
}
}
impl<'a, S> crate::RepoPath<'a> for Checkout<'a, S> {
fn repo_path(self, path: &'a Path) -> Self {
self.repo_path(path)
}
}
impl<'a> Checkout<'a, NoTarget> {
#[must_use]
pub fn commit_ish(self, commit_ish: impl Into<CommitIsh<'a>>) -> Checkout<'a, WithTarget<'a>> {
Checkout {
repo_path: self.repo_path,
state: WithTarget {
target: commit_ish.into(),
},
_phantom: PhantomData,
}
}
}
impl<'a> Checkout<'a, WithTarget<'a>> {
pub async fn status(self) -> Result<(), CommandError> {
crate::Build::build(self).status().await
}
}
impl<'a> crate::Build for Checkout<'a, WithTarget<'a>> {
fn build(self) -> cmd_proc::Command {
crate::base_command(self.repo_path)
.argument("checkout")
.argument(self.state.target)
}
}
#[cfg(feature = "test-utils")]
impl<'a> Checkout<'a, WithTarget<'a>> {
pub fn test_eq(&self, other: &cmd_proc::Command) {
let command = crate::Build::build(Self {
repo_path: self.repo_path,
state: WithTarget {
target: self.state.target,
},
_phantom: PhantomData,
});
command.test_eq(other);
}
}