diff --git a/src/app.rs b/src/app.rs index c09f05f..df32417 100644 --- a/src/app.rs +++ b/src/app.rs @@ -40,7 +40,7 @@ pub struct App { impl App { /// Initializes a new application instance with the provided variables. pub fn new(vars: Vec) -> Self { - let initial_input = vars.get(0).and_then(|v| v.value.clone()).unwrap_or_default(); + let initial_input = vars.first().and_then(|v| v.value.clone()).unwrap_or_default(); Self { vars, selected: 0, @@ -150,18 +150,17 @@ impl App { /// Commits the current text in the input buffer back to the selected variable's value. pub fn commit_input(&mut self) { - if let Some(var) = self.vars.get_mut(self.selected) { - if !var.is_group { + if let Some(var) = self.vars.get_mut(self.selected) + && !var.is_group { var.value = Some(self.input.value().to_string()); var.status = crate::format::ItemStatus::Modified; } - } } /// Transitions the application into Insert Mode with a specific variant. pub fn enter_insert(&mut self, variant: InsertVariant) { - if let Some(var) = self.vars.get(self.selected) { - if !var.is_group { + if let Some(var) = self.vars.get(self.selected) + && !var.is_group { self.save_undo_state(); self.mode = Mode::Insert; match variant { @@ -178,7 +177,6 @@ impl App { } } } - } } /// Commits the current input and transitions the application into Normal Mode. @@ -229,8 +227,8 @@ impl App { for var in self.vars.iter_mut() { if var.path.starts_with(&base) { // We need to find the index segment that matches this array - if let Some((b, i, suffix)) = find_array_segment(&var.path, &base) { - if b == base && i > removed_idx { + if let Some((b, i, suffix)) = find_array_segment(&var.path, &base) + && b == base && i > removed_idx { let new_idx = i - 1; var.path = format!("{}[{}]{}", base, new_idx, suffix); // Also update key if it matches the old index exactly @@ -238,7 +236,6 @@ impl App { var.key = format!("[{}]", new_idx); } } - } } } } @@ -279,17 +276,15 @@ impl App { // 1. Shift all items in this array that have index >= new_idx for var in self.vars.iter_mut() { - if var.path.starts_with(&base) { - if let Some((b, i)) = parse_index(&var.path) { - if b == base && i >= new_idx { + if var.path.starts_with(&base) + && let Some((b, i)) = parse_index(&var.path) + && b == base && i >= new_idx { var.path = format!("{}[{}]", base, i + 1); // Also update key if it was just the index if var.key == format!("[{}]", i) { var.key = format!("[{}]", i + 1); } } - } - } } // 2. Insert new item @@ -354,12 +349,11 @@ impl App { fn parse_index(path: &str) -> Option<(&str, usize)> { if let Some(end) = path.rfind(']') { let segment = &path[..=end]; - if let Some(start) = segment.rfind('[') { - if let Ok(idx) = segment[start + 1..end].parse::() { + if let Some(start) = segment.rfind('[') + && let Ok(idx) = segment[start + 1..end].parse::() { // Return the base and index return Some((&path[..start], idx)); } - } } None } @@ -370,12 +364,10 @@ fn find_array_segment<'a>(path: &'a str, base: &str) -> Option<(&'a str, usize, return None; } let remaining = &path[base.len()..]; - if remaining.starts_with('[') { - if let Some(end) = remaining.find(']') { - if let Ok(idx) = remaining[1..end].parse::() { + if remaining.starts_with('[') + && let Some(end) = remaining.find(']') + && let Ok(idx) = remaining[1..end].parse::() { return Some((&path[..base.len()], idx, &remaining[end + 1..])); } - } - } None } diff --git a/src/config.rs b/src/config.rs index bed2196..4eaf84e 100644 --- a/src/config.rs +++ b/src/config.rs @@ -162,13 +162,11 @@ pub fn load_config() -> Config { config_dir.push("mould"); config_dir.push("config.toml"); - if config_dir.exists() { - if let Ok(content) = fs::read_to_string(config_dir) { - if let Ok(config) = toml::from_str(&content) { + if config_dir.exists() + && let Ok(content) = fs::read_to_string(config_dir) + && let Ok(config) = toml::from_str(&content) { return config; } - } - } } Config::default() } diff --git a/src/format/hierarchical.rs b/src/format/hierarchical.rs index a50ebc8..b642bfd 100644 --- a/src/format/hierarchical.rs +++ b/src/format/hierarchical.rs @@ -135,7 +135,7 @@ fn json_to_xml(value: &Value) -> String { let mut s = String::new(); for (k, v) in map { if k == "$text" { - s.push_str(&v.as_str().unwrap_or("")); + s.push_str(v.as_str().unwrap_or("")); } else if let Some(arr) = v.as_array() { for item in arr { s.push_str(&format!("<{}>", k)); @@ -437,8 +437,8 @@ mod tests { "port_str": "8080", "is_enabled": true, "is_enabled_str": "true", - "float_num": 3.14, - "float_str": "3.14" + "float_num": 42.42, + "float_str": "42.42" }); flatten(&json, "", 0, "", &mut vars); @@ -466,10 +466,10 @@ mod tests { assert_eq!(unflattened["is_enabled_str"].as_str(), Some("true")); assert!(unflattened["float_num"].is_number(), "float_num should be a number"); - assert_eq!(unflattened["float_num"].as_f64(), Some(3.14)); + assert_eq!(unflattened["float_num"].as_f64(), Some(42.42)); assert!(unflattened["float_str"].is_string(), "float_str should be a string"); - assert_eq!(unflattened["float_str"].as_str(), Some("3.14")); + assert_eq!(unflattened["float_str"].as_str(), Some("42.42")); } #[test] diff --git a/src/format/ini.rs b/src/format/ini.rs index dbda8d7..2582739 100644 --- a/src/format/ini.rs +++ b/src/format/ini.rs @@ -68,7 +68,7 @@ impl FormatHandler for IniHandler { } } conf.write_to_file(path) - .map_err(|e| io::Error::new(io::ErrorKind::Other, e)) + .map_err(io::Error::other) } } diff --git a/src/format/mod.rs b/src/format/mod.rs index a86d35e..ed5f0e2 100644 --- a/src/format/mod.rs +++ b/src/format/mod.rs @@ -99,21 +99,15 @@ pub fn detect_format(path: &Path, override_format: Option) -> FormatType } } - let file_name = path.file_name().unwrap_or_default().to_string_lossy(); - if file_name.ends_with(".json") { - FormatType::Json - } else if file_name.ends_with(".yaml") || file_name.ends_with(".yml") { - FormatType::Yaml - } else if file_name.ends_with(".toml") { - FormatType::Toml - } else if file_name.ends_with(".xml") { - FormatType::Xml - } else if file_name.ends_with(".ini") { - FormatType::Ini - } else if file_name.ends_with(".properties") { - FormatType::Properties - } else { - FormatType::Env + let ext = path.extension().and_then(|s| s.to_str()).unwrap_or_default(); + match ext { + "json" => FormatType::Json, + "yaml" | "yml" => FormatType::Yaml, + "toml" => FormatType::Toml, + "xml" => FormatType::Xml, + "ini" => FormatType::Ini, + "properties" => FormatType::Properties, + _ => FormatType::Env, } } diff --git a/src/format/properties.rs b/src/format/properties.rs index a521a2f..642d657 100644 --- a/src/format/properties.rs +++ b/src/format/properties.rs @@ -23,15 +23,15 @@ impl FormatHandler for PropertiesHandler { let parts: Vec<&str> = path.split('.').collect(); let mut current_path = String::new(); - for i in 0..parts.len().saturating_sub(1) { + for (i, part) in parts.iter().enumerate().take(parts.len().saturating_sub(1)) { if !current_path.is_empty() { current_path.push('.'); } - current_path.push_str(parts[i]); + current_path.push_str(part); if groups.insert(current_path.clone()) { vars.push(ConfigItem { - key: parts[i].to_string(), + key: part.to_string(), path: current_path.clone(), value: None, template_value: None, @@ -73,11 +73,11 @@ impl FormatHandler for PropertiesHandler { .or(var.template_value.as_deref()) .unwrap_or(""); prop_writer.write(&var.path, val) - .map_err(|e| io::Error::other(e))?; + .map_err(io::Error::other)?; } } - prop_writer.finish().map_err(|e| io::Error::other(e)) + prop_writer.finish().map_err(io::Error::other) } } diff --git a/src/resolver.rs b/src/resolver.rs index 906819e..7b19da6 100644 --- a/src/resolver.rs +++ b/src/resolver.rs @@ -50,12 +50,10 @@ pub fn determine_output_path(input: &Path) -> PathBuf { if file_name == rule.template_suffix { return input.with_file_name(rule.active_suffix); } - } else { - if file_name == rule.template_suffix { - return input.with_file_name(rule.active_suffix); - } else if let Some(base) = file_name.strip_suffix(rule.template_suffix) { - return input.with_file_name(format!("{}{}", base, rule.active_suffix)); - } + } else if file_name == rule.template_suffix { + return input.with_file_name(rule.active_suffix); + } else if let Some(base) = file_name.strip_suffix(rule.template_suffix) { + return input.with_file_name(format!("{}{}", base, rule.active_suffix)); } } diff --git a/src/runner.rs b/src/runner.rs index 1fecf95..47b5d95 100644 --- a/src/runner.rs +++ b/src/runner.rs @@ -198,15 +198,14 @@ where /// Adds a missing item from the template to the active configuration. fn add_missing_item(&mut self) { - if let Some(var) = self.app.vars.get_mut(self.app.selected) { - if var.status == crate::format::ItemStatus::MissingFromActive { + if let Some(var) = self.app.vars.get_mut(self.app.selected) + && var.status == crate::format::ItemStatus::MissingFromActive { var.status = crate::format::ItemStatus::Present; if !var.is_group { var.value = var.template_value.clone(); } self.app.sync_input_with_selected(); } - } } /// Delegates key events to the `tui_input` handler during active editing. diff --git a/src/ui.rs b/src/ui.rs index f53ac9f..629e26b 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -150,8 +150,8 @@ pub fn draw(f: &mut Frame, app: &mut App, config: &Config) { Span::styled(val, value_style), ]; - if let Some(t_val) = &var.template_value { - if Some(t_val) != var.value.as_ref() { + if let Some(t_val) = &var.template_value + && Some(t_val) != var.value.as_ref() { let t_style = if is_selected { Style::default().fg(theme.bg_normal()).add_modifier(Modifier::DIM) } else { @@ -159,7 +159,6 @@ pub fn draw(f: &mut Frame, app: &mut App, config: &Config) { }; val_spans.push(Span::styled(format!(" [Def: {}]", t_val), t_style)); } - } ListItem::new(vec![Line::from(key_spans), Line::from(val_spans)]).style(item_style) }