...
This commit is contained in:
121
src/os/fs.rs
121
src/os/fs.rs
@@ -21,6 +21,9 @@ pub enum FsError {
|
||||
UnknownFileType(String),
|
||||
MetadataError(io::Error),
|
||||
ChangeDirFailed(io::Error),
|
||||
ReadFailed(io::Error),
|
||||
WriteFailed(io::Error),
|
||||
AppendFailed(io::Error),
|
||||
}
|
||||
|
||||
// Implement Display for FsError
|
||||
@@ -40,6 +43,9 @@ impl fmt::Display for FsError {
|
||||
FsError::UnknownFileType(path) => write!(f, "Unknown file type at '{}'", path),
|
||||
FsError::MetadataError(e) => write!(f, "Failed to get file metadata: {}", e),
|
||||
FsError::ChangeDirFailed(e) => write!(f, "Failed to change directory: {}", e),
|
||||
FsError::ReadFailed(e) => write!(f, "Failed to read file: {}", e),
|
||||
FsError::WriteFailed(e) => write!(f, "Failed to write to file: {}", e),
|
||||
FsError::AppendFailed(e) => write!(f, "Failed to append to file: {}", e),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -55,6 +61,9 @@ impl Error for FsError {
|
||||
FsError::InvalidGlobPattern(e) => Some(e),
|
||||
FsError::MetadataError(e) => Some(e),
|
||||
FsError::ChangeDirFailed(e) => Some(e),
|
||||
FsError::ReadFailed(e) => Some(e),
|
||||
FsError::WriteFailed(e) => Some(e),
|
||||
FsError::AppendFailed(e) => Some(e),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
@@ -619,3 +628,115 @@ pub fn chdir(path: &str) -> Result<String, FsError> {
|
||||
|
||||
Ok(format!("Successfully changed directory to '{}'", path))
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the contents of a file.
|
||||
*
|
||||
* # Arguments
|
||||
*
|
||||
* * `path` - The path of the file to read
|
||||
*
|
||||
* # Returns
|
||||
*
|
||||
* * `Ok(String)` - The contents of the file
|
||||
* * `Err(FsError)` - An error if the file doesn't exist or can't be read
|
||||
*
|
||||
* # Examples
|
||||
*
|
||||
* ```
|
||||
* let content = file_read("file.txt")?;
|
||||
* println!("File content: {}", content);
|
||||
* ```
|
||||
*/
|
||||
pub fn file_read(path: &str) -> Result<String, FsError> {
|
||||
let path_obj = Path::new(path);
|
||||
|
||||
// Check if file exists
|
||||
if !path_obj.exists() {
|
||||
return Err(FsError::FileNotFound(path.to_string()));
|
||||
}
|
||||
|
||||
// Check if it's a regular file
|
||||
if !path_obj.is_file() {
|
||||
return Err(FsError::NotAFile(path.to_string()));
|
||||
}
|
||||
|
||||
// Read file content
|
||||
fs::read_to_string(path_obj).map_err(FsError::ReadFailed)
|
||||
}
|
||||
|
||||
/**
|
||||
* Write content to a file (creates the file if it doesn't exist, overwrites if it does).
|
||||
*
|
||||
* # Arguments
|
||||
*
|
||||
* * `path` - The path of the file to write to
|
||||
* * `content` - The content to write to the file
|
||||
*
|
||||
* # Returns
|
||||
*
|
||||
* * `Ok(String)` - A success message indicating the file was written
|
||||
* * `Err(FsError)` - An error if the file can't be written
|
||||
*
|
||||
* # Examples
|
||||
*
|
||||
* ```
|
||||
* let result = file_write("file.txt", "Hello, world!")?;
|
||||
* println!("{}", result);
|
||||
* ```
|
||||
*/
|
||||
pub fn file_write(path: &str, content: &str) -> Result<String, FsError> {
|
||||
let path_obj = Path::new(path);
|
||||
|
||||
// Create parent directories if they don't exist
|
||||
if let Some(parent) = path_obj.parent() {
|
||||
fs::create_dir_all(parent).map_err(FsError::CreateDirectoryFailed)?;
|
||||
}
|
||||
|
||||
// Write content to file
|
||||
fs::write(path_obj, content).map_err(FsError::WriteFailed)?;
|
||||
|
||||
Ok(format!("Successfully wrote to file '{}'", path))
|
||||
}
|
||||
|
||||
/**
|
||||
* Append content to a file (creates the file if it doesn't exist).
|
||||
*
|
||||
* # Arguments
|
||||
*
|
||||
* * `path` - The path of the file to append to
|
||||
* * `content` - The content to append to the file
|
||||
*
|
||||
* # Returns
|
||||
*
|
||||
* * `Ok(String)` - A success message indicating the content was appended
|
||||
* * `Err(FsError)` - An error if the file can't be appended to
|
||||
*
|
||||
* # Examples
|
||||
*
|
||||
* ```
|
||||
* let result = file_write_append("log.txt", "New log entry\n")?;
|
||||
* println!("{}", result);
|
||||
* ```
|
||||
*/
|
||||
pub fn file_write_append(path: &str, content: &str) -> Result<String, FsError> {
|
||||
let path_obj = Path::new(path);
|
||||
|
||||
// Create parent directories if they don't exist
|
||||
if let Some(parent) = path_obj.parent() {
|
||||
fs::create_dir_all(parent).map_err(FsError::CreateDirectoryFailed)?;
|
||||
}
|
||||
|
||||
// Open file in append mode (or create if it doesn't exist)
|
||||
let mut file = fs::OpenOptions::new()
|
||||
.create(true)
|
||||
.append(true)
|
||||
.open(path_obj)
|
||||
.map_err(FsError::AppendFailed)?;
|
||||
|
||||
// Append content to file
|
||||
use std::io::Write;
|
||||
file.write_all(content.as_bytes()).map_err(FsError::AppendFailed)?;
|
||||
|
||||
Ok(format!("Successfully appended to file '{}'", path))
|
||||
}
|
||||
|
Reference in New Issue
Block a user