pub fn is_digit(string: &str) -> bool {
string.chars().all(|c| c.is_digit(10))
}
pub fn is_exec(string: &str) -> bool {
string.starts_with("$ ")
}
pub fn is_garden(string: &str) -> bool {
string.starts_with(':')
}
pub fn is_group(string: &str) -> bool {
string.starts_with('%')
}
pub fn is_tree(string: &str) -> bool {
string.starts_with('@')
}
pub fn is_graft(string: &str) -> bool {
string.contains("::")
}
pub fn is_git_dir(string: &str) -> bool {
string.len() > 4 && string.ends_with(".git") && !string.ends_with("/.git")
}
pub fn trim(string: &str) -> &str {
let needs_trim = is_group(string) || is_tree(string) || is_garden(string);
if !string.is_empty() && needs_trim {
&string[1..]
} else {
string
}
}
pub fn trim_exec(string: &str) -> &str {
let prefix = "$ ";
let prefix_len = prefix.len();
if string.len() >= prefix_len && string.starts_with(prefix) {
&string[prefix_len..]
} else {
string
}
}
pub fn split_string<'a>(string: &'a str, split: &str) -> (bool, &'a str, &'a str) {
let end = string.len();
let split_len = split.len();
let before = string.find(split).unwrap_or(end);
let after; let ok = before <= (end - split_len);
if ok {
after = before + split_len;
} else {
after = before;
}
(ok, &string[..before], &string[after..])
}
pub fn split_graft(string: &str) -> (bool, &str, &str) {
split_string(string, "::")
}
pub fn trim_graft(string: &str) -> Option<String> {
let (ok, _before, after) = split_graft(string);
if !ok {
return None;
}
let result;
if is_garden(string) {
result = ":".to_string() + after;
} else if is_group(string) {
result = "%".to_string() + after;
} else if is_tree(string) {
result = "@".to_string() + after;
} else {
result = after.to_string();
}
Some(result)
}
pub fn graft_basename(string: &str) -> Option<String> {
let (ok, before, _after) = split_graft(string);
if !ok {
return None;
}
let result;
if is_garden(string) || is_group(string) || is_tree(string) {
result = trim(before).to_string();
} else {
result = before.to_string();
}
Some(result)
}