Pour tout problème contactez-nous par mail : support@froggit.fr | La FAQ :grey_question: | Rejoignez-nous sur le Chat :speech_balloon:

Skip to content
Snippets Groups Projects
static_view.rs 1.1 KiB
Newer Older
  • Learn to ignore specific revisions
  • use crate::app_state::AppState;
    use std::fs::{create_dir, create_dir_all, File};
    use std::io::Write;
    
    pub fn create_static_view(
        app_state: &AppState,
        category: String,
        filename: String,
        html: String,
    ) -> Result<(), String> {
        let view_path = app_state.env.public_dir.join(&category).join("view");
    
        if !view_path.exists() {
            if let Err(e) = create_dir(&view_path) {
                return Err(format!("Couldn't create directory {:?}: {}", view_path, e));
            }
        }
    
        let d_path = app_state
            .env
            .public_dir
            .join(&category)
            .join("view")
            .join(&filename);
    
        if let Err(e) = create_dir_all(&d_path) {
            return Err(format!("Error creating directory {:?} : {}", d_path, e));
        }
    
        let f_path = d_path.join("index.html");
        match File::create(&f_path) {
            Ok(mut f) => {
                if let Err(e) = f.write_all(html.as_bytes()) {
                    return Err(format!("Error writing to {:?} : {}", f_path, e));
                }
                Ok(())
            }
            Err(e) => Err(format!("Error creating {:?} : {}", f_path, e)),
        }
    }