Struct PathFile

Source
pub struct PathFile(/* private fields */);
Expand description

a PathAbs that was a file at the time of initialization, with associated methods.

Implementations§

Source§

impl PathFile

Source

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")?;
Source

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.

Source

pub fn try_from<P: Into<PathAbs>>(path: P) -> Result<PathFile>

Convert a PathAbs into a PathFile, first validating that the path is a file.

§Error

If the path is not a file.

§Examples
use path_abs::{PathAbs, PathFile};

let lib_abs = PathAbs::new("src/lib.rs")?;
let lib_file = PathFile::try_from(lib_abs)?;
Source

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);
Source

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)?;
Source

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()?);
Source

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()?);
Source

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()?);
Source

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);
Source

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()?);
Source

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);
Source

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()?);
Source

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()?);

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);
Source

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());
Source

pub fn as_path(&self) -> &Path

Return a reference to a basic std::path::Path

Source

pub fn canonicalize(&self) -> Result<PathFile>

Returns the canonical form of the path with all intermediate components normalized and symbolic links resolved.

See PathAbs::canonicalize

Trait Implementations§

Source§

impl AsRef<OsStr> for PathFile

Source§

fn as_ref(&self) -> &OsStr

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl AsRef<Path> for PathFile

Source§

fn as_ref(&self) -> &Path

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl AsRef<PathAbs> for PathFile

Source§

fn as_ref(&self) -> &PathAbs

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl AsRef<PathBuf> for PathFile

Source§

fn as_ref(&self) -> &PathBuf

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<'a> Borrow<Path> for &'a PathFile

Source§

fn borrow(&self) -> &Path

Immutably borrows from an owned value. Read more
Source§

impl Borrow<Path> for PathFile

Source§

fn borrow(&self) -> &Path

Immutably borrows from an owned value. Read more
Source§

impl<'a> Borrow<PathAbs> for &'a PathFile

Source§

fn borrow(&self) -> &PathAbs

Immutably borrows from an owned value. Read more
Source§

impl Borrow<PathAbs> for PathFile

Source§

fn borrow(&self) -> &PathAbs

Immutably borrows from an owned value. Read more
Source§

impl<'a> Borrow<PathBuf> for &'a PathFile

Source§

fn borrow(&self) -> &PathBuf

Immutably borrows from an owned value. Read more
Source§

impl Borrow<PathBuf> for PathFile

Source§

fn borrow(&self) -> &PathBuf

Immutably borrows from an owned value. Read more
Source§

impl Clone for PathFile

Source§

fn clone(&self) -> PathFile

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for PathFile

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for PathFile

Source§

fn deserialize<D>(deserializer: D) -> Result<PathFile, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl From<PathFile> for Arc<PathBuf>

Source§

fn from(path: PathFile) -> Arc<PathBuf>

Converts to this type from the input type.
Source§

impl From<PathFile> for PathAbs

Source§

fn from(path: PathFile) -> PathAbs

Converts to this type from the input type.
Source§

impl From<PathFile> for PathBuf

Source§

fn from(path: PathFile) -> PathBuf

Converts to this type from the input type.
Source§

impl Hash for PathFile

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for PathFile

Source§

fn cmp(&self, other: &PathFile) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for PathFile

Source§

fn eq(&self, other: &PathFile) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for PathFile

Source§

fn partial_cmp(&self, other: &PathFile) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl PathOps for PathFile

Source§

type Output = PathAbs

Source§

fn concat<P: AsRef<Path>>(&self, path: P) -> Result<Self::Output>

Returns a new value representing the concatenation of two paths. Read more
Source§

fn join<P: AsRef<Path>>(&self, path: P) -> Self::Output

An exact replica of std::path::Path::join with all of its gotchas and pitfalls,, except returns a more relevant type. Read more
Source§

fn with_file_name<S: AsRef<OsStr>>(&self, file_name: S) -> Self::Output

Creates a new path object like self but with the given file name. Read more
Source§

fn with_extension<S: AsRef<OsStr>>(&self, extension: S) -> Self::Output

Creates a new path object like self but with the given extension. Read more
Source§

impl Serialize for PathFile

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Eq for PathFile

Source§

impl StructuralPartialEq for PathFile

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PathInfo for T
where T: Clone + Borrow<PathBuf> + Into<Arc<PathBuf>>,

Source§

fn as_path(&self) -> &Path

Source§

fn to_arc_pathbuf(&self) -> Arc<PathBuf>

Source§

fn as_os_str(&self) -> &OsStr

Source§

fn to_str(&self) -> Option<&str>

Source§

fn to_string_lossy(&self) -> Cow<'_, str>

Source§

fn is_absolute(&self) -> bool

Source§

fn is_relative(&self) -> bool

Source§

fn has_root(&self) -> bool

Source§

fn ancestors(&self) -> Ancestors<'_>

Source§

fn file_name(&self) -> Option<&OsStr>

Source§

fn strip_prefix<P>(&self, base: P) -> Result<&Path, StripPrefixError>
where P: AsRef<Path>,

Source§

fn starts_with<P: AsRef<Path>>(&self, base: P) -> bool

Source§

fn ends_with<P: AsRef<Path>>(&self, base: P) -> bool

Source§

fn file_stem(&self) -> Option<&OsStr>

Source§

fn extension(&self) -> Option<&OsStr>

Source§

fn components(&self) -> Components<'_>

Source§

fn iter(&self) -> Iter<'_>

Source§

fn display(&self) -> Display<'_>

Source§

fn metadata(&self) -> Result<Metadata>

Queries the file system to get information about a file, directory, etc. Read more
Queries the metadata about a file without following symlinks. Read more
Source§

fn exists(&self) -> bool

Source§

fn is_file(&self) -> bool

Source§

fn is_dir(&self) -> bool

Reads a symbolic link, returning the path that the link points to. Read more
Source§

fn canonicalize(&self) -> Result<PathAbs>

Returns the canonical, absolute form of the path with all intermediate components normalized and symbolic links resolved. Read more
Source§

fn parent(&self) -> Result<&Path>

Returns the path without its final component, if there is one. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToStfu8 for T
where T: Borrow<PathBuf>,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,