webapp-rs 0.1.0

A simple CLI tool to create webapps (only support firefox and linux for now.
use anyhow::Result;
use rand::Rng;

/// Génère un code unique à partir du nom de la webapp
/// Format: Nom + 4 caractères alphanumériques
/// Ex: "GitHub" -> "GitHub-A1B2"
pub fn generate_codename(name: &str) -> Result<String> {
    let alpha: String = name.chars().filter(|c| c.is_alphabetic()).collect();

    if alpha.is_empty() {
        anyhow::bail!("Name must contain at least one alphabetic character");
    }

    let num: String = rand::thread_rng()
        .sample_iter(&rand::distributions::Alphanumeric)
        .take(4)
        .map(|c| c as char)
        .collect();

    Ok(format!("{}-{}", alpha, num))
}

/// Normalise une URL pour s'assurer qu'elle a un protocole
/// Ex: "google.com" -> "http://google.com"
pub fn normalize_url(url: &str) -> Result<String> {
    let url = url.trim();

    if url.is_empty() {
        anyhow::bail!("URL cannot be empty");
    }

    if !url.contains("://") {
        Ok(format!("http://{}", url))
    } else {
        Ok(url.to_string())
    }
}

// Copie récursivement un dossier et son contenu
// pub fn copy_dir(src: &Path, dst: &Path) -> Result<()> {
//     if !src.exists() {
//         anyhow::bail!("Source directory does not exist: {:?}", src);
//     }

//     for entry in std::fs::read_dir(src)? {
//         let entry = entry?;
//         let src_path = entry.path();
//         let dst_path = dst.join(entry.file_name());

//         if entry.path().is_dir() {
//             std::fs::create_dir_all(&dst_path)?;
//             copy_dir(&src_path, &dst_path)?;
//         } else {
//             std::fs::copy(&src_path, &dst_path)?;
//         }
//     }
//     Ok(())
// }