File

Struct File 

Source
pub struct File { /* private fields */ }
Expand description

Represents a path to a file or directory.

§Constructing a File object

File can be constructed either with a path or an URL. The following URL schemes are supported:

  • file:
  • app: file in the application installation directory
  • app-storage: file in the application private directory

The File constructor performs implicit normalization of the given path argument.

Implementations§

Source§

impl File

Source

pub fn new(url_or_path: impl AsRef<str>) -> Self

Constructs a new File object.

Source

pub fn url(&self) -> String

The URL for this file path.

Source

pub fn native_path(&self) -> String

The full path in the host operating system representation.

Source

pub fn relative_path(&self, another: &File) -> String

Finds the relative path from the File object to another File object. If they point to the same path, this function returns a single dot (".").

§Example
let path = File::new("app://foo/bar").relative_path(&File::new("app://qux/foo"));
assert_eq!(path.as_str(), "../../qux/foo");
Source

pub fn resolve_path(&self, arg: impl AsRef<str>) -> Self

Resolves relative path.

§Example
let file = File::new("app://foo/bar").resolve_path("zxc/../abc");
assert_eq!(file.url().as_str(), "app://foo/bar/abc");
Source

pub fn name(&self) -> String

The last portion of this path.

§Example
assert_eq!(File::new("app://foo.txt").name(), "foo.txt".to_owned());
Source

pub fn name_without_extension(&self, extension: impl AsRef<str>) -> String

The last portion of this path, excluding the given extension.

§Example
assert_eq!(File::new("app://foo.txt").name_without_extension(".txt"), "foo".to_owned());
Source

pub fn extension(&self) -> String

The filename extension. This includes the dot.

§Example
assert_eq!(File::new("app://foo.txt").extension(), ".txt".to_owned());
Source

pub fn parent(&self) -> Option<File>

The directory that contains the file or directory referenced by the File object.

This property is identical to the return value of resolve_path("..") except that the parent of a root directory is None.

Source

pub fn application_directory() -> Self

Returns a reference to the application installation directory. This is equivalent to (File::new"app://"").

Source

pub fn application_storage_directory() -> Self

Returns a reference to the application private directory. This is equivalent to the URL File::new("app-storage://").

Source

pub fn downloads_directory() -> Option<File>

The user downloads directory.

Source

pub fn documents_directory() -> Option<File>

The user documents directory.

Source

pub fn executable_directory() -> Option<File>

The executable directory.

Source

pub fn user_directory() -> Option<File>

Source

pub fn pictures_directory() -> Option<File>

The user pictures directory.

Source

pub fn videos_directory() -> Option<File>

The user videos directory.

Source

pub fn working_directory() -> Option<File>

The application working directory. This is used primarily for command-line applications.

Source

pub fn exists(&self) -> bool

Determines whether the referenced path exists.

Source

pub fn is_directory(&self) -> bool

Determines whether the referenced path is a directory.

Source

pub fn is_file(&self) -> bool

Determines whether the referenced path is a file.

Determines whether the referenced path is a symbolic link.

Source

pub fn canonicalize(&self) -> File

Returns a canonicalization of the File path.

Source

pub async fn canonicalize_async(&self) -> File

Returns a canonicalization of the File path.

Source

pub fn copy_to(&self, new_location: &File) -> Result<(), FileError>

Copies the file at the location specified by the File object to the location specified by the new_location parameter.

This method will overwrite the contents of new_location.

Source

pub async fn copy_to_async(&self, new_location: &File) -> Result<(), FileError>

Asynchronously copies the file at the location specified by the File object to the location specified by the new_location parameter.

This method will overwrite the contents of new_location.

Source

pub fn create_directory(&self) -> Result<(), FileError>

Creates the specified directory and any necessary parent directories. If the directory already exists, no action is taken.

Source

pub async fn create_directory_async(&self) -> Result<(), FileError>

Asynchronously creates the specified directory and any necessary parent directories. If the directory already exists, no action is taken.

Source

pub fn read_bytes(&self) -> Result<Vec<u8>, FileError>

Read file contents as bytes.

Source

pub async fn read_bytes_async(&self) -> Result<Vec<u8>, FileError>

Asynchronously read file contents as bytes.

Source

pub fn read_utf8(&self) -> Result<String, FileError>

Read file contents as a UTF-8 string.

Source

pub async fn read_utf8_async(&self) -> Result<String, FileError>

Asynchronously read file contents as a UTF-8 string.

Source

pub fn get_directory_listing(&self) -> Result<Vec<File>, FileError>

Returns a vector of File objects corresponding to files and directories in the directory represented by the File object.

Source

pub fn delete_empty_directory(&self) -> Result<(), FileError>

Deletes empty directory.

Source

pub async fn delete_empty_directory_async(&self) -> Result<(), FileError>

Asynchronously deletes empty directory.

Source

pub fn delete_all_directory(&self) -> Result<(), FileError>

Deletes directory after deleting all its contents.

Source

pub async fn delete_all_directory_async(&self) -> Result<(), FileError>

Asynchronously deletes directory after deleting all its contents.

Source

pub fn delete_file(&self) -> Result<(), FileError>

Deletes file.

Source

pub async fn delete_file_async(&self) -> Result<(), FileError>

Asynchronously deletes file.

Source

pub fn move_to(&self, to: &File) -> Result<(), FileError>

Move a file or directory to another path specified by the to parameter, replacing the original file if to already exists.

Source

pub async fn move_to_async(&self, to: &File) -> Result<(), FileError>

Asynchronously move a file or directory to another path specified by the to parameter, replacing the original file if to already exists.

Source

pub fn rename(&self, to: &File) -> Result<(), FileError>

Rename a file or directory to a new name specified by the to parameter, replacing the original file if to already exists.

Source

pub async fn rename_async(&self, to: &File) -> Result<(), FileError>

Asynchronously rename a file or directory to a new name specified by the to parameter, replacing the original file if to already exists.

Source

pub fn write<B: AsRef<[u8]>>(&self, b: B) -> Result<(), FileError>

Writes bytes to a file.

Source

pub async fn write_async<B: AsRef<[u8]>>(&self, b: B) -> Result<(), FileError>

Asynchronously writes bytes to a file.

Source

pub fn creation_date(&self) -> Result<DateTime<Local>, FileError>

Creation date.

Source

pub async fn creation_date_async(&self) -> Result<DateTime<Local>, FileError>

Creation date.

Source

pub fn modification_date(&self) -> Result<DateTime<Local>, FileError>

Modification date.

Source

pub async fn modification_date_async( &self, ) -> Result<DateTime<Local>, FileError>

Modification date.

Source

pub fn size(&self) -> Result<i64, FileError>

Size of the file in bytes.

Source

pub async fn size_async(&self) -> Result<i64, FileError>

Size of the file in bytes.

Trait Implementations§

Source§

impl Clone for File

Source§

fn clone(&self) -> File

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 PartialEq for File

Source§

fn eq(&self, other: &File) -> 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 Eq for File

Source§

impl StructuralPartialEq for File

Auto Trait Implementations§

§

impl Freeze for File

§

impl RefUnwindSafe for File

§

impl Send for File

§

impl Sync for File

§

impl Unpin for File

§

impl UnwindSafe for File

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> 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, 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.