Skip to content

Commit bbd610f

Browse files
committed
feat: add deserialization from toml file
1 parent 2732d6d commit bbd610f

File tree

1 file changed

+80
-17
lines changed

1 file changed

+80
-17
lines changed

build.rs

+80-17
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
use std::io::Write;
1+
use serde::{Deserialize, Serialize};
22
use std::{
33
fs,
44
fs::{DirEntry, Metadata},
5+
io::Write,
56
path::PathBuf,
67
};
78

@@ -49,15 +50,37 @@ fn generate_comunity(path: PathBuf) {
4950
}
5051
let file_path = file.path();
5152
let toml_str = fs::read_to_string(&file_path).unwrap();
53+
let toml_str = toml::from_str::<CommunityItem>(&toml_str).unwrap();
5254
comunities.push((file_path, toml_str));
5355
}
5456
let mut out = fs::File::create("src/extras/comunities.rs").unwrap();
55-
write!(out, "use crate::models::CommunityItem;\npub const OTHER_COMUNITIES: &[CommunityItem] = &[\n").unwrap();
57+
write!(
58+
out,
59+
"use crate::models::CommunityItem;\npub const OTHER_COMUNITIES: &[CommunityItem] = &[\n"
60+
)
61+
.unwrap();
5662
for (_p, t) in comunities {
57-
write!(out, r#"
63+
let CommunityItem {
64+
name,
65+
description,
66+
link,
67+
icon,
68+
brand_src,
69+
brand_alt,
70+
} = t;
71+
write!(
72+
out,
73+
r#"
5874
CommunityItem {{
59-
{t}
60-
}},"#).unwrap();
75+
name: &{name:?},
76+
description: "{description}",
77+
link: "{link}",
78+
icon: "{icon}",
79+
brand_src: "{brand_src}",
80+
brand_alt: "{brand_alt}",
81+
}},"#
82+
)
83+
.unwrap();
6184
}
6285
write!(out, "\n];").unwrap();
6386
}
@@ -89,25 +112,65 @@ fn generate_projects(path: PathBuf) {
89112
}
90113
let file_path = file.path();
91114
let toml_str = fs::read_to_string(&file_path).unwrap();
115+
let toml_str = toml::from_str::<ProjectItem>(&toml_str).unwrap();
92116
projects.push((category.clone(), file_path, toml_str));
93117
});
94118
});
95119

96120
let mut out = fs::File::create("src/extras/projects.rs").unwrap();
97-
write!(out, "use crate::models::ProjectItem;\npub const COMUNITY_PROJECTS: &[ProjectItem] = &[\n").unwrap();
121+
write!(
122+
out,
123+
"use crate::models::ProjectItem;\npub const COMUNITY_PROJECTS: &[ProjectItem] = &[\n"
124+
)
125+
.unwrap();
98126
for (_c, _p, t) in projects {
99-
// name: ,
100-
// description: ,
101-
// link: ,
102-
// brand_src: ,
103-
// button_link: ,
104-
// button_text: ,
105-
// brand_as_letter:
106-
// button_bg_color: ,
107-
write!(out, r#"
127+
let ProjectItem {
128+
name,
129+
description,
130+
link,
131+
brand_src,
132+
button_link,
133+
button_text,
134+
brand_as_letter,
135+
button_bg_color,
136+
} = t;
137+
write!(
138+
out,
139+
r#"
108140
ProjectItem {{
109-
{t}
110-
}},"#).unwrap();
141+
name: &{name:?},
142+
description: "{description}",
143+
link: "{link}",
144+
brand_src: "{brand_src}",
145+
button_link: "{button_link}",
146+
button_text: "{button_text}",
147+
brand_as_letter: {brand_as_letter},
148+
button_bg_color: "{button_bg_color}",
149+
}},"#
150+
)
151+
.unwrap();
111152
}
112153
write!(out, "\n];").unwrap();
113154
}
155+
156+
#[derive(Serialize, Deserialize)]
157+
struct CommunityItem {
158+
pub name: Vec<String>,
159+
pub description: String,
160+
pub link: String,
161+
pub icon: String,
162+
pub brand_src: String,
163+
pub brand_alt: String,
164+
}
165+
166+
#[derive(Serialize, Deserialize)]
167+
struct ProjectItem {
168+
pub name: Vec<String>,
169+
pub description: String,
170+
pub link: String,
171+
pub brand_src: String,
172+
pub button_link: String,
173+
pub button_text: String,
174+
pub brand_as_letter: bool,
175+
pub button_bg_color: String,
176+
}

0 commit comments

Comments
 (0)