You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
1.1 KiB
46 lines
1.1 KiB
3 years ago
|
use crate::error::{GempressError, GempressResult};
|
||
|
use std::fs::File;
|
||
|
use std::io::{self, Read};
|
||
|
use std::path::Path;
|
||
|
|
||
|
/// Read a file into Vec
|
||
|
pub fn read_file(file_path: &str) -> Result<Vec<u8>, io::Error> {
|
||
|
let mut file = File::open(file_path)?;
|
||
|
let mut buf = Vec::new();
|
||
|
|
||
|
file.read_to_end(&mut buf)?;
|
||
|
|
||
|
Ok(buf)
|
||
|
}
|
||
|
|
||
|
/// Resolve path to a file, returning index.gmi if a subdirectory is encountered
|
||
|
///
|
||
|
/// If path points to a file, it is returned.
|
||
|
/// If path points to a directory, `./index.gmi` is returned
|
||
|
pub fn resolve_path(path: &Path) -> String {
|
||
|
if path.is_dir() {
|
||
|
path.join("index.gmi").to_string_lossy().into_owned()
|
||
|
} else {
|
||
|
path.to_string_lossy().into_owned()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#[cfg(test)]
|
||
|
mod tests {
|
||
|
use super::*;
|
||
|
|
||
|
#[test]
|
||
|
fn resolve_path_file() {
|
||
|
let path = Path::new("./file.gmi");
|
||
|
|
||
|
assert_eq!(resolve_path(&path), String::from("./file.gmi"));
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn resolve_path_dir() {
|
||
|
let path = Path::new("./");
|
||
|
|
||
|
assert_eq!(resolve_path(&path), String::from("./index.gmi"));
|
||
|
}
|
||
|
}
|