use crate::{
error::{Error, Result},
CONFIG_FILE,
};
use std::fs::{self, File};
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::process::Command;
pub(crate) fn read_file<P: AsRef<Path>>(path: P) -> Result<String> {
let mut buf = String::new();
File::open(&path)?.read_to_string(&mut buf)?;
Ok(buf)
}
pub fn write_file(path: &Path, bytes: &[u8]) -> Result<()> {
if let Some(p) = path.parent() {
fs::create_dir_all(p)?;
}
let mut f = File::create(path)?;
f.write_all(bytes)?;
Ok(())
}
pub fn find_root() -> Result<PathBuf> {
let mut path: PathBuf = std::env::current_dir().unwrap();
let look_for = Path::new(CONFIG_FILE);
loop {
path.push(look_for);
if path.is_file() {
path.pop();
return Ok(path);
}
if !(path.pop() && path.pop()) {
return Err(Error::Io(std::io::Error::new(
std::io::ErrorKind::NotFound,
"Unable to find an \"mdoc.toml\" file.",
)));
}
}
}
pub fn get_author_name() -> Option<String> {
let output = Command::new("git")
.args(&["config", "--get", "user.name"])
.output()
.ok()?;
if output.status.success() {
Some(String::from_utf8_lossy(&output.stdout).trim().to_owned())
} else {
None
}
}
pub fn data_dir() -> PathBuf {
dirs::data_dir()
.expect("Unable to get the data directory.")
.join("mdoc")
}
pub fn kebab(s: impl AsRef<str>) -> String {
s.as_ref()
.chars()
.filter_map(|ch| {
if ch.is_alphanumeric() || ch == '_' || ch == '-' {
Some(ch.to_ascii_lowercase())
} else if ch.is_whitespace() {
Some('-')
} else {
None
}
})
.collect()
}