pub trait PathMut: PathInfo {
// Required methods
fn append<P: AsRef<Path>>(&mut self, path: P) -> Result<()>;
fn pop_up(&mut self) -> Result<()>;
fn truncate_to_root(&mut self);
fn set_file_name<S: AsRef<OsStr>>(&mut self, file_name: S);
fn set_extension<S: AsRef<OsStr>>(&mut self, extension: S) -> bool;
}
Expand description
Methods that modify a path.
These methods are not implemented for all path_abs
types because they
may break the type’s invariant. For example, if you could call
pop_up()
on a PathFile
, it would no longer be the path to
a file, but the path to a directory.
As a general rule, methods that can return an error will return a rich
[path_abs::Error
] instead of a std::io::Error
(although it will
automatically convert into a std::io::Error
with ?
if needed).
Required Methods§
sourcefn append<P: AsRef<Path>>(&mut self, path: P) -> Result<()>
fn append<P: AsRef<Path>>(&mut self, path: P) -> Result<()>
Appends path
to this path.
Note that this method represents pure concatenation, not “adjoinment”
like PathBuf::push
, so absolute paths won’t wholly replace the
current path.
..
components are resolved using pop_up
, which can consume components
on self
Errors
This method returns an error if the result would try to go outside a filesystem root,
like /
on Unix or C:\
on Windows.
Example
use std::path::PathBuf;
use path_abs::PathMut;
let mut somepath = PathBuf::from("foo");
somepath.append("bar");
assert_eq!(somepath, PathBuf::from("foo/bar"));
sourcefn pop_up(&mut self) -> Result<()>
fn pop_up(&mut self) -> Result<()>
Go “up” one directory.
This removes the last component of this path. It also resolves any ..
that exist at the
end of the path until a real item can be truncated. If the path is relative, and no
items remain then a ..
is appended to the path.
Errors
This method returns an error if the result would try to go outside a filesystem root,
like /
on Unix or C:\
on Windows.
Example
use std::path::Path;
use path_abs::PathMut;
let executable = Path::new("/usr/loca/bin/myapp");
let mut install_path = executable.to_path_buf();
install_path.pop_up()?;
assert_eq!(install_path.as_path(), Path::new("/usr/local/bin"));
Example handling weird relative paths
use std::path::Path;
use path_abs::PathMut;
let executable = Path::new("../../weird/../relative/path/../../");
let mut install_path = executable.to_path_buf();
install_path.pop_up()?;
assert_eq!(install_path.as_path(), Path::new("../../../"));
Error use case
use std::path::Path;
use path_abs::PathMut;
let tmp = Path::new("/tmp");
let mut relative = tmp.to_path_buf();
relative.pop_up()?;
assert!(relative.pop_up().is_err());
sourcefn truncate_to_root(&mut self)
fn truncate_to_root(&mut self)
Removes all components after the root, if any.
This is mostly useful on Windows, since it preserves the prefix before the root.
Example
use std::path::PathBuf;
use path_abs::PathMut;
let mut somepath = PathBuf::from(r"C:\foo\bar");
somepath.truncate_to_root();
assert_eq!(somepath, PathBuf::from(r"C:\"));