| 1 |
use std::borrow::Borrow; |
| 2 |
use std::borrow::Cow; |
| 3 |
|
| 4 |
use crate::utils::hg_path::HgPath; |
| 5 |
|
| 6 |
/// Wraps `HgPath` or `HgPathBuf` to make it behave "as" its last path |
| 7 |
/// component, a.k.a. its base name (as in Python’s `os.path.basename`), but |
| 8 |
/// also allow recovering the full path. |
| 9 |
/// |
| 10 |
/// "Behaving as" means that equality and comparison consider only the base |
| 11 |
/// name, and `std::borrow::Borrow` is implemented to return only the base |
| 12 |
/// name. This allows using the base name as a map key while still being able |
| 13 |
/// to recover the full path, in a single memory allocation. |
| 14 |
#[derive(Debug)] |
| 15 |
pub struct WithBasename<T> { |
| 16 |
full_path: T, |
| 17 |
|
| 18 |
/// The position after the last slash separator in `full_path`, or `0` |
| 19 |
/// if there is no slash. |
| 20 |
base_name_start: usize, |
| 21 |
} |
| 22 |
|
| 23 |
impl<T> WithBasename<T> { |
| 24 |
pub fn full_path(&self) -> &T { |
| 25 |
&self.full_path |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
fn find_base_name_start(full_path: &HgPath) -> usize { |
| 30 |
if let Some(last_slash_position) = |
| 31 |
full_path.as_bytes().iter().rposition(|&byte| byte == b'/') |
| 32 |
{ |
| 33 |
last_slash_position + 1 |
| 34 |
} else { |
| 35 |
0 |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
impl<T: AsRef<HgPath>> WithBasename<T> { |
| 40 |
pub fn new(full_path: T) -> Self { |
| 41 |
Self { |
| 42 |
base_name_start: find_base_name_start(full_path.as_ref()), |
| 43 |
full_path, |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
pub fn from_raw_parts(full_path: T, base_name_start: usize) -> Self { |
| 48 |
debug_assert_eq!( |
| 49 |
base_name_start, |
| 50 |
find_base_name_start(full_path.as_ref()) |
| 51 |
); |
| 52 |
Self { base_name_start, full_path } |
| 53 |
} |
| 54 |
|
| 55 |
pub fn base_name(&self) -> &HgPath { |
| 56 |
HgPath::new(&self.full_path.as_ref().as_bytes()[self.base_name_start..]) |
| 57 |
} |
| 58 |
|
| 59 |
pub fn base_name_start(&self) -> usize { |
| 60 |
self.base_name_start |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
impl<T: AsRef<HgPath>> Borrow<HgPath> for WithBasename<T> { |
| 65 |
fn borrow(&self) -> &HgPath { |
| 66 |
self.base_name() |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
impl<T: AsRef<HgPath>> std::hash::Hash for WithBasename<T> { |
| 71 |
fn hash<H: std::hash::Hasher>(&self, hasher: &mut H) { |
| 72 |
self.base_name().hash(hasher) |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
impl<T: AsRef<HgPath> + PartialEq> PartialEq for WithBasename<T> { |
| 77 |
fn eq(&self, other: &Self) -> bool { |
| 78 |
self.base_name() == other.base_name() |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
impl<T: AsRef<HgPath> + Eq> Eq for WithBasename<T> {} |
| 83 |
|
| 84 |
impl<T: AsRef<HgPath> + PartialOrd> PartialOrd for WithBasename<T> { |
| 85 |
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> { |
| 86 |
self.base_name().partial_cmp(other.base_name()) |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
impl<T: AsRef<HgPath> + Ord> Ord for WithBasename<T> { |
| 91 |
fn cmp(&self, other: &Self) -> std::cmp::Ordering { |
| 92 |
self.base_name().cmp(other.base_name()) |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
impl<'a> WithBasename<&'a HgPath> { |
| 97 |
pub fn to_cow_borrowed(self) -> WithBasename<Cow<'a, HgPath>> { |
| 98 |
WithBasename { |
| 99 |
full_path: Cow::Borrowed(self.full_path), |
| 100 |
base_name_start: self.base_name_start, |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
pub fn to_cow_owned<'b>(self) -> WithBasename<Cow<'b, HgPath>> { |
| 105 |
WithBasename { |
| 106 |
full_path: Cow::Owned(self.full_path.to_owned()), |
| 107 |
base_name_start: self.base_name_start, |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
impl<'a> WithBasename<&'a HgPath> { |
| 113 |
/// Returns an iterator of `WithBasename<&HgPath>` for the ancestor |
| 114 |
/// directory paths of the given `path`, as well as `path` itself. |
| 115 |
/// |
| 116 |
/// For example, the full paths of inclusive ancestors of "a/b/c" are "a", |
| 117 |
/// "a/b", and "a/b/c" in that order. |
| 118 |
pub fn inclusive_ancestors_of( |
| 119 |
path: &'a HgPath, |
| 120 |
) -> impl Iterator<Item = WithBasename<&'a HgPath>> { |
| 121 |
let mut slash_positions = path |
| 122 |
.as_bytes() |
| 123 |
.iter() |
| 124 |
.enumerate() |
| 125 |
.filter_map(|(i, &byte)| if byte == b'/' { Some(i) } else { None }); |
| 126 |
let mut opt_next_component_start = Some(0); |
| 127 |
std::iter::from_fn(move || { |
| 128 |
opt_next_component_start.take().map(|next_component_start| { |
| 129 |
if let Some(slash_pos) = slash_positions.next() { |
| 130 |
opt_next_component_start = Some(slash_pos + 1); |
| 131 |
Self { |
| 132 |
full_path: HgPath::new(&path.as_bytes()[..slash_pos]), |
| 133 |
base_name_start: next_component_start, |
| 134 |
} |
| 135 |
} else { |
| 136 |
// Not setting `opt_next_component_start` here: there will |
| 137 |
// be no iteration after this one because `.take()` set it |
| 138 |
// to `None`. |
| 139 |
Self { |
| 140 |
full_path: path, |
| 141 |
base_name_start: next_component_start, |
| 142 |
} |
| 143 |
} |
| 144 |
}) |
| 145 |
}) |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
#[test] |
| 150 |
fn test() { |
| 151 |
let a = WithBasename::new(HgPath::new("a").to_owned()); |
| 152 |
assert_eq!(&**a.full_path(), HgPath::new(b"a")); |
| 153 |
assert_eq!(a.base_name(), HgPath::new(b"a")); |
| 154 |
|
| 155 |
let cba = WithBasename::new(HgPath::new("c/b/a").to_owned()); |
| 156 |
assert_eq!(&**cba.full_path(), HgPath::new(b"c/b/a")); |
| 157 |
assert_eq!(cba.base_name(), HgPath::new(b"a")); |
| 158 |
|
| 159 |
assert_eq!(a, cba); |
| 160 |
let borrowed: &HgPath = cba.borrow(); |
| 161 |
assert_eq!(borrowed, HgPath::new("a")); |
| 162 |
} |
| 163 |
|
| 164 |
#[test] |
| 165 |
fn test_inclusive_ancestors() { |
| 166 |
let mut iter = WithBasename::inclusive_ancestors_of(HgPath::new("a/bb/c")); |
| 167 |
|
| 168 |
let next = iter.next().unwrap(); |
| 169 |
assert_eq!(*next.full_path(), HgPath::new("a")); |
| 170 |
assert_eq!(next.base_name(), HgPath::new("a")); |
| 171 |
|
| 172 |
let next = iter.next().unwrap(); |
| 173 |
assert_eq!(*next.full_path(), HgPath::new("a/bb")); |
| 174 |
assert_eq!(next.base_name(), HgPath::new("bb")); |
| 175 |
|
| 176 |
let next = iter.next().unwrap(); |
| 177 |
assert_eq!(*next.full_path(), HgPath::new("a/bb/c")); |
| 178 |
assert_eq!(next.base_name(), HgPath::new("c")); |
| 179 |
|
| 180 |
assert!(iter.next().is_none()); |
| 181 |
} |