This repository has been archived on 2026-04-25. You can view files and clone it, but cannot push or open issues or pull requests.
RustyPass/src/resource.rs
2024-06-15 16:16:18 -04:00

55 lines
No EOL
1.8 KiB
Rust

// Libraries
use std::{fs::{read_to_string, write, File}, io::{self, Read}, path::Path};
// Structures
pub struct ResourceFile {
pub path: String,
pub content: String
}
pub struct ResourceFileBin {
pub path: String,
pub content: Vec<u8>
}
// Implementations
impl ResourceFile {
pub fn exists(file_path: &str) -> bool{
return Path::new(file_path).exists();
}
pub fn read(file_path: &str) -> Self {
// Reading from a file:
let content: String = read_to_string(String::from(file_path)).expect("Failed to read file.");
// Returning the value:
return ResourceFile {path: String::from(file_path), content};
}
pub fn write(file_path: &str, content: String) -> Self {
// Writing to file
write(file_path, &content).expect("Failed writing to file");
// Returning the value:
return ResourceFile {path: String::from(file_path), content: content};
}
}
impl ResourceFileBin {
pub fn exists(file_path: &str) -> bool{
return Path::new(file_path).exists();
}
pub fn read(file_path: &str) -> Self {
// Reading from a file:
let file: File = File::open(String::from(file_path)).expect("Failed to read file.");
let mut reader: io::BufReader<File> = io::BufReader::new(file);
let mut content: Vec<u8> = Vec::new();
reader.read_to_end(&mut content).expect("Failed to buffer file.");
// Returning the value:
return ResourceFileBin {path: String::from(file_path), content};
}
pub fn write(file_path: &str, content: Vec<u8>) -> Self {
// Writing to file
write(file_path, &content).expect("Failed writing to file");
// Returning the value:
return ResourceFileBin {path: String::from(file_path), content: content};
}
}