Get rid of Backend::Unknown variant

This commit is contained in:
southerntofu 2022-01-09 16:29:23 +01:00
parent ad61faaecf
commit dbd65cdc27
2 changed files with 15 additions and 36 deletions

View File

@ -6,16 +6,18 @@ use std::process::Command;
pub enum Backend { pub enum Backend {
Git, Git,
Mercurial, Mercurial,
Unknown(String),
} }
impl Backend { impl Backend {
pub fn from_setting(setting: Option<String>) -> Backend { /// Generates Some(Backend) from an optional String, or defaults to Git,
/// or None when the backend is not recognized
/// TODO: fallback backend should be customizable
pub fn from_setting(setting: Option<String>) -> Option<Backend> {
// Git is the default setting until further notice // Git is the default setting until further notice
setting.map_or(Backend::Git, |name| match name.as_ref() { setting.map_or(Some(Backend::Git), |name| match name.as_ref() {
"git" => Backend::Git, "git" => Some(Backend::Git),
"mercurial" => Backend::Mercurial, "mercurial" => Some(Backend::Mercurial),
_ => Backend::Unknown(name.to_string()), _ => None,
}) })
} }
} }
@ -55,10 +57,6 @@ impl Repo {
Backend::Mercurial => { Backend::Mercurial => {
unreachable!("UNIMPLEMENTED!"); unreachable!("UNIMPLEMENTED!");
} }
Backend::Unknown(name) => {
// TODO: This should be a forgebuild error message
panic!("Uknown backend: {}. Cannot find branch", name);
}
} }
} }
@ -77,11 +75,6 @@ impl Repo {
Backend::Mercurial => { Backend::Mercurial => {
unreachable!("Unimplemented"); unreachable!("Unimplemented");
} }
Backend::Unknown(name) => {
// TODO: This should be a forgebuild error message
eprintln!("Unknown DVCS: {}", name);
false
}
}; };
// If the clone was successful and subupdates is enabled, // If the clone was successful and subupdates is enabled,
@ -124,11 +117,6 @@ impl Repo {
Backend::Mercurial => { Backend::Mercurial => {
unreachable!("UNIMPLEMENTED"); unreachable!("UNIMPLEMENTED");
} }
Backend::Unknown(name) => {
// TODO: This should be a forgebuild error message
eprintln!("Unknown DVCS: {}", name);
false
}
} }
} }
@ -160,11 +148,6 @@ impl Repo {
}, },
Backend::Mercurial => { Backend::Mercurial => {
unimplemented!("Soon"); unimplemented!("Soon");
},
Backend::Unknown(name) => {
// TODO: This should be a forgebuild error message
eprintln!("Unknown DVCS:{}!", name);
Vec::new()
} }
} }
} }
@ -205,11 +188,6 @@ impl Repo {
Backend::Mercurial => { Backend::Mercurial => {
unimplemented!(); unimplemented!();
}, },
Backend::Unknown(name) => {
// TODO: This should be a forgebuild error message
eprintln!("Unknown DVCS:{}!", name);
false
}
} }
} }
@ -259,11 +237,6 @@ impl Repo {
Backend::Mercurial => { Backend::Mercurial => {
unreachable!("unimplemented"); unreachable!("unimplemented");
} }
Backend::Unknown(name) => {
// TODO: This should be a forgebuild error message
eprintln!("Unknown DVCS:{}!", name);
false
}
} }
} }
} }

View File

@ -76,13 +76,19 @@ impl Task {
context.insert("$i18n_source".to_string(), source_url.clone()); context.insert("$i18n_source".to_string(), source_url.clone());
} }
let dvcs = if let Some(dvcs) = Backend::from_setting(read_extension(path, "dvcs")) { dvcs } else {
// TODO: forgebuild error message
eprintln!("Unrecognized dvcs for task {}, possible options are 'git' or 'mercurial'. Skipped", &name);
return None;
};
Some(Task { Some(Task {
name, name,
bin: path.to_path_buf(), bin: path.to_path_buf(),
// None source = None repo // None source = None repo
repo: source.as_ref().map(|s| { repo: source.as_ref().map(|s| {
Repo::new( Repo::new(
Backend::from_setting(read_extension(path, "dvcs")), dvcs,
s, s,
&dest, &dest,
subupdates, subupdates,