Struct ergo_fs::PathFile
[−]
[src]
pub struct PathFile(_);
a PathAbs
that was a file at the time of initialization, with associated methods.
Methods
impl PathFile
[src]
fn new<P>(path: P) -> Result<PathFile, Error> where
P: AsRef<Path>,
[src]
P: AsRef<Path>,
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")?;
fn from_abs(abs: PathAbs) -> Result<PathFile, Error>
[src]
Consume the PathAbs
validating that the path is a file and returning PathFile
. The file
must exist or io::Error
will be returned.
If the path is actually a dir returns io::ErrorKind::InvalidInput
.
This does not call
Path::cannonicalize()
, instead trusting that the input is already a fully qualified path.
Examples
use path_abs::{PathAbs, PathFile}; let lib_abs = PathAbs::new("src/lib.rs")?; let lib_file = PathFile::from_abs(lib_abs)?;
fn from_abs_unchecked(abs: PathAbs) -> PathFile
[src]
Do the conversion without checking.
This is typically used by external libraries when the type is already known through some other means (to avoid a syscall).
fn create<P>(path: P) -> Result<PathFile, Error> where
P: AsRef<Path>,
[src]
P: AsRef<Path>,
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)?;
fn read_string(&self) -> Result<String, Error>
[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()?);
fn write_str(&self, s: &str) -> Result<(), Error>
[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()?);
fn append_str(&self, s: &str) -> Result<(), Error>
[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()?);
fn read(&self) -> Result<FileRead, Error>
[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.read()?; let mut s = String::new(); read.read_to_string(&mut s)?; assert_eq!(expected, s);
fn append(&self) -> Result<FileWrite, Error>
[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.append()?; append.write_all(b"bar\n")?; append.flush(); assert_eq!(expected, file.read_string()?);
fn edit(&self) -> Result<FileEdit, Error>
[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.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);
fn remove(self) -> Result<(), Error>
[src]
Remove (delete) the file from the filesystem, consuming self.
Examples
use path_abs::PathFile; 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());
fn as_path(&self) -> &Path
[src]
Return a reference to a basic std::path::Path
fn mock<P>(path: P) -> PathFile where
P: AsRef<Path>,
[src]
P: AsRef<Path>,
Create a mock file type. For use in tests only.
See the docs for PathAbs::mock
Methods from Deref<Target = PathAbs>
fn into_file(self) -> Result<PathFile, Error>
[src]
Resolve the PathAbs
as a PathFile
. Return an error if it is not a file.
fn into_dir(self) -> Result<PathDir, Error>
[src]
Resolve the PathAbs
as a PathDir
. Return an error if it is not a directory.
fn parent_dir(&self) -> Option<PathDir>
[src]
Get the parent directory of this path as a PathDir
.
This does not make aditional syscalls, as the parent by definition must be a directory and exist.
Examples
use path_abs::{PathDir, PathFile}; let lib = PathFile::new("src/lib.rs")?; let src = lib.parent_dir().unwrap(); assert_eq!(PathDir::new("src")?, src);
fn as_path(&self) -> &Path
[src]
Return a reference to a basic std::path::Path
Trait Implementations
impl Serialize for PathFile
[src]
fn serialize<S>(
&self,
serializer: S
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error> where
S: Serializer,
[src]
&self,
serializer: S
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error> where
S: Serializer,
impl Ord for PathFile
[src]
impl Hash for PathFile
[src]
fn hash<__H>(&self, __arg_0: &mut __H) where
__H: Hasher,
[src]
__H: Hasher,
Feeds this value into the given [Hasher
]. Read more
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0[src]
H: Hasher,
Feeds a slice of this type into the given [Hasher
]. Read more
impl Clone for PathFile
[src]
impl Eq for PathFile
[src]
impl Deref for PathFile
[src]
type Target = PathAbs
The resulting type after dereferencing.
fn deref(&self) -> &PathAbs
[src]
Dereferences the value.
impl AsRef<PathBuf> for PathFile
[src]
impl AsRef<Path> for PathFile
[src]
impl AsRef<PathAbs> for PathFile
[src]
impl PartialEq<PathFile> for PathFile
[src]
impl Debug for PathFile
[src]
fn fmt(&self, f: &mut Formatter) -> Result<(), Error>
[src]
Formats the value using the given formatter.
impl<'de> Deserialize<'de> for PathFile
[src]
fn deserialize<D>(
deserializer: D
) -> Result<PathFile, <D as Deserializer<'de>>::Error> where
D: Deserializer<'de>,
[src]
deserializer: D
) -> Result<PathFile, <D as Deserializer<'de>>::Error> where
D: Deserializer<'de>,
impl Into<PathArc> for PathFile
[src]
impl Into<PathBuf> for PathFile
[src]
fn into(self) -> PathBuf
[src]
Downgrades the PathFile
into a PathBuf
. Avoids a clone if this is the only reference.
Examples
use path_abs::PathFile; use std::path::PathBuf; let file = PathFile::new("src/lib.rs")?; let buf: PathBuf = file.into();
impl Into<PathAbs> for PathFile
[src]
fn into(self) -> PathAbs
[src]
Downgrades the PathFile
into a PathAbs
Examples
use std::path::PathBuf; use path_abs::{PathFile, PathAbs}; let file = PathFile::new("src/lib.rs")?; let abs: PathAbs = file.clone().into();