FileManager

Struct FileManager 

Source
pub struct FileManager;
Expand description

This is a file manager.

Implementations§

Source§

impl FileManager

Source

pub fn create_file(path: &str) -> Result<File, Error>

Creates a new File.

§Arguments
  • path - The name of the file.(This will get the path of the root folder) or the absolute path of the file.
§Example
use filelib::FileManager;
FileManager::create_file("file.txt").unwrap();
FileManager::create_file("C:\\user\\documents\\file.txt").unwrap();
§Errors

If the file cannot be created, an error is returned.

Source

pub fn write_to_file(path: &str, data: &str) -> Result<(), Error>

Writes a string to a file.

§Arguments
  • path - The name of the file.(This will get the path of the root folder) or the absolute path of the file.
  • content - The content to write to the file.
§Example
use filelib::FileManager;
FileManager::write_to_file("file.txt", "Hello World!").unwrap();
FileManager::write_to_file("C:\\user\\documents\\file.txt", "Hello World!").unwrap();
§Errors

If the file cannot be written, an error is returned.

Source

pub fn clean_file(path: &str) -> Result<(), Error>

Cleans a file.

§Arguments
  • path - The name of the file.(This will get the path of the root folder) or the absolute path of the file.
§Example
use filelib::FileManager;
FileManager::clean_file("file.txt").unwrap();
FileManager::clean_file("C:\\user\\documents\\file.txt").unwrap();
§Errors

If the file cannot be cleaned, an error is returned.

Source

pub fn read_file(path: &str) -> Result<String>

Reads a file and returns its content.

§Arguments
  • path - The name of the file.(This will get the path of the root folder) or the absolute path of the file.
§Example
use filelib::FileManager;
let cont1 = FileManager::read_file("file.txt").unwrap();
let cont2 = FileManager::read_file("C:\\user\\documents\\file.txt").unwrap();
Source

pub fn file_exists(path: &str) -> bool

Checks if a file exists.

§Arguments
  • path - The path of the file.
§Example
use filelib::{FileManager};
FileManager::file_exists("file.txt").unwrap();
FileManager::file_exists("C:\\user\\documents\\file.txt").unwrap();
§Errors

If the file cannot be found, an error is returned.

Source

pub fn delete_file(path: &str) -> Result<bool, Error>

Deletes a file.

§Arguments
  • path - The path of the file.
§Example
use filelib::{FileManager};
FileManager::delete_file("file.txt").unwrap();
FileManager::delete_file("C:\\user\\documents\\file.txt").unwrap();
§Errors

If the file cannot be deleted, an error is returned.

Source

pub fn rename_file(path: &str, new_name: &str) -> Result<(), Error>

Renames a file

§Arguments
  • path - The path of the file.
  • new_name - The new name of the file.
§Example
use filelib::{FileManager};
FileManager::rename_file("file.txt", "new_file.txt").unwrap();
FileManager::rename_file("C:\\user\\documents\\file.txt", "new_file.txt").unwrap();
§Errors

If the file cannot be renamed, an error is returned.

Source

pub fn change_extension(path: &str, new_extension: &str) -> Result<bool, Error>

Changes a file extension

§Arguments
  • path - The path of the file.
  • new_extension - The new extension of the file.
§Example
use filelib::{FileManager};
FileManager::change_extension("file.txt", "rs").unwrap();
FileManager::change_extension("C:\\user\\documents\\file.txt", "rs").unwrap();
§Errors

If the file cannot be renamed, an error is returned.

Source

pub fn copy_file(path: &str, another_path: &str) -> Result<bool, Error>

Copies a file

§Arguments
  • path - The path of the file.
  • another_path - The path of the new file.
§Example
use filelib::{FileManager};
FileManager::copy_file("file.txt", "C:\\user\\documents\\file.txt").unwrap();
FileManager::copy_file("C:\\user\\documents\\file.txt", "C:\\user\\documents\\file.txt").unwrap();
§Errors

If the file cannot be found, an error is returned.

If the paths have the same value , an error is returned.

Source

pub fn create_dir(path: &str) -> Result<bool, Error>

Creates a directory

§Arguments
  • path - The path of the directory.
§Example
use filelib::{FileManager};
FileManager::create_dir("C:\\user\\documents\\some_dir").unwrap();
FileManager::create_dir("C:\\user\\documents\\some_dir\\some_dir2").unwrap();
FileManager::create_dir("some_dir3").unwrap();
§Errors

If the directory cannot be created, an error is returned.

Source

pub fn delete_dir(path: &str) -> Result<bool, Error>

Deletes a directory

§Arguments
  • path - The path of the directory.
§Example
use filelib::{FileManager};
FileManager::delete_dir("C:\\user\\documents\\some_dir").unwrap();
FileManager::delete_dir("C:\\user\\documents\\some_dir\\some_dir2").unwrap();
FileManager::delete_dir("some_dir3").unwrap();
§Errors

If the directory cannot be deleted, an error is returned.

Source

pub fn rename_dir(path: &str, new_name: &str) -> Result<bool, Error>

Renames a directory

§Arguments
  • path - The path of the directory.
  • new_name - The new name of the directory.
§Example
use filelib::{FileManager};
FileManager::rename_dir("C:\\user\\documents\\some_dir", "new_dir").unwrap();
FileManager::rename_dir("C:\\user\\documents\\some_dir\\some_dir2", "new_dir2").unwrap();
FileManager::rename_dir("some_dir3", "new_dir3").unwrap();
§Errors

If the directory cannot be renamed, an error is returned.

Source

pub fn copy_dir(path: &str, another_path: &str) -> Result<bool, Error>

Copies a directory

§Arguments
  • path - The path of the directory.
  • another_path - The new path of the directory.
§Example
use filelib::{FileManager};
FileManager::copy_dir("C:\\user\\documents\\some_dir", "C:\\user\\documents\\some_dir2").unwrap();
FileManager::copy_dir("C:\\user\\documents\\some_dir\\some_dir2", "C:\\user\\documents\\some_dir2\\some_dir3").unwrap();
FileManager::copy_dir("some_dir3", "some_dir4").unwrap();
§Errors

If the directory is not found, an error is returned.

If the paths have the same value , an error is returned.

Auto Trait Implementations§

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