pub struct PathFile(_);
Expand description
a PathAbs
that was a file at the time of initialization, with associated methods.
Implementations§
source§impl PathFile
impl PathFile
sourcepub fn new<P: AsRef<Path>>(path: P) -> Result<PathFile>
pub fn new<P: AsRef<Path>>(path: P) -> Result<PathFile>
Instantiate a new PathFile
. The file must exist or io::Error
will be returned.
Returns io::ErrorKind::InvalidInput
if the path exists but is not a file.
Examples
use path_abs::PathFile;
let lib = PathFile::new("src/lib.rs")?;
sourcepub fn new_unchecked<P: Into<Arc<PathBuf>>>(path: P) -> PathFile
pub fn new_unchecked<P: Into<Arc<PathBuf>>>(path: P) -> PathFile
Create a PathFile
unchecked.
This is mostly used for constructing during tests, or if the path was previously validated.
This is effectively the same as a Arc<PathBuf>
.
Note: This is memory safe, so is not marked
unsafe
. However, it could cause panics in some methods if the path was not properly validated.
sourcepub fn parent_dir(&self) -> PathDir
pub fn parent_dir(&self) -> PathDir
Get the parent directory of this file as a PathDir
.
This does not make aditional syscalls, as the parent by definition must be a directory and exist.
Panics
Panics if there is no parent. The only way this could happen is if
it was constructed with new_unchecked
using a relative path.
Examples
use path_abs::{PathDir, PathFile};
let lib = PathFile::new("src/lib.rs")?;
let src = lib.parent_dir();
assert_eq!(PathDir::new("src")?, src);
sourcepub fn create<P: AsRef<Path>>(path: P) -> Result<PathFile>
pub fn create<P: AsRef<Path>>(path: P) -> Result<PathFile>
Instantiate a new PathFile
, creating an empty file if it doesn’t exist.
Examples
use path_abs::PathFile;
let example = "example.txt";
let file = PathFile::create(example)?;
// It can be done twice with no effect.
let _ = PathFile::create(example)?;
sourcepub fn read_string(&self) -> Result<String>
pub fn read_string(&self) -> Result<String>
Read the entire contents of the file into a String
.
Examples
use path_abs::PathFile;
let example = "example.txt";
let file = PathFile::create(example)?;
let expected = "foo\nbar";
file.write_str(expected)?;
assert_eq!(expected, file.read_string()?);
sourcepub fn write_str(&self, s: &str) -> Result<()>
pub fn write_str(&self, s: &str) -> Result<()>
Write the str
to a file, truncating it first if it exists and creating it otherwise.
Examples
use path_abs::PathFile;
let example = "example.txt";
let file = PathFile::create(example)?;
let expected = "foo\nbar";
file.write_str(expected)?;
assert_eq!(expected, file.read_string()?);
sourcepub fn append_str(&self, s: &str) -> Result<()>
pub fn append_str(&self, s: &str) -> Result<()>
Append the str
to a file, creating it if it doesn’t exist.
Examples
use path_abs::PathFile;
let example = "example.txt";
let file = PathFile::create(example)?;
let expected = "foo\nbar\nbaz";
file.append_str("foo\nbar")?;
file.append_str("\nbaz")?;
assert_eq!(expected, file.read_string()?);
sourcepub fn open_read(&self) -> Result<FileRead>
pub fn open_read(&self) -> Result<FileRead>
Open the file as read-only.
Examples
use std::io::Read;
use path_abs::PathFile;
let example = "example.txt";
let file = PathFile::create(example)?;
let expected = "foo\nbar";
file.write_str(expected)?;
let mut read = file.open_read()?;
let mut s = String::new();
read.read_to_string(&mut s)?;
assert_eq!(expected, s);
sourcepub fn open_append(&self) -> Result<FileWrite>
pub fn open_append(&self) -> Result<FileWrite>
Open the file as write-only in append mode.
Examples
use std::io::Write;
use path_abs::PathFile;
let example = "example.txt";
let file = PathFile::create(example)?;
let expected = "foo\nbar\n";
file.write_str("foo\n")?;
let mut append = file.open_append()?;
append.write_all(b"bar\n")?;
append.flush();
assert_eq!(expected, file.read_string()?);
sourcepub fn open_edit(&self) -> Result<FileEdit>
pub fn open_edit(&self) -> Result<FileEdit>
Open the file for editing (reading and writing).
Examples
use std::io::{Read, Seek, Write, SeekFrom};
use path_abs::PathFile;
let example = "example.txt";
let file = PathFile::create(example)?;
let expected = "foo\nbar";
let mut edit = file.open_edit()?;
let mut s = String::new();
edit.write_all(expected.as_bytes())?;
edit.seek(SeekFrom::Start(0))?;
edit.read_to_string(&mut s)?;
assert_eq!(expected, s);
sourcepub fn copy<P: AsRef<Path>>(&self, path: P) -> Result<PathFile>
pub fn copy<P: AsRef<Path>>(&self, path: P) -> Result<PathFile>
Copy the file to another location, including permission bits
Examples
use path_abs::PathFile;
use std::path::Path;
let example = "example.txt";
let example_bk = "example.txt.bk";
let file = PathFile::create(example)?;
let contents = "This is some contents";
file.write_str(contents);
let file_bk = file.copy(example_bk)?;
assert_eq!(contents, file.read_string()?);
assert_eq!(contents, file_bk.read_string()?);
sourcepub fn rename<P: AsRef<Path>>(self, to: P) -> Result<PathFile>
pub fn rename<P: AsRef<Path>>(self, to: P) -> Result<PathFile>
Rename a file, replacing the original file if to
already exists.
This will not work if the new name is on a different mount point.
Examples
use path_abs::{PathFile, PathInfo};
use std::path::Path;
let example = "example.txt";
let example_bk = "example.txt.bk";
let file = PathFile::create(example)?;
let contents = "This is some contents";
file.write_str(contents);
let file_bk = file.clone().rename(example_bk)?;
assert!(!file.exists());
assert_eq!(contents, file_bk.read_string()?);
sourcepub fn symlink<P: AsRef<Path>>(&self, dst: P) -> Result<PathFile>
pub fn symlink<P: AsRef<Path>>(&self, dst: P) -> Result<PathFile>
Creates a new symbolic link on the filesystem to the dst.
This handles platform specific behavior correctly.
Examples
use path_abs::PathFile;
use std::path::Path;
let example = "example.txt";
let example_sym = "example.txt.sym";
let file = PathFile::create(example)?;
let contents = "This is some contents";
file.write_str(contents);
let file_sym = file.symlink(example_sym)?;
// They have a different "absolute path"
assert_ne!(file, file_sym);
// But they can be canonicalized to the same file.
let file_can = file_sym.canonicalize()?;
assert_eq!(file, file_can);
sourcepub fn remove(self) -> Result<()>
pub fn remove(self) -> Result<()>
Remove (delete) the file from the filesystem, consuming self.
Examples
use path_abs::{PathFile, PathInfo};
use std::path::Path;
let example = "example.txt";
let file = PathFile::create(example)?;
assert!(file.exists());
file.remove()?;
// file.exists() <--- COMPILER ERROR, `file` was consumed
assert!(!Path::new(example).exists());
sourcepub fn canonicalize(&self) -> Result<PathFile>
pub fn canonicalize(&self) -> Result<PathFile>
Returns the canonical form of the path with all intermediate components normalized and symbolic links resolved.
Trait Implementations§
source§impl Ord for PathFile
impl Ord for PathFile
source§impl PartialEq<PathFile> for PathFile
impl PartialEq<PathFile> for PathFile
source§impl PartialOrd<PathFile> for PathFile
impl PartialOrd<PathFile> for PathFile
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl PathOps for PathFile
impl PathOps for PathFile
type Output = PathAbs
source§fn concat<P: AsRef<Path>>(&self, path: P) -> Result<Self::Output>
fn concat<P: AsRef<Path>>(&self, path: P) -> Result<Self::Output>
source§fn join<P: AsRef<Path>>(&self, path: P) -> Self::Output
fn join<P: AsRef<Path>>(&self, path: P) -> Self::Output
std::path::Path::join
with all of its gotchas and pitfalls,, except
returns a more relevant type. Read more