use crate::errors::READMEError;
use serde::Deserialize;
use std::{borrow::Cow, fs::File, io::Write, path::Path};
pub static README_FILENAME: &str = "README.md";
#[derive(Deserialize)]
pub struct README {
pub package_name: String,
}
impl README {
pub fn new(package_name: &str) -> Self {
Self {
package_name: package_name.to_string(),
}
}
pub fn package_name(&self) -> String {
self.package_name.clone()
}
pub fn exists_at(path: &Path) -> bool {
let mut path = Cow::from(path);
if path.is_dir() {
path.to_mut().push(README_FILENAME);
}
path.exists()
}
pub fn write_to(self, path: &Path) -> Result<(), READMEError> {
let mut path = Cow::from(path);
if path.is_dir() {
path.to_mut().push(README_FILENAME);
}
let mut file = File::create(&path)?;
Ok(file.write_all(self.template().as_bytes())?)
}
fn template(&self) -> String {
format!("# {}\n", self.package_name)
}
}