use crate::error::{IronError, Result};
use std::fs;
use std::path::Path;
pub fn read_file_string<P: AsRef<Path>>(path: P) -> Result<String> {
Ok(fs::read_to_string(path)?.trim().to_string())
}
pub fn read_file_u64<P: AsRef<Path>>(path: P) -> Result<u64> {
let content = read_file_string(path)?;
content
.parse()
.map_err(|e| IronError::Parse(format!("Failed to parse u64: {}", e)))
}
pub fn read_file_u32<P: AsRef<Path>>(path: P) -> Result<u32> {
let content = read_file_string(path)?;
content
.parse()
.map_err(|e| IronError::Parse(format!("Failed to parse u32: {}", e)))
}
pub fn read_file_f32<P: AsRef<Path>>(path: P) -> Result<f32> {
let content = read_file_string(path)?;
content
.parse()
.map_err(|e| IronError::Parse(format!("Failed to parse f32: {}", e)))
}
pub fn path_exists<P: AsRef<Path>>(path: P) -> bool {
path.as_ref().exists()
}
pub fn has_read_access<P: AsRef<Path>>(path: P) -> bool {
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
if let Ok(metadata) = fs::metadata(path) {
let permissions = metadata.permissions();
permissions.mode() & 0o444 != 0
} else {
false
}
}
#[cfg(not(unix))]
{
path.as_ref().exists()
}
}
pub fn write_file_string<P: AsRef<Path>>(path: P, content: &str) -> Result<()> {
fs::write(path, content)?;
Ok(())
}