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 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667
// This is taken and heavily modified of `unix_path`
// which is modified from the standard library.
// source: https://gitlab.com/SnejUgal/unix_path
//
// Permission is hereby granted, free of charge, to any
// person obtaining a copy of this software and associated
// documentation files (the "Software"), to deal in the
// Software without restriction, including without
// limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software
// is furnished to do so, subject to the following
// conditions:
// The above copyright notice and this permission notice
// shall be included in all copies or substantial portions
// of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
// ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
// SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//! A library for parsing, manipulating Paths that follow unix style conventions.
//!
//! This is similar to how the standard library's [`Path`] works, but may have less features.
//!
//! For simplicity, this uses `str` and `String` instead of `OsStr` and `OsString`.
//! Makes it much easier to work with.
use core::{
borrow::Borrow,
cmp, fmt,
hash::{Hash, Hasher},
iter::{self, FusedIterator},
ops::{self, Deref},
str::FromStr,
};
use alloc::{
borrow::{Cow, ToOwned},
boxed::Box,
rc::Rc,
string::{String, ToString},
sync::Arc,
};
/// use unix paths separator
pub const SEPARATOR: char = '/';
pub fn is_separator(c: char) -> bool {
c == SEPARATOR
}
fn is_separator_byte(c: u8) -> bool {
c == SEPARATOR as u8
}
// Iterate through `iter` while it matches `prefix`; return `None` if `prefix`
// is not a prefix of `iter`, otherwise return `Some(iter_after_prefix)` giving
// `iter` after having exhausted `prefix`.
fn iter_after<'a, 'b, I, J>(mut iter: I, mut prefix: J) -> Option<I>
where
I: Iterator<Item = Component<'a>> + Clone,
J: Iterator<Item = Component<'b>>,
{
loop {
let mut iter_next = iter.clone();
match (iter_next.next(), prefix.next()) {
(Some(ref x), Some(ref y)) if x == y => (),
(Some(_), Some(_)) => return None,
(Some(_), None) => return Some(iter),
(None, None) => return Some(iter),
(None, Some(_)) => return None,
}
iter = iter_next;
}
}
/// Check if the first char is `/`
fn has_root(path: &str) -> bool {
!path.is_empty() && path.as_bytes()[0] == b'/'
}
/// Component parsing works by a double-ended state machine; the cursors at the
/// front and back of the path each keep track of what parts of the path have
/// been consumed so far.
///
/// Going front to back, a path is made up of a prefix, a starting
/// directory component, and a body (of normal components)
#[derive(Copy, Clone, PartialEq, PartialOrd, Debug)]
enum State {
Prefix = 0,
StartDir = 1, // / or . or nothing
Body = 2, // foo/bar/baz
Done = 3,
}
/// A single component of a path.
///
/// A `Component` roughly corresponds to a substring between path separators
/// (`/`).
///
/// This `enum` is created by iterating over [`Components`], which in turn is
/// created by the [`components`][`Path::components`] method on [`Path`].
///
/// # Examples
///
/// ```rust
/// let path = Path::new("/tmp/foo/bar.txt");
/// let components = path.components().collect::<Vec<_>>();
/// assert_eq!(&components, &[
/// Component::RootDir,
/// Component::Normal("tmp".as_ref()),
/// Component::Normal("foo".as_ref()),
/// Component::Normal("bar.txt".as_ref()),
/// ]);
/// ```
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Component<'a> {
/// The root directory component, appears after any prefix and before anything else.
///
/// It represents a separator that designates that a path starts from root.
RootDir,
/// A reference to the current directory, i.e., `.`.
CurDir,
/// A reference to the parent directory, i.e., `..`.
ParentDir,
/// A normal component, e.g., `a` and `b` in `a/b`.
///
/// This variant is the most common one, it represents references to files
/// or directories.
Normal(&'a str),
}
impl<'a> Component<'a> {
/// Extracts the underlying `str`.
///
/// # Examples
///
/// ```
/// let path = Path::new("./tmp/foo/bar.txt");
/// let components: Vec<_> = path.components().map(|comp| comp.as_str()).collect();
/// assert_eq!(&components, &[".", "tmp", "foo", "bar.txt"]);
/// ```
pub fn as_str(self) -> &'a str {
match self {
Component::RootDir => "/",
Component::CurDir => ".",
Component::ParentDir => "..",
Component::Normal(path) => path,
}
}
}
impl AsRef<str> for Component<'_> {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl AsRef<Path> for Component<'_> {
fn as_ref(&self) -> &Path {
self.as_str().as_ref()
}
}
/// An iterator over the [`Component`]s of a [`Path`].
///
/// This `struct` is created by the [`components`](Path::components) method on [`Path`].
/// See its documentation for more.
///
/// # Examples
///
/// ```
/// let path = Path::new("/tmp/foo/bar.txt");
///
/// for component in path.components() {
/// println!("{:?}", component);
/// }
/// ```
#[derive(Clone)]
pub struct Components<'a> {
// The path left to parse components from
path: &'a str,
// true if path *physically* has a root separator;.
has_root: bool,
// The iterator is double-ended, and these two states keep track of what has
// been produced from either end
front: State,
back: State,
}
/// An iterator over the [`Component`]s of a [`Path`], as [`str`] slices.
///
/// This `struct` is created by the [`iter`] method on [`Path`].
/// See its documentation for more.
#[derive(Clone)]
pub struct Iter<'a> {
inner: Components<'a>,
}
impl fmt::Debug for Components<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
struct DebugHelper<'a>(&'a Path);
impl fmt::Debug for DebugHelper<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_list().entries(self.0.components()).finish()
}
}
f.debug_tuple("Components")
.field(&DebugHelper(self.as_path()))
.finish()
}
}
impl<'a> Components<'a> {
// Given the iteration so far, how much of the pre-State::Body path is left?
#[inline]
fn len_before_body(&self) -> usize {
let root = if self.front <= State::StartDir && self.has_root {
1
} else {
0
};
let cur_dir = if self.front <= State::StartDir && self.include_cur_dir() {
1
} else {
0
};
root + cur_dir
}
// is the iteration complete?
#[inline]
fn finished(&self) -> bool {
self.front == State::Done || self.back == State::Done || self.front > self.back
}
/// Extracts a slice corresponding to the portion of the path remaining for iteration.
///
/// # Examples
///
/// ```
/// let mut components = Path::new("/tmp/foo/bar.txt").components();
/// components.next();
/// components.next();
///
/// assert_eq!(Path::new("foo/bar.txt"), components.as_path());
/// ```
pub fn as_path(&self) -> &'a Path {
let mut comps = self.clone();
if comps.front == State::Body {
comps.trim_left();
}
if comps.back == State::Body {
comps.trim_right();
}
Path::new(comps.path)
}
/// Is the *original* path rooted?
fn has_root(&self) -> bool {
self.has_root
}
/// Should the normalized path include a leading . ?
fn include_cur_dir(&self) -> bool {
if self.has_root() {
return false;
}
let mut iter = self.path[..].chars();
match (iter.next(), iter.next()) {
(Some('.'), None) => true,
(Some('.'), Some(c)) => is_separator(c),
_ => false,
}
}
// parse a given byte sequence into the corresponding path component
fn parse_single_component<'b>(&self, comp: &'b str) -> Option<Component<'b>> {
match comp {
"." => None, // . components are normalized away, except at
// the beginning of a path, which is treated
// separately via `include_cur_dir`
".." => Some(Component::ParentDir),
"" => None,
_ => Some(Component::Normal(comp)),
}
}
// parse a component from the left, saying how many bytes to consume to
// remove the component
fn parse_next_component(&self) -> (usize, Option<Component<'a>>) {
debug_assert!(self.front == State::Body);
let (extra, comp) = match self.path.chars().position(is_separator) {
None => (0, self.path),
Some(i) => (1, &self.path[..i]),
};
(comp.len() + extra, self.parse_single_component(comp))
}
// parse a component from the right, saying how many bytes to consume to
// remove the component
fn parse_next_component_back(&self) -> (usize, Option<Component<'a>>) {
debug_assert!(self.back == State::Body);
let start = self.len_before_body();
// FIXME: should be `chars`, but its easier this way
let (extra, comp) = match self.path[start..].bytes().rposition(is_separator_byte) {
None => (0, &self.path[start..]),
Some(i) => (1, &self.path[start + i + 1..]),
};
(comp.len() + extra, self.parse_single_component(comp))
}
// trim away repeated separators (i.e., empty components) on the left
fn trim_left(&mut self) {
while !self.path.is_empty() {
let (size, comp) = self.parse_next_component();
if comp.is_some() {
return;
} else {
self.path = &self.path[size..];
}
}
}
// trim away repeated separators (i.e., empty components) on the right
fn trim_right(&mut self) {
while self.path.len() > self.len_before_body() {
let (size, comp) = self.parse_next_component_back();
if comp.is_some() {
return;
} else {
self.path = &self.path[..self.path.len() - size];
}
}
}
pub fn peek(&self) -> Option<Component<'a>> {
// this is a lie, but it works, the clone cost isn't very high, so should be okay
self.clone().next()
}
}
impl AsRef<Path> for Components<'_> {
fn as_ref(&self) -> &Path {
self.as_path()
}
}
impl AsRef<str> for Components<'_> {
fn as_ref(&self) -> &str {
self.as_path().as_str()
}
}
impl fmt::Debug for Iter<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
struct DebugHelper<'a>(&'a Path);
impl fmt::Debug for DebugHelper<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_list().entries(self.0.iter()).finish()
}
}
f.debug_tuple("Iter")
.field(&DebugHelper(self.as_path()))
.finish()
}
}
impl<'a> Iter<'a> {
/// Extracts a slice corresponding to the portion of the path remaining for iteration.
///
/// # Examples
///
/// ```
/// let mut iter = Path::new("/tmp/foo/bar.txt").iter();
/// iter.next();
/// iter.next();
///
/// assert_eq!(Path::new("foo/bar.txt"), iter.as_path());
/// ```
pub fn as_path(&self) -> &'a Path {
self.inner.as_path()
}
}
impl AsRef<Path> for Iter<'_> {
fn as_ref(&self) -> &Path {
self.as_path()
}
}
impl AsRef<str> for Iter<'_> {
fn as_ref(&self) -> &str {
self.as_path().as_str()
}
}
impl<'a> Iterator for Iter<'a> {
type Item = &'a str;
fn next(&mut self) -> Option<Self::Item> {
self.inner.next().map(Component::as_str)
}
}
impl<'a> DoubleEndedIterator for Iter<'a> {
fn next_back(&mut self) -> Option<Self::Item> {
self.inner.next_back().map(Component::as_str)
}
}
impl FusedIterator for Iter<'_> {}
impl<'a> Iterator for Components<'a> {
type Item = Component<'a>;
fn next(&mut self) -> Option<Component<'a>> {
while !self.finished() {
match self.front {
State::Prefix => {
self.front = State::StartDir;
}
State::StartDir => {
self.front = State::Body;
if self.has_root {
debug_assert!(!self.path.is_empty());
self.path = &self.path[1..];
return Some(Component::RootDir);
} else if self.include_cur_dir() {
debug_assert!(!self.path.is_empty());
self.path = &self.path[1..];
return Some(Component::CurDir);
}
}
State::Body if !self.path.is_empty() => {
let (size, comp) = self.parse_next_component();
self.path = &self.path[size..];
if comp.is_some() {
return comp;
}
}
State::Body => {
self.front = State::Done;
}
State::Done => unreachable!(),
}
}
None
}
}
impl<'a> DoubleEndedIterator for Components<'a> {
fn next_back(&mut self) -> Option<Component<'a>> {
while !self.finished() {
match self.back {
State::Body if self.path.len() > self.len_before_body() => {
let (size, comp) = self.parse_next_component_back();
self.path = &self.path[..self.path.len() - size];
if comp.is_some() {
return comp;
}
}
State::Body => {
self.back = State::StartDir;
}
State::StartDir => {
self.back = State::Prefix;
if self.has_root {
self.path = &self.path[..self.path.len() - 1];
return Some(Component::RootDir);
} else if self.include_cur_dir() {
self.path = &self.path[..self.path.len() - 1];
return Some(Component::CurDir);
}
}
State::Prefix => {
self.back = State::Done;
return None;
}
State::Done => unreachable!(),
}
}
None
}
}
impl FusedIterator for Components<'_> {}
impl<'a> cmp::PartialEq for Components<'a> {
fn eq(&self, other: &Components<'a>) -> bool {
Iterator::eq(self.clone(), other.clone())
}
}
impl cmp::Eq for Components<'_> {}
impl<'a> cmp::PartialOrd for Components<'a> {
fn partial_cmp(&self, other: &Components<'a>) -> Option<cmp::Ordering> {
Some(self.cmp(other))
}
}
impl cmp::Ord for Components<'_> {
fn cmp(&self, other: &Self) -> cmp::Ordering {
Iterator::cmp(self.clone(), other.clone())
}
}
/// An iterator over [`Path`] and its ancestors.
///
/// This `struct` is created by the [`Path::ancestors`] method on [`Path`].
/// See its documentation for more.
///
/// # Examples
///
/// ```
/// let path = Path::new("/foo/bar");
///
/// for ancestor in path.ancestors() {
/// println!("{:?}", ancestor);
/// }
/// ```
#[derive(Copy, Clone, Debug)]
pub struct Ancestors<'a> {
next: Option<&'a Path>,
}
impl<'a> Iterator for Ancestors<'a> {
type Item = &'a Path;
fn next(&mut self) -> Option<Self::Item> {
let next = self.next;
self.next = next.and_then(Path::parent);
next
}
}
impl FusedIterator for Ancestors<'_> {}
/// An owned, mutable path (akin to `String`).
///
/// This type provides methods like [`PathBuf::push`] that mutate
/// the path in place. It also implements `Deref` to [`Path`], meaning that
/// all methods on [`Path`] slices are available on `PathBuf` values as well.
///
/// More details about the overall approach can be found in
/// the [crate documentation](index.html).
///
/// # Examples
///
/// You can use [`PathBuf::push`] to build up a [`PathBuf`] from
/// components:
///
/// ```
/// let mut path = PathBuf::new();
///
/// path.push("/");
/// path.push("feel");
/// path.push("the");
/// ```
///
/// However, [`PathBuf::push`] is best used for dynamic situations. This is a better way
/// to do this when you know all of the components ahead of time:
///
/// ```
/// let path: PathBuf = ["/", "feel", "the.force"].iter().collect();
/// ```
///
/// We can still do better than this! Since these are all strings, we can use
/// [`From::from`]:
///
/// ```
/// let path = PathBuf::from(r"/feel/the.force");
/// ```
///
/// Which method works best depends on what kind of situation you're in.
#[derive(Clone)]
pub struct PathBuf {
inner: String,
}
impl PathBuf {
/// Allocates an empty [`PathBuf`].
///
/// # Examples
///
/// ```
/// let path = PathBuf::new();
/// ```
pub fn new() -> PathBuf {
PathBuf {
inner: String::new(),
}
}
/// Creates a new [`PathBuf`] with a given capacity used to create the
/// internal [`String`]. See [`String::with_capacity`].
///
/// # Examples
///
/// ```
/// let mut path = PathBuf::with_capacity(10);
/// let capacity = path.capacity();
///
/// // This push is done without reallocating
/// path.push("/");
///
/// assert_eq!(capacity, path.capacity());
/// ```
pub fn with_capacity(capacity: usize) -> PathBuf {
PathBuf {
inner: String::with_capacity(capacity),
}
}
/// Coerces to a [`Path`] slice.
///
/// [`Path`]: struct.Path.html
///
/// # Examples
///
/// ```
/// let p = PathBuf::from("/test");
/// assert_eq!(Path::new("/test"), p.as_path());
/// ```
pub fn as_path(&self) -> &Path {
self
}
/// Extends `self` with `path`.
///
/// If `path` is absolute, it replaces the current path.
///
/// # Examples
///
/// Pushing a relative path extends the existing path:
///
/// ```
/// let mut path = PathBuf::from("/tmp");
/// path.push("file.bk");
/// assert_eq!(path, PathBuf::from("/tmp/file.bk"));
/// ```
///
/// Pushing an absolute path replaces the existing path:
///
/// ```
/// let mut path = PathBuf::from("/tmp");
/// path.push("/etc");
/// assert_eq!(path, PathBuf::from("/etc"));
/// ```
pub fn push<P: AsRef<Path>>(&mut self, path: P) {
self._push(path.as_ref())
}
fn _push(&mut self, path: &Path) {
// in general, a separator is needed if the rightmost byte is not a separator
let need_sep = self
.as_str()
.chars()
.last()
.map(|c| !is_separator(c))
.unwrap_or(false);
// absolute `path` replaces `self`
if path.is_absolute() || path.has_root() {
self.inner.clear();
} else if need_sep {
self.inner.push(SEPARATOR);
}
self.inner.push_str(path.as_str());
}
/// Truncates `self` to [`self.parent`].
///
/// Returns `false` and does nothing if [`self.parent`] is `None`.
/// Otherwise, returns `true`.
///
/// [`self.parent`]: struct.PathBuf.html#method.parent
///
/// # Examples
///
/// ```
/// let mut p = PathBuf::from("/test/test.rs");
///
/// p.pop();
/// assert_eq!(Path::new("/test"), p);
/// p.pop();
/// assert_eq!(Path::new("/"), p);
/// ```
pub fn pop(&mut self) -> bool {
match self.parent().map(|p| p.as_str().len()) {
Some(len) => {
self.inner.truncate(len);
true
}
None => false,
}
}
/// Updates [`self.file_name`] to `file_name`.
///
/// If [`self.file_name`] was `None`, this is equivalent to pushing
/// `file_name`.
///
/// Otherwise it is equivalent to calling [`pop`] and then pushing
/// `file_name`. The new path will be a sibling of the original path.
/// (That is, it will have the same parent.)
///
/// [`self.file_name`]: PathBuf
/// [`pop`]: PathBuf::pop
///
/// # Examples
///
/// ```
/// let mut buf = PathBuf::from("/");
/// assert!(buf.file_name() == None);
/// buf.set_file_name("bar");
/// assert!(buf == PathBuf::from("/bar"));
/// assert!(buf.file_name().is_some());
/// buf.set_file_name("baz.txt");
/// assert!(buf == PathBuf::from("/baz.txt"));
/// ```
pub fn set_file_name<S: AsRef<str>>(&mut self, file_name: S) {
self._set_file_name(file_name.as_ref())
}
fn _set_file_name(&mut self, file_name: &str) {
if self.file_name().is_some() {
let popped = self.pop();
debug_assert!(popped);
}
self.push(file_name);
}
/// Consumes the `PathBuf`, yielding its internal `String` storage.
///
/// # Examples
///
/// ```
/// let p = PathBuf::from("/the/head");
/// let bytes = p.into_string();
/// ```
pub fn into_string(self) -> String {
self.inner
}
/// Converts this `PathBuf` into a boxed [`Path`].
///
/// [`Path`]: struct.Path.html
pub fn into_boxed_path(self) -> Box<Path> {
let rw = Box::into_raw(self.inner.into_boxed_str()) as *mut Path;
unsafe { Box::from_raw(rw) }
}
/// Invokes `capacity` on the underlying instance of `String`.
pub fn capacity(&self) -> usize {
self.inner.capacity()
}
/// Invokes `clear` on the underlying instance of `String`.
pub fn clear(&mut self) {
self.inner.clear()
}
/// Invokes `reserve` on the underlying instance of `String`.
pub fn reserve(&mut self, additional: usize) {
self.inner.reserve(additional)
}
/// Invokes `reserve_exact` on the underlying instance of `String`.
pub fn reserve_exact(&mut self, additional: usize) {
self.inner.reserve_exact(additional)
}
/// Invokes `shrink_to_fit` on the underlying instance of `String`.
pub fn shrink_to_fit(&mut self) {
self.inner.shrink_to_fit()
}
/// Invokes `shrink_to` on the underlying instance of `String`.
pub fn shrink_to(&mut self, min_capacity: usize) {
self.inner.shrink_to(min_capacity)
}
}
impl From<&Path> for Box<Path> {
fn from(path: &Path) -> Box<Path> {
path.to_path_buf().into_boxed_path()
}
}
impl From<Cow<'_, Path>> for Box<Path> {
#[inline]
fn from(cow: Cow<'_, Path>) -> Box<Path> {
match cow {
Cow::Borrowed(path) => Box::from(path),
Cow::Owned(path) => Box::from(path),
}
}
}
impl From<PathBuf> for Box<Path> {
/// Converts a `PathBuf` into a `Box<Path>`
///
/// This conversion currently should not allocate memory,
/// but this behavior is not guaranteed in all future versions.
fn from(p: PathBuf) -> Self {
p.into_boxed_path()
}
}
impl Clone for Box<Path> {
#[inline]
fn clone(&self) -> Self {
self.to_path_buf().into_boxed_path()
}
}
impl<T: ?Sized + AsRef<str>> From<&T> for PathBuf {
fn from(s: &T) -> Self {
PathBuf::from(s.as_ref().to_string())
}
}
impl From<String> for PathBuf {
/// Converts a [`String`] into a [`PathBuf`]
///
/// This conversion does not allocate or copy memory.
#[inline]
fn from(s: String) -> Self {
PathBuf { inner: s }
}
}
impl From<PathBuf> for String {
/// Converts a [`PathBuf`] into a [`String`]
///
/// This conversion does not allocate or copy memory.
fn from(path_buf: PathBuf) -> Self {
path_buf.inner
}
}
impl FromStr for PathBuf {
type Err = core::convert::Infallible;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(PathBuf::from(s))
}
}
impl<P: AsRef<Path>> iter::FromIterator<P> for PathBuf {
fn from_iter<I: IntoIterator<Item = P>>(iter: I) -> PathBuf {
let mut buf = PathBuf::new();
buf.extend(iter);
buf
}
}
impl<P: AsRef<Path>> iter::Extend<P> for PathBuf {
fn extend<I: IntoIterator<Item = P>>(&mut self, iter: I) {
iter.into_iter().for_each(move |p| self.push(p.as_ref()));
}
}
impl fmt::Debug for PathBuf {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&**self, formatter)
}
}
impl ops::Deref for PathBuf {
type Target = Path;
#[inline]
fn deref(&self) -> &Path {
Path::new(&self.inner)
}
}
impl Borrow<Path> for PathBuf {
fn borrow(&self) -> &Path {
self.deref()
}
}
impl Default for PathBuf {
fn default() -> Self {
PathBuf::new()
}
}
impl<'a> From<&'a Path> for Cow<'a, Path> {
#[inline]
fn from(s: &'a Path) -> Cow<'a, Path> {
Cow::Borrowed(s)
}
}
impl<'a> From<PathBuf> for Cow<'a, Path> {
#[inline]
fn from(s: PathBuf) -> Cow<'a, Path> {
Cow::Owned(s)
}
}
impl<'a> From<&'a PathBuf> for Cow<'a, Path> {
#[inline]
fn from(p: &'a PathBuf) -> Cow<'a, Path> {
Cow::Borrowed(p.as_path())
}
}
impl<'a> From<Cow<'a, Path>> for PathBuf {
#[inline]
fn from(p: Cow<'a, Path>) -> Self {
p.into_owned()
}
}
impl From<PathBuf> for Arc<Path> {
/// Converts a `PathBuf` into an `Arc` by moving the `PathBuf` data into a new `Arc` buffer.
#[inline]
fn from(s: PathBuf) -> Arc<Path> {
let arc: Arc<str> = Arc::from(s.into_string());
unsafe { Arc::from_raw(Arc::into_raw(arc) as *const Path) }
}
}
impl From<&Path> for Arc<Path> {
/// Converts a `Path` into an `Arc` by copying the `Path` data into a new `Arc` buffer.
#[inline]
fn from(s: &Path) -> Arc<Path> {
let arc: Arc<str> = Arc::from(s.as_str());
unsafe { Arc::from_raw(Arc::into_raw(arc) as *const Path) }
}
}
impl From<PathBuf> for Rc<Path> {
/// Converts a `PathBuf` into an `Rc` by moving the `PathBuf` data into a new `Rc` buffer.
#[inline]
fn from(s: PathBuf) -> Rc<Path> {
let rc: Rc<str> = Rc::from(s.into_string());
unsafe { Rc::from_raw(Rc::into_raw(rc) as *const Path) }
}
}
impl From<&Path> for Rc<Path> {
/// Converts a `Path` into an `Rc` by copying the `Path` data into a new `Rc` buffer.
#[inline]
fn from(s: &Path) -> Rc<Path> {
let rc: Rc<str> = Rc::from(s.as_str());
unsafe { Rc::from_raw(Rc::into_raw(rc) as *const Path) }
}
}
impl ToOwned for Path {
type Owned = PathBuf;
fn to_owned(&self) -> PathBuf {
self.to_path_buf()
}
}
impl cmp::PartialEq for PathBuf {
fn eq(&self, other: &PathBuf) -> bool {
self.components() == other.components()
}
}
impl Hash for PathBuf {
fn hash<H: Hasher>(&self, h: &mut H) {
self.as_path().hash(h)
}
}
impl cmp::Eq for PathBuf {}
impl cmp::PartialOrd for PathBuf {
fn partial_cmp(&self, other: &PathBuf) -> Option<cmp::Ordering> {
Some(self.cmp(other))
}
}
impl cmp::Ord for PathBuf {
fn cmp(&self, other: &PathBuf) -> cmp::Ordering {
self.components().cmp(other.components())
}
}
impl AsRef<str> for PathBuf {
fn as_ref(&self) -> &str {
&self.inner[..]
}
}
/// A slice of a path (akin to `str`).
///
/// This type supports a number of operations for inspecting a path, including
/// breaking the path into its components (separated by `/` ), extracting the
/// file name, determining whether the path is absolute, and so on.
///
/// This is an *unsized* type, meaning that it must always be used behind a
/// pointer like `&` or `Box`. For an owned version of this type,
/// see [`PathBuf`].
///
/// More details about the overall approach can be found in
/// the [crate documentation](index.html).
///
/// # Examples
///
/// ```
/// let path = Path::new("./foo/bar.txt");
///
/// let parent = path.parent();
/// assert_eq!(parent, Some(Path::new("./foo")));
/// ```
#[repr(transparent)]
pub struct Path {
inner: str,
}
/// An error returned from [`Path::strip_prefix`][`strip_prefix`] if the prefix
/// was not found.
///
/// This `struct` is created by the [`strip_prefix`] method on [`Path`].
/// See its documentation for more.
///
/// [`strip_prefix`]: struct.Path.html#method.strip_prefix
/// [`Path`]: struct.Path.html
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StripPrefixError(());
impl Path {
/// Directly wraps a string slice as a `Path` slice.
///
/// This is a cost-free conversion.
///
/// # Examples
///
/// ```
/// Path::new("foo.txt");
/// ```
///
/// You can create `Path`s from `String`s, or even other `Path`s:
///
/// ```
/// let string = String::from("foo.txt");
/// let from_string = Path::new(&string);
/// let from_path = Path::new(&from_string);
/// assert_eq!(from_string, from_path);
/// ```
pub fn new<S: AsRef<str> + ?Sized>(s: &S) -> &Path {
unsafe { &*(s.as_ref() as *const str as *const Path) }
}
/// Yields the underlying bytes.
///
/// # Examples
///
/// ```
/// let os_str = Path::new("foo.txt").as_str();
/// assert_eq!(os_str, "foo.txt");
/// ```
pub fn as_str(&self) -> &str {
&self.inner
}
/// Yields a `&str` slice if the `Path` is valid unicode.
///
/// This conversion may entail doing a check for UTF-8 validity.
/// Note that validation is performed because non-UTF-8 strings are
/// perfectly valid for some OS.
///
/// # Examples
///
/// ```
/// let path = Path::new("foo.txt");
/// assert_eq!(path.to_str(), Some("foo.txt"));
/// ```
pub fn to_str(&self) -> Option<&str> {
Some(self.as_str())
}
/// Converts a `Path` to a `Cow<str>`.
///
/// Any non-Unicode sequences are replaced with
/// `U+FFFD REPLACEMENT CHARACTER`.
///
///
/// # Examples
///
/// Calling `to_string_lossy` on a `Path` with valid unicode:
///
/// ```
/// let path = Path::new("foo.txt");
/// assert_eq!(path.to_string_lossy(), "foo.txt");
/// ```
///
/// Had `path` contained invalid unicode, the `to_string_lossy` call might
/// have returned `"fo�.txt"`.
pub fn to_string_lossy(&self) -> Cow<'_, str> {
Cow::Borrowed(&self.inner)
}
/// Converts a `Path` to an owned [`PathBuf`].
///
/// [`PathBuf`]: struct.PathBuf.html
///
/// # Examples
///
/// ```
/// let path_buf = Path::new("foo.txt").to_path_buf();
/// assert_eq!(path_buf, PathBuf::from("foo.txt"));
/// ```
pub fn to_path_buf(&self) -> PathBuf {
PathBuf::from(&self.inner)
}
/// Returns `true` if the `Path` is absolute, i.e., if it is independent of
/// the current directory.
///
/// A path is absolute if it starts with the root, so `is_absolute` and
/// [`has_root`] are equivalent.
///
/// # Examples
///
/// ```
/// assert!(!Path::new("foo.txt").is_absolute());
/// ```
pub fn is_absolute(&self) -> bool {
self.has_root()
}
/// Returns `true` if the `Path` is relative, i.e., not absolute.
///
/// See [`is_absolute`]'s documentation for more details.
///
/// # Examples
///
/// ```
/// assert!(Path::new("foo.txt").is_relative());
/// ```
///
/// [`is_absolute`]: #method.is_absolute
pub fn is_relative(&self) -> bool {
!self.is_absolute()
}
/// Returns `true` if the `Path` has a root.
///
/// A path has a root if it begins with `/`.
///
/// # Examples
///
/// ```
/// assert!(Path::new("/etc/passwd").has_root());
/// ```
pub fn has_root(&self) -> bool {
self.components().has_root()
}
/// Checks if the path ends with a separator.
///
/// This function checks if the last character of the path string is a separator character.
/// If the path is empty, it returns `false`.
///
/// # Returns
///
/// * `true` if the path ends with a separator.
/// * `false` if the path does not end with a separator or if the path is empty.
///
/// # Examples
///
/// ```
/// use your_module::Path;
///
/// let p = Path::new("/some/path/");
/// assert_eq!(p.has_last_separator(), true);
///
/// let p = Path::new("/some/path");
/// assert_eq!(p.has_last_separator(), false);
/// ```
pub fn has_last_separator(&self) -> bool {
self.as_str()
.chars()
.last()
.map(is_separator)
.unwrap_or(false)
}
/// Checks if the path is the root directory.
///
/// This function compares the path string with "/", the root directory.
/// If the path is "/", it returns `true`. For any other path, it returns `false`.
///
/// # Returns
///
/// * `true` if the path is the root directory ("/").
/// * `false` if the path is not the root directory.
///
/// # Examples
///
/// ```
/// use your_module::Path;
///
/// let p = Path::new("/");
/// assert_eq!(p.is_root(), true);
///
/// let p = Path::new("/some/path");
/// assert_eq!(p.is_root(), false);
/// ```
pub fn is_root(&self) -> bool {
self.as_str() == "/"
}
/// Checks if the path is empty.
///
/// This function checks if the path string is empty. If the path is an empty string, it returns `true`.
/// For any non-empty path, it returns `false`.
///
/// # Returns
///
/// * `true` if the path is an empty string.
/// * `false` if the path is not an empty string.
///
/// # Examples
///
/// ```
/// use your_module::Path;
///
/// let p = Path::new("");
/// assert_eq!(p.is_empty(), true);
///
/// let p = Path::new("/some/path");
/// assert_eq!(p.is_empty(), false);
/// ```
pub fn is_empty(&self) -> bool {
self.as_str().is_empty()
}
/// Returns the `Path` without its final component, if there is one.
///
/// Returns `None` if the path terminates in a root or prefix.
///
/// # Examples
///
/// ```
/// let path = Path::new("/foo/bar");
/// let parent = path.parent().unwrap();
/// assert_eq!(parent, Path::new("/foo"));
///
/// let grand_parent = parent.parent().unwrap();
/// assert_eq!(grand_parent, Path::new("/"));
/// assert_eq!(grand_parent.parent(), None);
/// ```
pub fn parent(&self) -> Option<&Path> {
let mut comps = self.components();
let comp = comps.next_back();
comp.and_then(|p| match p {
Component::Normal(_) | Component::CurDir | Component::ParentDir => {
Some(comps.as_path())
}
_ => None,
})
}
/// Produces an iterator over `Path` and its ancestors.
///
/// The iterator will yield the `Path` that is returned if the [`parent`] method is used zero
/// or more times. That means, the iterator will yield `&self`, `&self.parent().unwrap()`,
/// `&self.parent().unwrap().parent().unwrap()` and so on. If the [`parent`] method returns
/// `None`, the iterator will do likewise. The iterator will always yield at least one value,
/// namely `&self`.
///
/// # Examples
///
/// ```
/// let mut ancestors = Path::new("/foo/bar").ancestors();
/// assert_eq!(ancestors.next(), Some(Path::new("/foo/bar")));
/// assert_eq!(ancestors.next(), Some(Path::new("/foo")));
/// assert_eq!(ancestors.next(), Some(Path::new("/")));
/// assert_eq!(ancestors.next(), None);
/// ```
///
/// [`parent`]: struct.Path.html#method.parent
pub fn ancestors(&self) -> Ancestors<'_> {
Ancestors { next: Some(self) }
}
/// Returns the final component of the `Path`, if there is one.
///
/// If the path is a normal file, this is the file name. If it's the path of a directory, this
/// is the directory name.
///
/// Returns `None` if the path terminates in `..`.
///
/// # Examples
///
/// ```
/// assert_eq!(Some("bin"), Path::new("/usr/bin/").file_name());
/// assert_eq!(Some("foo.txt"), Path::new("tmp/foo.txt").file_name());
/// assert_eq!(Some("foo.txt"), Path::new("foo.txt/.").file_name());
/// assert_eq!(Some("foo.txt"), Path::new("foo.txt/.//").file_name());
/// assert_eq!(None, Path::new("foo.txt/..").file_name());
/// assert_eq!(None, Path::new("/").file_name());
/// ```
pub fn file_name(&self) -> Option<&str> {
self.components().next_back().and_then(|p| match p {
Component::Normal(p) => Some(p),
_ => None,
})
}
/// Returns a path that, when joined onto `base`, yields `self`.
///
/// # Errors
///
/// If `base` is not a prefix of `self` (i.e., [`starts_with`]
/// returns `false`), returns `Err`.
///
/// [`starts_with`]: #method.starts_with
///
/// # Examples
///
/// ```
/// let path = Path::new("/test/haha/foo.txt");
///
/// assert_eq!(path.strip_prefix("/"), Ok(Path::new("test/haha/foo.txt")));
/// assert_eq!(path.strip_prefix("/test"), Ok(Path::new("haha/foo.txt")));
/// assert_eq!(path.strip_prefix("/test/"), Ok(Path::new("haha/foo.txt")));
/// assert_eq!(path.strip_prefix("/test/haha/foo.txt"), Ok(Path::new("")));
/// assert_eq!(path.strip_prefix("/test/haha/foo.txt/"), Ok(Path::new("")));
/// assert_eq!(path.strip_prefix("test").is_ok(), false);
/// assert_eq!(path.strip_prefix("/haha").is_ok(), false);
///
/// let prefix = PathBuf::from("/test/");
/// assert_eq!(path.strip_prefix(prefix), Ok(Path::new("haha/foo.txt")));
/// ```
pub fn strip_prefix<P>(&self, base: P) -> Result<&Path, StripPrefixError>
where
P: AsRef<Path>,
{
self._strip_prefix(base.as_ref())
}
fn _strip_prefix(&self, base: &Path) -> Result<&Path, StripPrefixError> {
iter_after(self.components(), base.components())
.map(|c| c.as_path())
.ok_or(StripPrefixError(()))
}
/// Determines whether `base` is a prefix of `self`.
///
/// Only considers whole path components to match.
///
/// # Examples
///
/// ```
/// let path = Path::new("/etc/passwd");
///
/// assert!(path.starts_with("/etc"));
/// assert!(path.starts_with("/etc/"));
/// assert!(path.starts_with("/etc/passwd"));
/// assert!(path.starts_with("/etc/passwd/"));
///
/// assert!(!path.starts_with("/e"));
/// ```
pub fn starts_with<P: AsRef<Path>>(&self, base: P) -> bool {
self._starts_with(base.as_ref())
}
fn _starts_with(&self, base: &Path) -> bool {
iter_after(self.components(), base.components()).is_some()
}
/// Determines whether `child` is a suffix of `self`.
///
/// Only considers whole path components to match.
///
/// # Examples
///
/// ```
/// let path = Path::new("/etc/passwd");
///
/// assert!(path.ends_with("passwd"));
/// ```
pub fn ends_with<P: AsRef<Path>>(&self, child: P) -> bool {
self._ends_with(child.as_ref())
}
fn _ends_with(&self, child: &Path) -> bool {
iter_after(self.components().rev(), child.components().rev()).is_some()
}
/// Creates an owned [`PathBuf`] with `path` adjoined to `self`.
///
/// See [`PathBuf::push`] for more details on what it means to adjoin a path.
///
/// [`PathBuf`]: struct.PathBuf.html
/// [`PathBuf::push`]: struct.PathBuf.html#method.push
///
/// # Examples
///
/// ```
/// assert_eq!(Path::new("/etc").join("passwd"), PathBuf::from("/etc/passwd"));
/// ```
#[must_use]
pub fn join<P: AsRef<Path>>(&self, path: P) -> PathBuf {
self._join(path.as_ref())
}
fn _join(&self, path: &Path) -> PathBuf {
let mut buf = self.to_path_buf();
buf.push(path);
buf
}
/// Creates an owned [`PathBuf`] like `self` but with the given file name.
///
/// See [`PathBuf::set_file_name`] for more details.
///
/// [`PathBuf`]: struct.PathBuf.html
/// [`PathBuf::set_file_name`]: struct.PathBuf.html#method.set_file_name
///
/// # Examples
///
/// ```
/// let path = Path::new("/tmp/foo.txt");
/// assert_eq!(path.with_file_name("bar.txt"), PathBuf::from("/tmp/bar.txt"));
///
/// let path = Path::new("/tmp");
/// assert_eq!(path.with_file_name("var"), PathBuf::from("/var"));
/// ```
pub fn with_file_name<S: AsRef<str>>(&self, file_name: S) -> PathBuf {
self._with_file_name(file_name.as_ref())
}
fn _with_file_name(&self, file_name: &str) -> PathBuf {
let mut buf = self.to_path_buf();
buf.set_file_name(file_name);
buf
}
/// Produces an iterator over the [`Component`]s of the path.
///
/// When parsing the path, there is a small amount of normalization:
///
/// * Repeated separators are ignored, so `a/b` and `a//b` both have
/// `a` and `b` as components.
///
/// * Occurrences of `.` are normalized away, except if they are at the
/// beginning of the path. For example, `a/./b`, `a/b/`, `a/b/.` and
/// `a/b` all have `a` and `b` as components, but `./a/b` starts with
/// an additional [`Component::CurDir`] component.
///
/// * A trailing slash is normalized away, `/a/b` and `/a/b/` are equivalent.
///
/// Note that no other normalization takes place; in particular, `a/c`
/// and `a/b/../c` are distinct, to account for the possibility that `b`
/// is a symbolic link (so its parent isn't `a`).
///
/// # Examples
///
/// ```
/// let mut components = Path::new("/tmp/foo.txt").components();
///
/// assert_eq!(components.next(), Some(Component::RootDir));
/// assert_eq!(components.next(), Some(Component::Normal("tmp")));
/// assert_eq!(components.next(), Some(Component::Normal("foo.txt")));
/// assert_eq!(components.next(), None)
/// ```
pub fn components(&self) -> Components<'_> {
Components {
path: &self.inner,
has_root: has_root(&self.inner),
front: State::Prefix,
back: State::Body,
}
}
/// Produces an iterator over the path's components viewed as `str`
/// slices.
///
/// For more information about the particulars of how the path is separated
/// into components, see [`components`].
///
/// [`components`]: #method.components
///
/// # Examples
///
/// ```
/// let mut it = Path::new("/tmp/foo.txt").iter();
/// assert_eq!(it.next(), Some("/"));
/// assert_eq!(it.next(), Some("tmp"));
/// assert_eq!(it.next(), Some("foo.txt"));
/// assert_eq!(it.next(), None)
/// ```
pub fn iter(&self) -> Iter<'_> {
Iter {
inner: self.components(),
}
}
/// Returns a newtype that implements Display for safely printing paths
/// that may contain non-Unicode data.
pub fn display(&self) -> Display<'_> {
Display { path: self }
}
}
impl AsRef<str> for Path {
fn as_ref(&self) -> &str {
&self.inner
}
}
impl fmt::Debug for Path {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.inner, formatter)
}
}
impl cmp::PartialEq for Path {
fn eq(&self, other: &Path) -> bool {
self.components().eq(other.components())
}
}
impl Hash for Path {
fn hash<H: Hasher>(&self, h: &mut H) {
for component in self.components() {
component.hash(h);
}
}
}
impl cmp::Eq for Path {}
impl cmp::PartialOrd for Path {
fn partial_cmp(&self, other: &Path) -> Option<cmp::Ordering> {
Some(self.cmp(other))
}
}
impl cmp::Ord for Path {
fn cmp(&self, other: &Path) -> cmp::Ordering {
self.components().cmp(other.components())
}
}
impl AsRef<Path> for Path {
fn as_ref(&self) -> &Path {
self
}
}
impl AsRef<Path> for str {
#[inline]
fn as_ref(&self) -> &Path {
Path::new(self)
}
}
impl AsRef<Path> for String {
fn as_ref(&self) -> &Path {
Path::new(self)
}
}
impl AsRef<Path> for PathBuf {
#[inline]
fn as_ref(&self) -> &Path {
self
}
}
impl<'a> IntoIterator for &'a PathBuf {
type Item = &'a str;
type IntoIter = Iter<'a>;
fn into_iter(self) -> Iter<'a> {
self.iter()
}
}
impl<'a> IntoIterator for &'a Path {
type Item = &'a str;
type IntoIter = Iter<'a>;
fn into_iter(self) -> Iter<'a> {
self.iter()
}
}
macro_rules! impl_cmp {
($lhs:ty, $rhs: ty) => {
impl<'a, 'b> PartialEq<$rhs> for $lhs {
#[inline]
fn eq(&self, other: &$rhs) -> bool {
<Path as PartialEq>::eq(self, other)
}
}
impl<'a, 'b> PartialEq<$lhs> for $rhs {
#[inline]
fn eq(&self, other: &$lhs) -> bool {
<Path as PartialEq>::eq(self, other)
}
}
impl<'a, 'b> PartialOrd<$rhs> for $lhs {
#[inline]
fn partial_cmp(&self, other: &$rhs) -> Option<cmp::Ordering> {
<Path as PartialOrd>::partial_cmp(self, other)
}
}
impl<'a, 'b> PartialOrd<$lhs> for $rhs {
#[inline]
fn partial_cmp(&self, other: &$lhs) -> Option<cmp::Ordering> {
<Path as PartialOrd>::partial_cmp(self, other)
}
}
};
}
impl_cmp!(PathBuf, Path);
impl_cmp!(PathBuf, &'a Path);
impl_cmp!(Cow<'a, Path>, Path);
impl_cmp!(Cow<'a, Path>, &'b Path);
impl_cmp!(Cow<'a, Path>, PathBuf);
impl fmt::Display for StripPrefixError {
#[allow(deprecated, deprecated_in_future)]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
"prefix not found".fmt(f)
}
}
pub struct Display<'a> {
path: &'a Path,
}
impl fmt::Debug for Display<'_> {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.path, formatter)
}
}
impl fmt::Display for Display<'_> {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.path.as_str(), formatter)
}
}