use bstr::{BStr, BString, ByteSlice};
use gix_error::ExnMessageResult;
#[derive(Default, Debug, Clone, Copy, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub enum Ignore {
All,
Dirty,
Untracked,
#[default]
None,
}
impl TryFrom<&BStr> for Ignore {
type Error = ();
fn try_from(value: &BStr) -> Result<Self, Self::Error> {
Ok(match value.as_bytes() {
b"all" => Ignore::All,
b"dirty" => Ignore::Dirty,
b"untracked" => Ignore::Untracked,
b"none" => Ignore::None,
_ => return Err(()),
})
}
}
#[derive(Default, Debug, Clone, Copy, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub enum FetchRecurse {
#[default]
OnDemand,
Always,
Never,
}
impl FetchRecurse {
pub fn new(boolean: ExnMessageResult<Option<bool>>) -> Result<Option<Self>, BString> {
Ok(match boolean {
Ok(Some(value)) => Some(if value {
FetchRecurse::Always
} else {
FetchRecurse::Never
}),
Ok(None) => None,
Err(err) => {
let Some(gix_error::MetadataValue::Bytes(input)) = err.into_inner().values.remove("input") else {
unreachable!("gix-config-value validation errors retain their input as bytes");
};
if input != "on-demand" {
return Err(input);
}
Some(FetchRecurse::OnDemand)
}
})
}
}
#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub enum Branch {
CurrentInSuperproject,
Name(BString),
}
impl Default for Branch {
fn default() -> Self {
Branch::Name("HEAD".into())
}
}
impl TryFrom<&BStr> for Branch {
type Error = gix_error::Exn<gix_error::Message>;
fn try_from(value: &BStr) -> Result<Self, Self::Error> {
if value == "." {
return Ok(Branch::CurrentInSuperproject);
}
gix_refspec::parse(value, gix_refspec::parse::Operation::Fetch)
.map(|spec| Branch::Name(spec.source().expect("no object").to_owned()))
}
}
#[derive(Default, Debug, Clone, Hash, PartialOrd, PartialEq, Ord, Eq)]
pub enum Update {
#[default]
Checkout,
Rebase,
Merge,
Command(BString),
None,
}
impl TryFrom<&BStr> for Update {
type Error = ();
fn try_from(value: &BStr) -> Result<Self, Self::Error> {
Ok(match value.as_bstr().as_bytes() {
b"checkout" => Update::Checkout,
b"rebase" => Update::Rebase,
b"merge" => Update::Merge,
b"none" => Update::None,
command if command.first() == Some(&b'!') => Update::Command(command[1..].to_owned().into()),
_ => return Err(()),
})
}
}