o402 0.1.2

OpenAI-compatible gateway, paid with x402.
//! `o402 init --path gateway.toml`

use std::fs;
use std::path::Path;

use crate::error::AppError;

/// Bytes of the unpaid example, baked in so `init` does not depend on cwd.
const UNPAID_EXAMPLE: &str =
    include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/unpaid.example.toml"));

/// Write the unpaid example. Refuses to overwrite.
///
/// # Errors
///
/// Returns [`AppError::AlreadyExists`] when `path` exists, or [`AppError::Write`]
/// when the filesystem rejects the write.
pub(crate) fn run(path: &Path) -> Result<(), AppError> {
    if path.exists() {
        return Err(AppError::AlreadyExists {
            path: path.to_path_buf(),
        });
    }
    fs::write(path, UNPAID_EXAMPLE).map_err(|source| AppError::Write {
        path: path.to_path_buf(),
        source,
    })?;
    #[allow(
        clippy::print_stdout,
        reason = "init reports the written path to the operator"
    )]
    {
        println!("wrote {}", path.display());
    }
    Ok(())
}