[][src]Struct path_abs::PathFile

pub struct PathFile(_);

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

Methods

impl PathFile[src]

pub fn new<P: AsRef<Path>>(path: P) -> Result<PathFile>[src]

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

pub fn new_unchecked<P: Into<Arc<PathBuf>>>(path: P) -> PathFile[src]

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.

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

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

pub fn parent_dir(&self) -> PathDir[src]

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

pub fn create<P: AsRef<Path>>(path: P) -> Result<PathFile>[src]

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

pub fn read_string(&self) -> Result<String>[src]

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

pub fn write_str(&self, s: &str) -> Result<()>[src]

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

pub fn append_str(&self, s: &str) -> Result<()>[src]

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

pub fn open_read(&self) -> Result<FileRead>[src]

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

pub fn open_append(&self) -> Result<FileWrite>[src]

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

pub fn open_edit(&self) -> Result<FileEdit>[src]

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

pub fn copy<P: AsRef<Path>>(&self, path: P) -> Result<PathFile>[src]

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

pub fn rename<P: AsRef<Path>>(self, to: P) -> Result<PathFile>[src]

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

pub fn remove(self) -> Result<()>[src]

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

pub fn as_path(&self) -> &Path[src]

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

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

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

See PathAbs::canonicalize

Trait Implementations

impl PathOps for PathFile[src]

type Output = PathAbs

impl From<PathFile> for PathAbs[src]

impl From<PathFile> for Arc<PathBuf>[src]

impl From<PathFile> for PathBuf[src]

impl PartialEq<PathFile> for PathFile[src]

impl Clone for PathFile[src]

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl AsRef<OsStr> for PathFile[src]

impl AsRef<PathAbs> for PathFile[src]

impl AsRef<Path> for PathFile[src]

impl AsRef<PathBuf> for PathFile[src]

impl PartialOrd<PathFile> for PathFile[src]

impl Eq for PathFile[src]

impl Ord for PathFile[src]

fn max(self, other: Self) -> Self1.21.0[src]

Compares and returns the maximum of two values. Read more

fn min(self, other: Self) -> Self1.21.0[src]

Compares and returns the minimum of two values. Read more

fn clamp(self, min: Self, max: Self) -> Self[src]

🔬 This is a nightly-only experimental API. (clamp)

Restrict a value to a certain interval. Read more

impl Debug for PathFile[src]

impl Hash for PathFile[src]

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

Feeds a slice of this type into the given [Hasher]. Read more

impl Borrow<PathAbs> for PathFile[src]

impl Borrow<Path> for PathFile[src]

impl Borrow<PathBuf> for PathFile[src]

impl<'a> Borrow<PathAbs> for &'a PathFile[src]

impl<'a> Borrow<Path> for &'a PathFile[src]

impl<'a> Borrow<PathBuf> for &'a PathFile[src]

impl Serialize for PathFile[src]

impl<'de> Deserialize<'de> for PathFile[src]

Auto Trait Implementations

Blanket Implementations

impl<T> ToStfu8 for T where
    T: Borrow<PathBuf>, 
[src]

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

fn as_os_str(&self) -> &OsStr[src]

fn to_str(&self) -> Option<&str>[src]

fn to_string_lossy(&self) -> Cow<str>[src]

fn is_absolute(&self) -> bool[src]

fn is_relative(&self) -> bool[src]

fn has_root(&self) -> bool[src]

fn ancestors(&self) -> Ancestors[src]

fn file_name(&self) -> Option<&OsStr>[src]

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

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

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

fn file_stem(&self) -> Option<&OsStr>[src]

fn extension(&self) -> Option<&OsStr>[src]

fn components(&self) -> Components[src]

fn iter(&self) -> Iter[src]

fn display(&self) -> Display[src]

fn metadata(&self) -> Result<Metadata>[src]

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

fn exists(&self) -> bool[src]

fn is_file(&self) -> bool[src]

fn is_dir(&self) -> bool[src]

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

fn canonicalize(&self) -> Result<PathAbs>[src]

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

fn parent(&self) -> Result<&Path>[src]

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

impl<T> From<T> for T[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

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