1use strum::EnumProperty as _;
4use strum_macros::{AsRefStr, Display, EnumMessage, EnumProperty, EnumString, VariantArray};
5
6use crate::templates::{Template, Type};
7
8#[derive(
10 AsRefStr, Clone, Default, Debug, Display, EnumMessage, EnumString, Eq, PartialEq, VariantArray,
11)]
12pub enum FrontendType {
13 #[default]
15 #[strum(ascii_case_insensitive, serialize = "contract", message = "Contract")]
16 Contract,
17 #[strum(ascii_case_insensitive, serialize = "chain", message = "Chain")]
19 Chain,
20}
21impl Type<FrontendTemplate> for FrontendType {
22 fn default_template(&self) -> Option<FrontendTemplate> {
23 match &self {
24 FrontendType::Contract => Some(FrontendTemplate::Typink),
25 FrontendType::Chain => Some(FrontendTemplate::CreateDotApp),
26 }
27 }
28}
29
30#[derive(
32 AsRefStr,
33 Clone,
34 Debug,
35 Default,
36 Display,
37 EnumMessage,
38 EnumProperty,
39 EnumString,
40 Eq,
41 Hash,
42 PartialEq,
43 VariantArray,
44)]
45pub enum FrontendTemplate {
46 #[default]
48 #[strum(
49 serialize = "typink",
50 message = "Typink",
51 detailed_message = "The ultimate toolkit for dApps development on Polkadot, powered by https://dedot.dev",
52 props(Command = "create-typink@latest", Type = "Contract",)
53 )]
54 Typink,
55 #[strum(
58 serialize = "inkathon",
59 message = "Inkathon",
60 detailed_message = "Next generation full-stack boilerplate for ink! smart contracts running on PolkaVM.",
61 props(Command = "create-inkathon-app@latest", Type = "Contract",)
62 )]
63 Inkathon,
64 #[strum(
67 serialize = "create-dot-app",
68 message = "create-dot-app",
69 detailed_message = "A command-line interface (CLI) tool designed to streamline the development process for Polkadot-based decentralized applications (dApps)",
70 props(Command = "create-dot-app@latest", Type = "Chain",)
71 )]
72 CreateDotApp,
73}
74
75impl Template for FrontendTemplate {}
76
77impl FrontendTemplate {
78 pub fn command(&self) -> Option<&str> {
80 self.get_str("Command")
81 }
82}
83
84#[cfg(test)]
85mod tests {
86 use super::*;
87 use FrontendTemplate::*;
88 use std::{collections::HashMap, str::FromStr};
89 use strum::VariantArray;
90
91 fn templates_names() -> HashMap<String, FrontendTemplate> {
92 HashMap::from([
93 ("inkathon".to_string(), Inkathon),
94 ("typink".to_string(), Typink),
95 ("create-dot-app".to_string(), CreateDotApp),
96 ])
97 }
98
99 fn templates_commands() -> HashMap<String, &'static str> {
100 HashMap::from([
101 ("inkathon".to_string(), "create-inkathon-app@latest"),
102 ("typink".to_string(), "create-typink@latest"),
103 ("create-polkadot-dapp".to_string(), "create-polkadot-dapp@latest"),
104 ("create-dot-app".to_string(), "create-dot-app@latest"),
105 ])
106 }
107
108 fn templates_description() -> HashMap<FrontendTemplate, &'static str> {
109 HashMap::from([
110 (
111 Inkathon,
112 "Next generation full-stack boilerplate for ink! smart contracts running on PolkaVM.",
113 ),
114 (
115 Typink,
116 "The ultimate toolkit for dApps development on Polkadot, powered by https://dedot.dev",
117 ),
118 (
119 CreateDotApp,
120 "A command-line interface (CLI) tool designed to streamline the development process for Polkadot-based decentralized applications (dApps)",
121 ),
122 ])
123 }
124
125 #[test]
126 fn test_convert_string_to_template() {
127 let template_names = templates_names();
128 assert_eq!(FrontendTemplate::from_str("").unwrap_or_default(), Typink);
130 for template in FrontendTemplate::VARIANTS {
132 assert_eq!(
133 &FrontendTemplate::from_str(template.as_ref()).unwrap(),
134 template_names.get(&template.to_string()).unwrap()
135 );
136 }
137 }
138
139 #[test]
140 fn test_repository_command() {
141 let template_urls = templates_commands();
142 for template in FrontendTemplate::VARIANTS {
143 assert_eq!(
144 &template.command().unwrap(),
145 template_urls.get(&template.to_string()).unwrap()
146 );
147 }
148 }
149
150 #[test]
151 fn test_templates_description() {
152 let templates_description = templates_description();
153 for template in FrontendTemplate::VARIANTS {
154 assert_eq!(template.description(), templates_description[template]);
155 }
156 }
157
158 #[test]
159 fn test_templates_of_type() {
160 let mut frontend_type = FrontendType::Contract;
161 assert_eq!(frontend_type.templates(), [&Typink, &Inkathon]);
162 frontend_type = FrontendType::Chain;
163 assert_eq!(frontend_type.templates(), [&CreateDotApp]);
164 }
165}