code style fixes and nesting fixes
All checks were successful
Version Check / check-version (pull_request) Successful in 3s
All checks were successful
Version Check / check-version (pull_request) Successful in 3s
This commit is contained in:
38
src/app.rs
38
src/app.rs
@@ -40,7 +40,7 @@ pub struct App {
|
|||||||
impl App {
|
impl App {
|
||||||
/// Initializes a new application instance with the provided variables.
|
/// Initializes a new application instance with the provided variables.
|
||||||
pub fn new(vars: Vec<ConfigItem>) -> Self {
|
pub fn new(vars: Vec<ConfigItem>) -> 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 {
|
Self {
|
||||||
vars,
|
vars,
|
||||||
selected: 0,
|
selected: 0,
|
||||||
@@ -150,18 +150,17 @@ impl App {
|
|||||||
|
|
||||||
/// Commits the current text in the input buffer back to the selected variable's value.
|
/// Commits the current text in the input buffer back to the selected variable's value.
|
||||||
pub fn commit_input(&mut self) {
|
pub fn commit_input(&mut self) {
|
||||||
if let Some(var) = self.vars.get_mut(self.selected) {
|
if let Some(var) = self.vars.get_mut(self.selected)
|
||||||
if !var.is_group {
|
&& !var.is_group {
|
||||||
var.value = Some(self.input.value().to_string());
|
var.value = Some(self.input.value().to_string());
|
||||||
var.status = crate::format::ItemStatus::Modified;
|
var.status = crate::format::ItemStatus::Modified;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/// Transitions the application into Insert Mode with a specific variant.
|
/// Transitions the application into Insert Mode with a specific variant.
|
||||||
pub fn enter_insert(&mut self, variant: InsertVariant) {
|
pub fn enter_insert(&mut self, variant: InsertVariant) {
|
||||||
if let Some(var) = self.vars.get(self.selected) {
|
if let Some(var) = self.vars.get(self.selected)
|
||||||
if !var.is_group {
|
&& !var.is_group {
|
||||||
self.save_undo_state();
|
self.save_undo_state();
|
||||||
self.mode = Mode::Insert;
|
self.mode = Mode::Insert;
|
||||||
match variant {
|
match variant {
|
||||||
@@ -179,7 +178,6 @@ impl App {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/// Commits the current input and transitions the application into Normal Mode.
|
/// Commits the current input and transitions the application into Normal Mode.
|
||||||
pub fn enter_normal(&mut self) {
|
pub fn enter_normal(&mut self) {
|
||||||
@@ -229,8 +227,8 @@ impl App {
|
|||||||
for var in self.vars.iter_mut() {
|
for var in self.vars.iter_mut() {
|
||||||
if var.path.starts_with(&base) {
|
if var.path.starts_with(&base) {
|
||||||
// We need to find the index segment that matches this array
|
// We need to find the index segment that matches this array
|
||||||
if let Some((b, i, suffix)) = find_array_segment(&var.path, &base) {
|
if let Some((b, i, suffix)) = find_array_segment(&var.path, &base)
|
||||||
if b == base && i > removed_idx {
|
&& b == base && i > removed_idx {
|
||||||
let new_idx = i - 1;
|
let new_idx = i - 1;
|
||||||
var.path = format!("{}[{}]{}", base, new_idx, suffix);
|
var.path = format!("{}[{}]{}", base, new_idx, suffix);
|
||||||
// Also update key if it matches the old index exactly
|
// Also update key if it matches the old index exactly
|
||||||
@@ -241,7 +239,6 @@ impl App {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// 4. Adjust selection
|
// 4. Adjust selection
|
||||||
if self.selected >= self.vars.len() && !self.vars.is_empty() {
|
if self.selected >= self.vars.len() && !self.vars.is_empty() {
|
||||||
@@ -279,9 +276,9 @@ impl App {
|
|||||||
|
|
||||||
// 1. Shift all items in this array that have index >= new_idx
|
// 1. Shift all items in this array that have index >= new_idx
|
||||||
for var in self.vars.iter_mut() {
|
for var in self.vars.iter_mut() {
|
||||||
if var.path.starts_with(&base) {
|
if var.path.starts_with(&base)
|
||||||
if let Some((b, i)) = parse_index(&var.path) {
|
&& let Some((b, i)) = parse_index(&var.path)
|
||||||
if b == base && i >= new_idx {
|
&& b == base && i >= new_idx {
|
||||||
var.path = format!("{}[{}]", base, i + 1);
|
var.path = format!("{}[{}]", base, i + 1);
|
||||||
// Also update key if it was just the index
|
// Also update key if it was just the index
|
||||||
if var.key == format!("[{}]", i) {
|
if var.key == format!("[{}]", i) {
|
||||||
@@ -289,8 +286,6 @@ impl App {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 2. Insert new item
|
// 2. Insert new item
|
||||||
let new_item = ConfigItem {
|
let new_item = ConfigItem {
|
||||||
@@ -354,13 +349,12 @@ impl App {
|
|||||||
fn parse_index(path: &str) -> Option<(&str, usize)> {
|
fn parse_index(path: &str) -> Option<(&str, usize)> {
|
||||||
if let Some(end) = path.rfind(']') {
|
if let Some(end) = path.rfind(']') {
|
||||||
let segment = &path[..=end];
|
let segment = &path[..=end];
|
||||||
if let Some(start) = segment.rfind('[') {
|
if let Some(start) = segment.rfind('[')
|
||||||
if let Ok(idx) = segment[start + 1..end].parse::<usize>() {
|
&& let Ok(idx) = segment[start + 1..end].parse::<usize>() {
|
||||||
// Return the base and index
|
// Return the base and index
|
||||||
return Some((&path[..start], idx));
|
return Some((&path[..start], idx));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -370,12 +364,10 @@ fn find_array_segment<'a>(path: &'a str, base: &str) -> Option<(&'a str, usize,
|
|||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
let remaining = &path[base.len()..];
|
let remaining = &path[base.len()..];
|
||||||
if remaining.starts_with('[') {
|
if remaining.starts_with('[')
|
||||||
if let Some(end) = remaining.find(']') {
|
&& let Some(end) = remaining.find(']')
|
||||||
if let Ok(idx) = remaining[1..end].parse::<usize>() {
|
&& let Ok(idx) = remaining[1..end].parse::<usize>() {
|
||||||
return Some((&path[..base.len()], idx, &remaining[end + 1..]));
|
return Some((&path[..base.len()], idx, &remaining[end + 1..]));
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -162,13 +162,11 @@ pub fn load_config() -> Config {
|
|||||||
config_dir.push("mould");
|
config_dir.push("mould");
|
||||||
config_dir.push("config.toml");
|
config_dir.push("config.toml");
|
||||||
|
|
||||||
if config_dir.exists() {
|
if config_dir.exists()
|
||||||
if let Ok(content) = fs::read_to_string(config_dir) {
|
&& let Ok(content) = fs::read_to_string(config_dir)
|
||||||
if let Ok(config) = toml::from_str(&content) {
|
&& let Ok(config) = toml::from_str(&content) {
|
||||||
return config;
|
return config;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
Config::default()
|
Config::default()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -135,7 +135,7 @@ fn json_to_xml(value: &Value) -> String {
|
|||||||
let mut s = String::new();
|
let mut s = String::new();
|
||||||
for (k, v) in map {
|
for (k, v) in map {
|
||||||
if k == "$text" {
|
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() {
|
} else if let Some(arr) = v.as_array() {
|
||||||
for item in arr {
|
for item in arr {
|
||||||
s.push_str(&format!("<{}>", k));
|
s.push_str(&format!("<{}>", k));
|
||||||
@@ -437,8 +437,8 @@ mod tests {
|
|||||||
"port_str": "8080",
|
"port_str": "8080",
|
||||||
"is_enabled": true,
|
"is_enabled": true,
|
||||||
"is_enabled_str": "true",
|
"is_enabled_str": "true",
|
||||||
"float_num": 3.14,
|
"float_num": 42.42,
|
||||||
"float_str": "3.14"
|
"float_str": "42.42"
|
||||||
});
|
});
|
||||||
|
|
||||||
flatten(&json, "", 0, "", &mut vars);
|
flatten(&json, "", 0, "", &mut vars);
|
||||||
@@ -466,10 +466,10 @@ mod tests {
|
|||||||
assert_eq!(unflattened["is_enabled_str"].as_str(), Some("true"));
|
assert_eq!(unflattened["is_enabled_str"].as_str(), Some("true"));
|
||||||
|
|
||||||
assert!(unflattened["float_num"].is_number(), "float_num should be a number");
|
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!(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]
|
#[test]
|
||||||
|
|||||||
@@ -68,7 +68,7 @@ impl FormatHandler for IniHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
conf.write_to_file(path)
|
conf.write_to_file(path)
|
||||||
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))
|
.map_err(io::Error::other)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -99,21 +99,15 @@ pub fn detect_format(path: &Path, override_format: Option<String>) -> FormatType
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let file_name = path.file_name().unwrap_or_default().to_string_lossy();
|
let ext = path.extension().and_then(|s| s.to_str()).unwrap_or_default();
|
||||||
if file_name.ends_with(".json") {
|
match ext {
|
||||||
FormatType::Json
|
"json" => FormatType::Json,
|
||||||
} else if file_name.ends_with(".yaml") || file_name.ends_with(".yml") {
|
"yaml" | "yml" => FormatType::Yaml,
|
||||||
FormatType::Yaml
|
"toml" => FormatType::Toml,
|
||||||
} else if file_name.ends_with(".toml") {
|
"xml" => FormatType::Xml,
|
||||||
FormatType::Toml
|
"ini" => FormatType::Ini,
|
||||||
} else if file_name.ends_with(".xml") {
|
"properties" => FormatType::Properties,
|
||||||
FormatType::Xml
|
_ => FormatType::Env,
|
||||||
} else if file_name.ends_with(".ini") {
|
|
||||||
FormatType::Ini
|
|
||||||
} else if file_name.ends_with(".properties") {
|
|
||||||
FormatType::Properties
|
|
||||||
} else {
|
|
||||||
FormatType::Env
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -23,15 +23,15 @@ impl FormatHandler for PropertiesHandler {
|
|||||||
let parts: Vec<&str> = path.split('.').collect();
|
let parts: Vec<&str> = path.split('.').collect();
|
||||||
let mut current_path = String::new();
|
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() {
|
if !current_path.is_empty() {
|
||||||
current_path.push('.');
|
current_path.push('.');
|
||||||
}
|
}
|
||||||
current_path.push_str(parts[i]);
|
current_path.push_str(part);
|
||||||
|
|
||||||
if groups.insert(current_path.clone()) {
|
if groups.insert(current_path.clone()) {
|
||||||
vars.push(ConfigItem {
|
vars.push(ConfigItem {
|
||||||
key: parts[i].to_string(),
|
key: part.to_string(),
|
||||||
path: current_path.clone(),
|
path: current_path.clone(),
|
||||||
value: None,
|
value: None,
|
||||||
template_value: None,
|
template_value: None,
|
||||||
@@ -73,11 +73,11 @@ impl FormatHandler for PropertiesHandler {
|
|||||||
.or(var.template_value.as_deref())
|
.or(var.template_value.as_deref())
|
||||||
.unwrap_or("");
|
.unwrap_or("");
|
||||||
prop_writer.write(&var.path, val)
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -50,14 +50,12 @@ pub fn determine_output_path(input: &Path) -> PathBuf {
|
|||||||
if file_name == rule.template_suffix {
|
if file_name == rule.template_suffix {
|
||||||
return input.with_file_name(rule.active_suffix);
|
return input.with_file_name(rule.active_suffix);
|
||||||
}
|
}
|
||||||
} else {
|
} else if file_name == rule.template_suffix {
|
||||||
if file_name == rule.template_suffix {
|
|
||||||
return input.with_file_name(rule.active_suffix);
|
return input.with_file_name(rule.active_suffix);
|
||||||
} else if let Some(base) = file_name.strip_suffix(rule.template_suffix) {
|
} else if let Some(base) = file_name.strip_suffix(rule.template_suffix) {
|
||||||
return input.with_file_name(format!("{}{}", base, rule.active_suffix));
|
return input.with_file_name(format!("{}{}", base, rule.active_suffix));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
input.with_extension(format!(
|
input.with_extension(format!(
|
||||||
"{}.out",
|
"{}.out",
|
||||||
|
|||||||
@@ -198,8 +198,8 @@ where
|
|||||||
|
|
||||||
/// Adds a missing item from the template to the active configuration.
|
/// Adds a missing item from the template to the active configuration.
|
||||||
fn add_missing_item(&mut self) {
|
fn add_missing_item(&mut self) {
|
||||||
if let Some(var) = self.app.vars.get_mut(self.app.selected) {
|
if let Some(var) = self.app.vars.get_mut(self.app.selected)
|
||||||
if var.status == crate::format::ItemStatus::MissingFromActive {
|
&& var.status == crate::format::ItemStatus::MissingFromActive {
|
||||||
var.status = crate::format::ItemStatus::Present;
|
var.status = crate::format::ItemStatus::Present;
|
||||||
if !var.is_group {
|
if !var.is_group {
|
||||||
var.value = var.template_value.clone();
|
var.value = var.template_value.clone();
|
||||||
@@ -207,7 +207,6 @@ where
|
|||||||
self.app.sync_input_with_selected();
|
self.app.sync_input_with_selected();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/// Delegates key events to the `tui_input` handler during active editing.
|
/// Delegates key events to the `tui_input` handler during active editing.
|
||||||
fn handle_insert_mode(&mut self, key: KeyEvent) -> io::Result<()> {
|
fn handle_insert_mode(&mut self, key: KeyEvent) -> io::Result<()> {
|
||||||
|
|||||||
@@ -150,8 +150,8 @@ pub fn draw(f: &mut Frame, app: &mut App, config: &Config) {
|
|||||||
Span::styled(val, value_style),
|
Span::styled(val, value_style),
|
||||||
];
|
];
|
||||||
|
|
||||||
if let Some(t_val) = &var.template_value {
|
if let Some(t_val) = &var.template_value
|
||||||
if Some(t_val) != var.value.as_ref() {
|
&& Some(t_val) != var.value.as_ref() {
|
||||||
let t_style = if is_selected {
|
let t_style = if is_selected {
|
||||||
Style::default().fg(theme.bg_normal()).add_modifier(Modifier::DIM)
|
Style::default().fg(theme.bg_normal()).add_modifier(Modifier::DIM)
|
||||||
} else {
|
} 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));
|
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)
|
ListItem::new(vec![Line::from(key_spans), Line::from(val_spans)]).style(item_style)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user