parent
dd8f120152
commit
e94a9ca351
1 changed files with 10 additions and 8 deletions
18
src/main.rs
18
src/main.rs
|
@ -17,7 +17,6 @@
|
|||
|
||||
mod cli;
|
||||
|
||||
use std::path::Path;
|
||||
use std::{
|
||||
fs::File,
|
||||
io::{BufWriter, Write},
|
||||
|
@ -28,30 +27,33 @@ use clap::Parser;
|
|||
fn main() -> Result<(), Error> {
|
||||
let cli = cli::Args::parse();
|
||||
|
||||
change_env_layout(&cli.file, &cli.section_names)?;
|
||||
change_env_layout(&cli)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn change_env_layout(file: &Path, section_names: &[String]) -> Result<(), Error> {
|
||||
let content = std::fs::read_to_string(file).map_err(|_| Error::OpenFile)?;
|
||||
fn change_env_layout(args: &cli::Args) -> Result<(), Error> {
|
||||
let content = std::fs::read_to_string(&args.file).map_err(|_| Error::OpenFile)?;
|
||||
|
||||
let mut writer = File::create(file)
|
||||
let mut writer = File::create(&args.file)
|
||||
.map_err(|_| Error::OpenFile)
|
||||
.map(BufWriter::new)?;
|
||||
|
||||
let mut current_section_name: Option<String> = None;
|
||||
let mut current_section_name: Option<Vec<String>> = None;
|
||||
|
||||
for line in content.split_inclusive('\n') {
|
||||
let new_line = if is_section_end(line) {
|
||||
current_section_name = None;
|
||||
line.to_string()
|
||||
} else if let Some(section_info) = line.strip_prefix("### ") {
|
||||
current_section_name = section_info.split_whitespace().next().map(String::from);
|
||||
current_section_name = section_info
|
||||
.split_whitespace()
|
||||
.next()
|
||||
.map(|r| r.split(',').map(str::trim).map(String::from).collect());
|
||||
line.to_string()
|
||||
} else if let Some(cur_name) = current_section_name.clone() {
|
||||
let trimmed_line = line.trim_start_matches(['#', ' ']);
|
||||
if section_names.contains(&cur_name) {
|
||||
if args.section_names.iter().any(|sn| cur_name.contains(sn)) {
|
||||
String::from(trimmed_line)
|
||||
} else {
|
||||
format!("# {}", trimmed_line)
|
||||
|
|
Loading…
Reference in a new issue