1use std::{collections::HashMap, path::Path};
41
42use crate::Run::{
43 Constant::ProfileDefault,
44 Definition::{Profile, RunProfileConfig},
45 Error::{Error, Result},
46};
47
48pub fn load_profiles(config_path:&Path) -> Result<HashMap<String, Profile>> {
58 let content =
59 std::fs::read_to_string(config_path).map_err(|_e| Error::ConfigNotFound(config_path.to_path_buf()))?;
60
61 let config:serde_json::Value = serde_json::from_str(&content).map_err(|e| Error::ConfigParse(e.to_string()))?;
62
63 let mut profiles = HashMap::new();
64
65 if let Some(profiles_obj) = config.get("profiles").and_then(|v| v.as_object()) {
66 for (name, value) in profiles_obj {
67 if let Ok(profile) = serde_json::from_value::<Profile>(value.clone()) {
68 profiles.insert(name.clone(), profile);
69 }
70 }
71 }
72
73 Ok(profiles)
74}
75
76pub fn resolve_name(name:&str, aliases:&HashMap<String, String>) -> String {
87 aliases.get(name).cloned().unwrap_or_else(|| name.to_string())
88}
89
90pub fn default_name() -> String { ProfileDefault.to_string() }
96
97pub fn validate(profile:&Profile) -> (Vec<String>, Vec<String>) {
107 let mut warnings = Vec::new();
108 let mut issues = Vec::new();
109
110 if profile.description.is_none() {
112 warnings.push("Profile has no description".to_string());
113 }
114
115 if profile.workbench.is_none() {
117 issues.push("Profile has no workbench type specified".to_string());
118 }
119
120 if profile.env.is_none() || profile.env.as_ref().unwrap().is_empty() {
122 warnings.push("Profile has no environment variables defined".to_string());
123 }
124
125 if let Some(run_config) = &profile.run_config {
127 if run_config.live_reload_port == 0 {
128 issues.push("Live-reload port cannot be 0".to_string());
129 }
130 }
131
132 (warnings, issues)
133}
134
135pub fn merge(base:&Profile, override_profile:&Profile) -> Profile {
146 Profile {
147 name:base.name.clone(),
148 description:override_profile.description.clone().or_else(|| base.description.clone()),
149 workbench:override_profile.workbench.clone().or_else(|| base.workbench.clone()),
150 env:{
151 let mut env = base.env.clone().unwrap_or_default();
152 if let Some(override_env) = &override_profile.env {
153 for (key, value) in override_env {
154 env.insert(key.clone(), value.clone());
155 }
156 }
157 if env.is_empty() { None } else { Some(env) }
158 },
159 run_config:override_profile.run_config.clone().or_else(|| base.run_config.clone()),
160 }
161}
162
163pub fn from_env(name:&str, env_vars:HashMap<String, String>) -> Profile {
174 Profile {
175 name:name.to_string(),
176 description:Some("Environment-based profile".to_string()),
177 workbench:env_vars.get("Workbench").cloned(),
178 env:Some(env_vars),
179 run_config:None,
180 }
181}
182
183pub fn get_run_config(profile:&Profile) -> RunProfileConfig {
193 profile
194 .run_config
195 .clone()
196 .unwrap_or_else(|| RunProfileConfig { hot_reload:true, watch:true, live_reload_port:3001, features:None })
197}