fixed ordering
All checks were successful
Version Check / check-version (pull_request) Successful in 3s

This commit is contained in:
2026-03-18 17:11:27 +01:00
parent b8c49d4c13
commit b3651aa5dd
4 changed files with 54 additions and 92 deletions

View File

@@ -1,8 +1,7 @@
use super::{ConfigItem, FormatHandler, ItemStatus, ValueType};
use java_properties::{read, write};
use std::collections::HashMap;
use java_properties::{LineContent, PropertiesIter, PropertiesWriter};
use std::fs::File;
use std::io::{self, BufReader};
use std::io::{self, BufReader, BufWriter};
use std::path::Path;
pub struct PropertiesHandler;
@@ -11,69 +10,74 @@ impl FormatHandler for PropertiesHandler {
fn parse(&self, path: &Path) -> io::Result<Vec<ConfigItem>> {
let file = File::open(path)?;
let reader = BufReader::new(file);
let props = read(reader)
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
let iter = PropertiesIter::new(reader);
let mut vars = Vec::new();
let mut groups = std::collections::HashSet::new();
for (path, value) in &props {
// Add groups based on dot notation
let parts: Vec<&str> = path.split('.').collect();
let mut current_path = String::new();
for line_result in iter {
let line = line_result.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
for i in 0..parts.len() - 1 {
if !current_path.is_empty() {
current_path.push('.');
}
current_path.push_str(parts[i]);
if let LineContent::KVPair(path, value) = line.consume_content() {
// Add groups based on dot notation
let parts: Vec<&str> = path.split('.').collect();
let mut current_path = String::new();
if groups.insert(current_path.clone()) {
vars.push(ConfigItem {
key: parts[i].to_string(),
path: current_path.clone(),
value: None,
template_value: None,
default_value: None,
depth: i,
is_group: true,
status: ItemStatus::Present,
value_type: ValueType::Null,
});
for i in 0..parts.len().saturating_sub(1) {
if !current_path.is_empty() {
current_path.push('.');
}
current_path.push_str(parts[i]);
if groups.insert(current_path.clone()) {
vars.push(ConfigItem {
key: parts[i].to_string(),
path: current_path.clone(),
value: None,
template_value: None,
default_value: None,
depth: i,
is_group: true,
status: ItemStatus::Present,
value_type: ValueType::Null,
});
}
}
}
vars.push(ConfigItem {
key: parts.last().unwrap().to_string(),
path: path.clone(),
value: Some(value.clone()),
template_value: Some(value.clone()),
default_value: Some(value.clone()),
depth: parts.len() - 1,
is_group: false,
status: ItemStatus::Present,
value_type: ValueType::String,
});
vars.push(ConfigItem {
key: parts.last().unwrap_or(&"").to_string(),
path: path.clone(),
value: Some(value.clone()),
template_value: Some(value.clone()),
default_value: Some(value.clone()),
depth: parts.len().saturating_sub(1),
is_group: false,
status: ItemStatus::Present,
value_type: ValueType::String,
});
}
}
// Sort by path to keep it organized
vars.sort_by(|a, b| a.path.cmp(&b.path));
// We don't sort here to preserve the original file order!
Ok(vars)
}
fn write(&self, path: &Path, vars: &[ConfigItem]) -> io::Result<()> {
let mut props = HashMap::new();
let file = File::create(path)?;
let writer = BufWriter::new(file);
let mut prop_writer = PropertiesWriter::new(writer);
for var in vars {
if !var.is_group {
let val = var.value.as_deref()
.or(var.template_value.as_deref())
.unwrap_or("");
props.insert(var.path.clone(), val.to_string());
prop_writer.write(&var.path, val)
.map_err(|e| io::Error::other(e))?;
}
}
let file = File::create(path)?;
write(file, &props).map_err(|e| io::Error::new(io::ErrorKind::Other, e))
prop_writer.finish().map_err(|e| io::Error::other(e))
}
}