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 directoryapp-storage:file in the application private directory
The File constructor performs implicit normalization of the
given path argument.
Implementations§
Source§impl File
impl File
Sourcepub fn native_path(&self) -> String
pub fn native_path(&self) -> String
The full path in the host operating system representation.
Sourcepub fn relative_path(&self, another: &File) -> String
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");Sourcepub fn resolve_path(&self, arg: impl AsRef<str>) -> Self
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");Sourcepub fn name(&self) -> String
pub fn name(&self) -> String
The last portion of this path.
§Example
assert_eq!(File::new("app://foo.txt").name(), "foo.txt".to_owned());Sourcepub fn name_without_extension(&self, extension: impl AsRef<str>) -> String
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());Sourcepub fn extension(&self) -> String
pub fn extension(&self) -> String
The filename extension. This includes the dot.
§Example
assert_eq!(File::new("app://foo.txt").extension(), ".txt".to_owned());Sourcepub fn parent(&self) -> Option<File>
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.
Sourcepub fn application_directory() -> Self
pub fn application_directory() -> Self
Returns a reference to the application installation directory.
This is equivalent to (File::new"app://"").
Sourcepub fn application_storage_directory() -> Self
pub fn application_storage_directory() -> Self
Returns a reference to the application private directory.
This is equivalent to the URL File::new("app-storage://").
Sourcepub fn downloads_directory() -> Option<File>
pub fn downloads_directory() -> Option<File>
The user downloads directory.
Sourcepub fn documents_directory() -> Option<File>
pub fn documents_directory() -> Option<File>
The user documents directory.
Sourcepub fn executable_directory() -> Option<File>
pub fn executable_directory() -> Option<File>
The executable directory.
pub fn user_directory() -> Option<File>
Sourcepub fn pictures_directory() -> Option<File>
pub fn pictures_directory() -> Option<File>
The user pictures directory.
Sourcepub fn videos_directory() -> Option<File>
pub fn videos_directory() -> Option<File>
The user videos directory.
Sourcepub fn working_directory() -> Option<File>
pub fn working_directory() -> Option<File>
The application working directory. This is used primarily for command-line applications.
Sourcepub fn is_directory(&self) -> bool
pub fn is_directory(&self) -> bool
Determines whether the referenced path is a directory.
Sourcepub fn is_symbolic_link(&self) -> bool
pub fn is_symbolic_link(&self) -> bool
Determines whether the referenced path is a symbolic link.
Sourcepub fn canonicalize(&self) -> File
pub fn canonicalize(&self) -> File
Returns a canonicalization of the File path.
Sourcepub async fn canonicalize_async(&self) -> File
pub async fn canonicalize_async(&self) -> File
Returns a canonicalization of the File path.
Sourcepub fn copy_to(&self, new_location: &File) -> Result<(), FileError>
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.
Sourcepub async fn copy_to_async(&self, new_location: &File) -> Result<(), FileError>
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.
Sourcepub fn create_directory(&self) -> Result<(), FileError>
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.
Sourcepub async fn create_directory_async(&self) -> Result<(), FileError>
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.
Sourcepub async fn read_bytes_async(&self) -> Result<Vec<u8>, FileError>
pub async fn read_bytes_async(&self) -> Result<Vec<u8>, FileError>
Asynchronously read file contents as bytes.
Sourcepub async fn read_utf8_async(&self) -> Result<String, FileError>
pub async fn read_utf8_async(&self) -> Result<String, FileError>
Asynchronously read file contents as a UTF-8 string.
Sourcepub fn get_directory_listing(&self) -> Result<Vec<File>, FileError>
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.
Sourcepub fn delete_empty_directory(&self) -> Result<(), FileError>
pub fn delete_empty_directory(&self) -> Result<(), FileError>
Deletes empty directory.
Sourcepub async fn delete_empty_directory_async(&self) -> Result<(), FileError>
pub async fn delete_empty_directory_async(&self) -> Result<(), FileError>
Asynchronously deletes empty directory.
Sourcepub fn delete_all_directory(&self) -> Result<(), FileError>
pub fn delete_all_directory(&self) -> Result<(), FileError>
Deletes directory after deleting all its contents.
Sourcepub async fn delete_all_directory_async(&self) -> Result<(), FileError>
pub async fn delete_all_directory_async(&self) -> Result<(), FileError>
Asynchronously deletes directory after deleting all its contents.
Sourcepub fn delete_file(&self) -> Result<(), FileError>
pub fn delete_file(&self) -> Result<(), FileError>
Deletes file.
Sourcepub async fn delete_file_async(&self) -> Result<(), FileError>
pub async fn delete_file_async(&self) -> Result<(), FileError>
Asynchronously deletes file.
Sourcepub fn move_to(&self, to: &File) -> Result<(), FileError>
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.
Sourcepub async fn move_to_async(&self, to: &File) -> Result<(), FileError>
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.
Sourcepub fn rename(&self, to: &File) -> Result<(), FileError>
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.
Sourcepub async fn rename_async(&self, to: &File) -> Result<(), FileError>
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.
Sourcepub async fn write_async<B: AsRef<[u8]>>(&self, b: B) -> Result<(), FileError>
pub async fn write_async<B: AsRef<[u8]>>(&self, b: B) -> Result<(), FileError>
Asynchronously writes bytes to a file.
Sourcepub async fn modification_date_async(
&self,
) -> Result<DateTime<Local>, FileError>
pub async fn modification_date_async( &self, ) -> Result<DateTime<Local>, FileError>
Modification date.
Sourcepub async fn size_async(&self) -> Result<i64, FileError>
pub async fn size_async(&self) -> Result<i64, FileError>
Size of the file in bytes.