use std::{fs::File, io::Write, path::Path};
use thiserror::Error;
use tracing::info;
use typstify_core::Config;
#[derive(Debug, Error)]
pub enum RobotsError {
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
}
pub type Result<T> = std::result::Result<T, RobotsError>;
#[derive(Debug)]
pub struct RobotsGenerator {
config: Config,
}
impl RobotsGenerator {
#[must_use]
pub fn new(config: Config) -> Self {
Self { config }
}
pub fn generate(&self, output_dir: &Path) -> Result<()> {
if !self.config.robots.enabled {
return Ok(());
}
info!("generating robots.txt");
let path = output_dir.join("robots.txt");
let mut file = File::create(path)?;
writeln!(file, "User-agent: *")?;
for path in &self.config.robots.disallow {
writeln!(file, "Disallow: {path}")?;
}
for path in &self.config.robots.allow {
writeln!(file, "Allow: {path}")?;
}
let sitemap_url = format!("{}/sitemap.xml", self.config.site.base_url);
writeln!(file, "Sitemap: {sitemap_url}")?;
Ok(())
}
}