Struct path_abs::PathFile

source ·
pub struct PathFile(_);
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 copy 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 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) -> Selfwhere Self: Sized,

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

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

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

fn clamp(self, min: Self, max: Self) -> Selfwhere Self: Sized + PartialOrd<Self>,

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

impl PartialEq<PathFile> for PathFile

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd<PathFile> 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

This method 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

This method 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

This method 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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl PathOps for PathFile

§

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 Eq for PathFile

source§

impl StructuralEq for PathFile

source§

impl StructuralPartialEq for PathFile

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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 Twhere 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 Twhere T: Clone,

§

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, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.