francoisgib_webserver 1.0.3

HTTP Webserver
Documentation
use std::{fs::File, io::Read, path::Path};

/// Reads the entire contents of a file into a `String`.
///
/// # Arguments
/// * `path` - A string slice representing the path to the file.
///
/// # Returns
/// * `Ok(String)` containing the file contents if successful.
/// * `Err(String)` with an error message if the file could not be opened or read.
pub fn read_file(path: &str) -> Result<Vec<u8>, String> {
    let mut file = File::open(path).map_err(|e| format!("Failed to open file: {}", e))?;
    let mut buffer = vec![];
    file.read_to_end(&mut buffer)
        .map_err(|e| format!("Failed to read file: {}", e))?;
    Ok(buffer)
}

/// Extracts the file extension from a file path, if any.
///
/// # Arguments
/// * `path` - A string slice representing the path to the file.
///
/// # Returns
/// * `Some(String)` the file extension if present.
/// * `None` if the file has no extension.
pub fn get_file_extension(path: &str) -> Option<String> {
    // let parts: Vec<&str> = path.split('.').collect();
    // if parts.len() > 1 {
    //     Some(parts[parts.len() - 1].to_string())
    // } else {
    //     None
    // }
    Path::new(path)
        .extension()
        .and_then(|ext| ext.to_str())
        .map(|s| s.to_string())
}