Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move file and folder io to new modules #46

Merged
merged 1 commit into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::path::Path;

use serde::{Serialize, Deserialize};

use crate::{util::read_file_utf8, web::discord::request::model::Webhook};
use crate::{filesystem::file::read_file_utf8, web::discord::request::model::Webhook};

/// Configure the stats collection
/// - ```save_period_seconds```: periodically save to disc
Expand Down
51 changes: 51 additions & 0 deletions src/filesystem/file.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use std::{fs::File, io::{Read, Write}};

pub fn write_file(path: &str, data: &[u8])
{
let mut file = File::create(path).unwrap();
file.write_all(data).unwrap();
}

pub fn read_file_utf8(path: &str) -> Option<String>
{
let mut file = match File::open(path) {
Err(why) =>
{
crate::debug(format!("error reading file to utf8, {}", why), None);
return None
},
Ok(file) => file,
};

let mut s = String::new();
match file.read_to_string(&mut s) {
Err(why) =>
{
crate::debug(format!("error reading file to utf8, {}", why), None);
None
},
Ok(_) => Some(s)
}
}

pub fn read_file_bytes(path: &str) -> Option<Vec<u8>>
{
let mut file = match File::open(path) {
Err(why) =>
{
crate::debug(format!("error reading file to utf8, {}", why), None);
return None
},
Ok(file) => file,
};

let mut s: Vec<u8> = vec![];
match file.read_to_end(&mut s) {
Err(why) =>
{
crate::debug(format!("error reading file to utf8, {}", why), None);
None
},
Ok(_) => Some(s)
}
}
151 changes: 151 additions & 0 deletions src/filesystem/folder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
use core::fmt;
use std::fs::DirEntry;

use regex::Regex;

#[derive(Debug, Clone)]
pub struct ListDirError
{
pub why: String
}

impl fmt::Display for ListDirError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.why)
}
}

pub fn list_dir(path: String) -> Result<std::fs::ReadDir, ListDirError>
{
match std::fs::read_dir(path)
{
Ok(files) =>
{
Ok(files)
},
Err(why) =>
{
Err(ListDirError { why: format!("{}", why)})
}
}
}

pub fn dir_entry_to_path(d: DirEntry) -> Option<String>
{
let file_os_string = d.file_name();

match file_os_string.to_str()
{
Some(name) => Some(name.to_string()),
None =>
{
crate::debug(format!("could not load file name: {:?}", file_os_string), None);
None
}
}
}

pub fn list_sub_dirs(path: String) -> Vec<String>
{
let mut found_dirs: Vec<String> = vec![];
match std::fs::read_dir(path.clone())
{
Ok(files) =>
{

for file in files
{
let name = match file
{
Ok(d) => dir_entry_to_path(d),
Err(e) =>
{
crate::debug(format!("could not load file name: {}", e), None);
continue
}
};

match name
{
Some(n) =>
{
let p = path.clone()+"/"+&n;
match std::fs::metadata(p.clone())
{
Ok(md) =>
{
match md.is_dir()
{
true => {found_dirs.push(p.clone()); crate::debug(format!("found folder: {}", p), None)},
false => {continue}
}
},
Err(e) =>
{
crate::debug(format!("error getting file: {}", e), None);
continue
}
}
},
None => continue
}
}
},
Err(why) =>
{
crate::debug(format!("Error reading dir {}\n {}", path, why), None);
}
}

found_dirs
}

pub fn list_dir_by(pattern: Option<Regex>, path: String) -> Vec<String>
{
match std::fs::read_dir(path.clone())
{
Ok(files) =>
{
let mut found_files: Vec<String> = vec![];
for file in files
{

let file_name = match file
{
Ok(d) => dir_entry_to_path(d),
Err(e) =>
{
crate::debug(format!("could not load file name: {}", e), None);
continue
}
};

let file_path = match file_name
{
Some(name) => path.clone() + "/" + &name,
None => continue
};

if pattern.clone().is_some()
{
match pattern.clone().unwrap().captures(&file_path)
{
Some(_caps) => {found_files.push(file_path.to_string())},
None => {continue}
}
}
else
{
found_files.push(file_path.to_string())
}
}

return found_files
},
Err(why) =>
{
crate::debug(format!("Error reading dir {}\n {}", path, why), None);
}
}
vec![]
}
2 changes: 2 additions & 0 deletions src/filesystem/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod file;
pub mod folder;
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub mod util;
pub mod pages;
pub mod resources;
pub mod config;
pub mod filesystem;

const MAJOR: &str = env!("CARGO_PKG_VERSION_MAJOR");
const MINOR: &str = env!("CARGO_PKG_VERSION_MINOR");
Expand Down
3 changes: 1 addition & 2 deletions src/pages/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use regex::Regex;

use crate::{util::{list_dir_by, list_sub_dirs, read_file_utf8}, HTML_REGEX};

use crate::{filesystem::folder::{list_dir_by, list_sub_dirs}, HTML_REGEX, filesystem::file::read_file_utf8};
use self::page::Page;

pub mod page;
Expand Down
2 changes: 1 addition & 1 deletion src/pages/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use axum::response::{IntoResponse, Response, Html};
use regex::Regex;
use serde::{Serialize, Deserialize};

use crate::util::read_file_utf8;
use crate::filesystem::file::read_file_utf8;

/// An HTML webpage with a uri and body
///
Expand Down
2 changes: 1 addition & 1 deletion src/resources/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pub mod resource;

use regex::Regex;

use crate::{util::{list_dir_by, list_sub_dirs, read_file_bytes}, HTML_REGEX, RESOURCE_REGEX};
use crate::{filesystem::folder::{list_dir_by, list_sub_dirs}, HTML_REGEX, RESOURCE_REGEX, filesystem::file::read_file_bytes};

use self::resource::{content_type, Resource};

Expand Down
Loading
Loading