1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
use crate::{
chars::Chars,
pack::{Pack, PackError},
utils,
};
use arcstr::ArcStr;
use bytes::{Buf, BufMut};
use std::{
borrow::{Borrow, Cow},
cmp::{Eq, Ord, PartialEq, PartialOrd},
convert::{AsRef, From},
fmt,
iter::{DoubleEndedIterator, Iterator},
ops::Deref,
result::Result,
str::{self, FromStr},
};
pub static ESC: char = '\\';
pub static SEP: char = '/';
fn is_canonical(s: &str) -> bool {
for _ in Path::parts(s).filter(|p| *p == "") {
return false;
}
true
}
fn canonize(s: &str) -> String {
let mut res = String::with_capacity(s.len());
if s.len() > 0 {
if s.starts_with(SEP) {
res.push(SEP)
}
let mut first = true;
for p in Path::parts(s).filter(|p| *p != "") {
if first {
first = false;
} else {
res.push(SEP)
}
res.push_str(p);
}
}
res
}
/// A path in the namespace. Paths are immutable and reference
/// counted. Path components are seperated by /, which may be escaped
/// with \. / and \ are the only special characters in path, any other
/// unicode character may be used. Path lengths are not limited on the
/// local machine, but may be restricted by maximum message size on
/// the wire.
#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct Path(ArcStr);
impl Pack for Path {
fn encoded_len(&self) -> usize {
<ArcStr as Pack>::encoded_len(&self.0)
}
fn encode(&self, buf: &mut impl BufMut) -> Result<(), PackError> {
<ArcStr as Pack>::encode(&self.0, buf)
}
fn decode(buf: &mut impl Buf) -> Result<Self, PackError> {
Ok(Path::from(<ArcStr as Pack>::decode(buf)?))
}
}
impl fmt::Display for Path {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
impl AsRef<str> for Path {
fn as_ref(&self) -> &str {
&*self.0
}
}
impl Borrow<str> for Path {
fn borrow(&self) -> &str {
&*self.0
}
}
impl Deref for Path {
type Target = str;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl From<Chars> for Path {
fn from(c: Chars) -> Path {
if is_canonical(&c) {
Path(ArcStr::from(c.as_ref()))
} else {
Path(ArcStr::from(canonize(&c)))
}
}
}
impl From<String> for Path {
fn from(s: String) -> Path {
if is_canonical(&s) {
Path(ArcStr::from(s))
} else {
Path(ArcStr::from(canonize(&s)))
}
}
}
impl From<&'static str> for Path {
fn from(s: &'static str) -> Path {
if is_canonical(s) {
Path(ArcStr::from(s))
} else {
Path(ArcStr::from(canonize(s)))
}
}
}
impl<'a> From<&'a String> for Path {
fn from(s: &String) -> Path {
if is_canonical(s.as_str()) {
Path(ArcStr::from(s.clone()))
} else {
Path(ArcStr::from(canonize(s.as_str())))
}
}
}
impl From<ArcStr> for Path {
fn from(s: ArcStr) -> Path {
if is_canonical(&*s) {
Path(s)
} else {
Path(ArcStr::from(canonize(&*s)))
}
}
}
impl FromStr for Path {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if is_canonical(s) {
Ok(Path(ArcStr::from(s)))
} else {
Ok(Path(ArcStr::from(canonize(s))))
}
}
}
pub enum DirNames<'a> {
Root(bool),
Path { cur: &'a str, all: &'a str, base: usize },
}
impl<'a> Iterator for DirNames<'a> {
type Item = &'a str;
fn next(&mut self) -> Option<Self::Item> {
match self {
DirNames::Path { ref mut cur, all, ref mut base } => {
if *base >= all.len() {
None
} else {
match Path::find_sep(cur) {
None => {
*base = all.len();
Some(all)
}
Some(p) => {
*base += p + 1;
*cur = &all[*base..];
Some(&all[0..*base - 1])
}
}
}
}
DirNames::Root(true) => {
*self = DirNames::Root(false);
Some("/")
}
DirNames::Root(false) => None,
}
}
}
impl<'a> DoubleEndedIterator for DirNames<'a> {
fn next_back(&mut self) -> Option<Self::Item> {
match self {
DirNames::Path { cur: _, ref mut all, base: _ } => {
match Path::dirname(*all) {
Some(dn) => {
let res = *all;
*all = dn;
Some(res)
}
None => {
*self = DirNames::Root(false);
Some("/")
}
}
}
DirNames::Root(true) => {
*self = DirNames::Root(false);
Some("/")
}
DirNames::Root(false) => None,
}
}
}
impl Path {
/// create a path from a non static str by copying the contents of the str
pub fn from_str(s: &str) -> Self {
if is_canonical(s) {
Path(ArcStr::from(s))
} else {
Path(ArcStr::from(canonize(s)))
}
}
/// returns /
pub fn root() -> Path {
// CR estokes: need a good solution for using SEP here
Path::from("/")
}
/// returns true if the path starts with /, false otherwise
pub fn is_absolute<T: AsRef<str> + ?Sized>(p: &T) -> bool {
p.as_ref().starts_with(SEP)
}
/// true if this path is a parent to the specified path. A path is it's own parent.
///
/// # Examples
/// ```
/// use netidx_core::path::Path;
/// assert!(Path::is_parent("/", "/foo/bar/baz"));
/// assert!(Path::is_parent("/foo/bar", "/foo/bar/baz"));
/// assert!(!Path::is_parent("/foo/bar", "/foo/bareth/bazeth"));
/// assert!(Path::is_parent("/foo/bar", "/foo/bar"));
/// ```
pub fn is_parent<T: AsRef<str> + ?Sized, U: AsRef<str> + ?Sized>(
parent: &T,
other: &U,
) -> bool {
let parent = parent.as_ref();
let other = other.as_ref();
parent == "/"
|| (other.starts_with(parent)
&& (other.len() == parent.len()
|| other.as_bytes()[parent.len()] == SEP as u8))
}
/// true if this path is the parent to the specified path, and is
/// exactly 1 level above the specfied path.
///
/// # Examples
/// ```
/// use netidx_core::path::Path;
/// assert!(!Path::is_immediate_parent("/", "/foo/bar/baz"));
/// assert!(Path::is_immediate_parent("/foo/bar", "/foo/bar/baz"));
/// assert!(!Path::is_immediate_parent("/foo/bar", "/foo/bareth/bazeth"));
/// assert!(!Path::is_immediate_parent("/foo/bar", "/foo/bar"));
/// assert!(!Path::is_immediate_parent("/", "/"));
/// ```
pub fn is_immediate_parent<T: AsRef<str> + ?Sized, U: AsRef<str> + ?Sized>(
parent: &T,
other: &U,
) -> bool {
let parent = if parent.as_ref() == "/" { None } else { Some(parent.as_ref()) };
other.as_ref().len() > 0
&& other.as_ref() != "/"
&& Path::dirname(other) == parent
}
/// strips prefix from path at the separator boundry, including
/// the separator. Returns None if prefix is not a parent of path
/// (even if it happens to be a prefix).
pub fn strip_prefix<'a, T: AsRef<str> + ?Sized, U: AsRef<str> + ?Sized>(prefix: &T, path: &'a U) -> Option<&'a str> {
if Path::is_parent(prefix, path) {
path.as_ref().strip_prefix(prefix.as_ref()).map(|s| s.strip_prefix("/").unwrap_or(s))
} else {
None
}
}
/// finds the longest common parent of the two specified paths, /
/// in the case they are completely disjoint.
pub fn lcp<'a, T: AsRef<str> + ?Sized, U: AsRef<str> + ?Sized>(
path0: &'a T,
path1: &'a U,
) -> &'a str {
let (mut p0, p1) = if path0.as_ref().len() <= path1.as_ref().len() {
(path0.as_ref(), path1.as_ref())
} else {
(path1.as_ref(), path0.as_ref())
};
loop {
if Path::is_parent(p0, p1) {
return p0;
} else {
match Path::dirname(p0) {
Some(p) => p0 = p,
None => return "/",
}
}
}
}
/// This will escape the path seperator and the escape character
/// in a path part. If you want to be sure that e.g. `append` will
/// only append 1 level, then you should call this function on
/// your part before appending it.
///
/// # Examples
/// ```
/// use netidx_core::path::Path;
/// assert_eq!("foo\\/bar", &*Path::escape("foo/bar"));
/// assert_eq!("\\\\hello world", &*Path::escape("\\hello world"));
/// ```
pub fn escape<T: AsRef<str> + ?Sized>(s: &T) -> Cow<str> {
utils::escape(s, ESC, &[SEP])
}
/// This will unescape the path seperator and the escape character
/// in a path part.
///
/// # Examples
/// ```
/// use netidx_core::path::Path;
/// assert_eq!("foo/bar", &*Path::unescape("foo\\/bar"));
/// assert_eq!("\\hello world", &*Path::unescape("\\\\hello world"));
/// ```
pub fn unescape<T: AsRef<str> + ?Sized>(s: &T) -> Cow<str> {
let s = s.as_ref();
if !(0..s.len()).into_iter().any(|i| utils::is_escaped(s, ESC, i)) {
Cow::Borrowed(s)
} else {
let mut out = String::with_capacity(s.len());
for (i, c) in s.chars().enumerate() {
if utils::is_escaped(s, ESC, i) {
out.pop();
}
out.push(c);
}
Cow::Owned(out)
}
}
/// return a new path with the specified string appended as a new
/// part separated by the pathsep char.
///
/// # Examples
/// ```
/// use netidx_core::path::Path;
/// let p = Path::root().append("bar").append("baz");
/// assert_eq!(&*p, "/bar/baz");
///
/// let p = Path::root().append("/bar").append("//baz//////foo/");
/// assert_eq!(&*p, "/bar/baz/foo");
/// ```
pub fn append<T: AsRef<str> + ?Sized>(&self, other: &T) -> Self {
let other = other.as_ref();
if other.len() == 0 {
self.clone()
} else {
let mut res = String::with_capacity(self.as_ref().len() + other.len());
res.push_str(self.as_ref());
res.push(SEP);
res.push_str(other);
Path::from(res)
}
}
/// return an iterator over the parts of the path. The path
/// separator may be escaped with \. and a literal \ may be
/// represented as \\.
///
/// # Examples
/// ```
/// use netidx_core::path::Path;
/// let p = Path::from("/foo/bar/baz");
/// assert_eq!(Path::parts(&p).collect::<Vec<_>>(), vec!["foo", "bar", "baz"]);
///
/// let p = Path::from(r"/foo\/bar/baz");
/// assert_eq!(Path::parts(&p).collect::<Vec<_>>(), vec![r"foo\/bar", "baz"]);
///
/// let p = Path::from(r"/foo\\/bar/baz");
/// assert_eq!(Path::parts(&p).collect::<Vec<_>>(), vec![r"foo\\", "bar", "baz"]);
///
/// let p = Path::from(r"/foo\\\/bar/baz");
/// assert_eq!(Path::parts(&p).collect::<Vec<_>>(), vec![r"foo\\\/bar", "baz"]);
/// ```
pub fn parts<T: AsRef<str> + ?Sized>(s: &T) -> impl Iterator<Item = &str> {
let s = s.as_ref();
let skip = if s == "/" {
2
} else if s.starts_with("/") {
1
} else {
0
};
utils::split_escaped(s, ESC, SEP).skip(skip)
}
/// Return an iterator over all the dirnames in the path starting
/// from the root and ending with the entire path.
///
/// # Examples
/// ```
/// use netidx_core::path::Path;
/// let p = Path::from("/some/path/ending/in/foo");
/// let mut bn = Path::dirnames(&p);
/// assert_eq!(bn.next(), Some("/"));
/// assert_eq!(bn.next(), Some("/some"));
/// assert_eq!(bn.next(), Some("/some/path"));
/// assert_eq!(bn.next(), Some("/some/path/ending"));
/// assert_eq!(bn.next(), Some("/some/path/ending/in"));
/// assert_eq!(bn.next(), Some("/some/path/ending/in/foo"));
/// assert_eq!(bn.next(), None);
/// ```
pub fn dirnames<T: AsRef<str> + ?Sized>(s: &T) -> DirNames {
let s = s.as_ref();
if s == "/" {
DirNames::Root(true)
} else {
DirNames::Path { cur: s, all: s, base: 1 }
}
}
/// Return the number of levels in the path.
///
/// # Examples
/// ```
/// use netidx_core::path::Path;
/// let p = Path::from("/foo/bar/baz");
/// assert_eq!(Path::levels(&p), 3);
/// ```
pub fn levels<T: AsRef<str> + ?Sized>(s: &T) -> usize {
let mut p = 0;
for _ in Path::parts(s) {
p += 1
}
p
}
/// return the path without the last part, or return None if the
/// path is empty or /.
///
/// # Examples
/// ```
/// use netidx_core::path::Path;
/// let p = Path::from("/foo/bar/baz");
/// assert_eq!(Path::dirname(&p), Some("/foo/bar"));
///
/// let p = Path::root();
/// assert_eq!(Path::dirname(&p), None);
///
/// let p = Path::from("/foo");
/// assert_eq!(Path::dirname(&p), None);
/// ```
pub fn dirname<'a, T: AsRef<str> + ?Sized>(s: &'a T) -> Option<&'a str> {
let s = s.as_ref();
Path::rfind_sep(s).and_then(|i| if i == 0 { None } else { Some(&s[0..i]) })
}
pub fn dirname_with_sep<T: AsRef<str> + ?Sized>(s: &T) -> Option<&str> {
let s = s.as_ref();
Path::rfind_sep(s).and_then(|i| if i == 0 { None } else { Some(&s[0..i + 1]) })
}
/// return the last part of the path, or return None if the path
/// is empty.
///
/// # Examples
/// ```
/// use netidx_core::path::Path;
/// let p = Path::from("/foo/bar/baz");
/// assert_eq!(Path::basename(&p), Some("baz"));
///
/// let p = Path::from("foo");
/// assert_eq!(Path::basename(&p), Some("foo"));
///
/// let p = Path::from("foo/bar");
/// assert_eq!(Path::basename(&p), Some("bar"));
///
/// let p = Path::from("");
/// assert_eq!(Path::basename(&p), None);
///
/// let p = Path::from("/");
/// assert_eq!(Path::basename(&p), None);
/// ```
pub fn basename<T: AsRef<str> + ?Sized>(s: &T) -> Option<&str> {
let s = s.as_ref();
match Path::rfind_sep(s) {
None => {
if s.len() > 0 {
Some(s)
} else {
None
}
}
Some(i) => {
if s.len() <= 1 {
None
} else {
Some(&s[i + 1..s.len()])
}
}
}
}
fn find_sep_int<F: Fn(&str) -> Option<usize>>(mut s: &str, f: F) -> Option<usize> {
if s.len() == 0 {
None
} else {
loop {
match f(s) {
None => return None,
Some(i) => {
if !utils::is_escaped(s, ESC, i) {
return Some(i);
} else {
s = &s[0..i];
}
}
}
}
}
}
/// return the position of the last path separator in the path, or
/// None if there isn't one.
///
/// # Examples
/// ```
/// use netidx_core::path::Path;
/// let p = Path::from("/foo/bar/baz");
/// assert_eq!(Path::rfind_sep(&p), Some(8));
///
/// let p = Path::from("");
/// assert_eq!(Path::rfind_sep(&p), None);
/// ```
pub fn rfind_sep<T: AsRef<str> + ?Sized>(s: &T) -> Option<usize> {
let s = s.as_ref();
Path::find_sep_int(s, |s| s.rfind(SEP))
}
/// return the position of the first path separator in the path, or
/// None if there isn't one.
///
/// # Examples
/// ```
/// use netidx_core::path::Path;
/// let p = Path::from("foo/bar/baz");
/// assert_eq!(Path::find_sep(&p), Some(3));
///
/// let p = Path::from("");
/// assert_eq!(Path::find_sep(&p), None);
/// ```
pub fn find_sep<T: AsRef<str> + ?Sized>(s: &T) -> Option<usize> {
let s = s.as_ref();
Path::find_sep_int(s, |s| s.find(SEP))
}
}