1use crate::prelude::*;
2use std::fs;
3use std::fs::File;
4use std::io::LineWriter;
5use std::io::Write;
6use std::path::Path;
7
8pub fn write_file<P: AsRef<Path> + Clone>(
10 path: P,
11 content: &str,
12) -> Result<(), Box<dyn std::error::Error>> {
13 let path_str = path.clone().as_ref().to_string_lossy().to_string();
14 let file = File::create(path)?;
15 let mut writer = LineWriter::new(file);
16 writer.write_all(content.as_bytes())?;
17 println!(
18 "cargo:warning=dioxus-class::build::write_file: [{}] {}",
19 content.len(), path_str
20 );
21 Ok(())
22}
23
24pub fn generate_classes<P: AsRef<Path> + Clone>(
26 path: P,
27 classes: Vec<Class>,
28) -> Result<(), Box<dyn std::error::Error>> {
29 let path_str = path.clone().as_ref().to_string_lossy().to_string();
30 let file = File::create(path)?;
31 let mut writer = LineWriter::new(file);
32 let mut class_count: usize = 0;
33 let mut size: usize = 0;
34 for class in classes {
35 let str = class.to_class();
36 class_count += 1;
37 size += str.len();
38 writer.write_all(format!("<div class=\"{}\">{}</div>\n", str, class.to_string()).as_bytes())?;
39 }
40 println!(
41 "cargo:warning=dioxus-class::build::generate_classes: {}-[{}] {}",
42 class_count, size, path_str
43 );
44 Ok(())
45}
46
47pub fn check_vec_ending(content: &str) -> bool {
48 for ch in content.chars().rev() {
49 if ch == ']' {
50 return true;
51 } else if vec!['\n', '\r', ' ', '\t'].contains(&ch) {
52 continue;
53 } else {
54 return false;
55 }
56 }
57 false
58}
59
60pub fn check_classes<P: AsRef<Path> + Clone>(
61 path: P,
62) -> Result<(), Box<dyn std::error::Error>> {
63 let content = if path.as_ref().exists() {
64 let lines = fs::read_to_string(path.clone())?;
65 if check_vec_ending(&lines) {
66 return Ok(())
67 }
68 println!("cargo:warning=dioxus-class::build::check_classes: append vec ending");
69 lines + "]\n"
70 } else {
71 println!("cargo:warning=dioxus-class::build::check_classes: create empty file");
72
73 "vec![]\n".to_owned()
74 };
75 write_file(path, &content)
76}