This commit is contained in:
Qyriad 2026-01-28 19:30:59 +01:00
parent 34a9c3f864
commit 551e5a7851
4 changed files with 163 additions and 33 deletions

View file

@ -1,4 +1,4 @@
use std::sync::Arc;
use std::sync::{Arc, LazyLock};
pub(crate) mod prelude {
#![allow(unused_imports)]
@ -43,6 +43,8 @@ use serde::{Deserialize, Serialize};
use crate::source::SourceFile;
pub const ASCII_WHITESPACE: &[char] = &['\t', '\n', '\x0C', '\r', ' '];
#[derive(Debug, Clone, PartialEq, Hash, Serialize, Deserialize)]
pub struct DefinitionWithLocation {
pub file: Box<Path>,
@ -75,8 +77,8 @@ pub fn get_where(option_name: &str, configuration_nix: &Path) -> Result<Box<Path
pub fn get_highest_prio(
option_name: &str,
mut source: SourceFile,
) -> Result<SourceLine, BoxDynError> {
source: SourceFile,
) -> Result<(i64, SourceLine), BoxDynError> {
// Get the current highest priority.
let expr: OsString = [
@ -93,7 +95,7 @@ pub fn get_highest_prio(
.into_command()
.output_checked_utf8()?;
let stdout = output.stdout();
let highest_prio = stdout.trim();
let highest_prio = i64::from_str(stdout.trim())?;
let needle = format!("lib.mkOverride ({})", highest_prio);
@ -111,27 +113,77 @@ pub fn get_highest_prio(
)
});
Ok(line.clone())
Ok((highest_prio, line.clone()))
}
pub fn get_next_prio_line(
source: SourceFile,
option_name: Arc<str>,
last_line_def: SourceLine,
new_prio: i64,
new_value: Arc<str>,
) -> Result<SourceLine, BoxDynError> {
if !last_line_def.text.ends_with(';') {
todo!();
}
let next_line = source.line(last_line_def.line.next())?;
if next_line.text.trim() != "}" {
todo!();
}
let (indentation, _rest) = last_line_def.text.split_at(
last_line_def
.text
.find(|ch: char| !ch.is_ascii_whitespace())
.unwrap_or_default(),
);
// FIXME: fix indentation
let new_text = format!("{indentation}{option_name} = lib.mkOverride ({new_prio}) ({new_value});",);
let new_line = SourceLine {
line: next_line.line.next(),
path: source.path(),
text: Arc::from(new_text),
};
Ok(new_line)
}
pub fn write_next_prio(
mut source: SourceFile,
last_line_def: SourceLine,
last_pri: i64,
new_prio: i64,
new_text: Arc<str>,
) -> Result<(), BoxDynError> {
//let lines = source.lines()?;
let old_text = last_line_def.text();
let new_text = old_text.replace(last_pri.to_string().as_str(), new_prio.to_string().as_str());
let open_brace_line = source.line(last_line_def.line.prev())?.text();
let close_brace_line = source.line(last_line_def.line.next())?.text();
let new_mod_start = SourceLine {
line: last_line_def.line.next(),
path: source.path(),
text: open_brace_line,
};
let new_line = SourceLine {
line: Line::from_index(last_line_def.line.index() + 1),
line: new_mod_start.line.next(),
path: source.path(),
text: Arc::from(new_text),
};
let new_mod_end = SourceLine {
line: new_line.line.next(),
path: source.path(),
text: close_brace_line,
};
source.insert_line(new_line.line, new_line.text())?;
dbg!(&new_mod_start.text());
source.insert_lines(&[
new_mod_start,
new_line,
new_mod_end,
])?;
//source.insert_line(new_line.line, new_line.text())?;
Ok(())
}