Partially implement config includes

Subsequent commits will add merging for all leftover sections.
This commit is contained in:
Ivan Molodetskikh
2025-09-27 11:20:43 +03:00
parent 264289cd41
commit b3ae3adbb7
15 changed files with 694 additions and 358 deletions

View File

@@ -1103,8 +1103,7 @@ mod tests {
#[test]
fn rule_color_can_override_base_gradient() {
let config = Config::parse(
"test.kdl",
let config = Config::parse_mem(
r##"
// Start with gradient set.
layout {
@@ -1127,7 +1126,7 @@ mod tests {
)
.unwrap();
let mut border = config.resolve_layout().border;
let mut border = config.layout.border;
for rule in &config.window_rules {
border.merge_with(&rule.border);
}
@@ -1151,8 +1150,7 @@ mod tests {
#[test]
fn rule_color_can_override_rule_gradient() {
let config = Config::parse(
"test.kdl",
let config = Config::parse_mem(
r##"
// Start with gradient set.
layout {
@@ -1196,7 +1194,7 @@ mod tests {
)
.unwrap();
let mut border = config.resolve_layout().border;
let mut border = config.layout.border;
let mut tab_indicator_rule = TabIndicatorRule::default();
for rule in &config.window_rules {
border.merge_with(&rule.border);

99
niri-config/src/error.rs Normal file
View File

@@ -0,0 +1,99 @@
use std::error::Error;
use std::fmt;
use std::path::PathBuf;
use miette::Diagnostic;
#[derive(Debug)]
pub struct ConfigParseResult<T, E> {
pub config: Result<T, E>,
// We always try to return includes for the file watcher.
//
// If the main config is valid, but an included file fails to parse, config will be an Err(),
// but includes will still be filled, so that fixing just the included file is enough to
// trigger a reload.
pub includes: Vec<PathBuf>,
}
/// Error type that chains main errors with include errors.
///
/// Allows miette's Report formatting to have main + include errors all in one.
#[derive(Debug)]
pub struct ConfigIncludeError {
pub main: knuffel::Error,
pub includes: Vec<knuffel::Error>,
}
impl<T, E> ConfigParseResult<T, E> {
pub fn from_err(err: E) -> Self {
Self {
config: Err(err),
includes: Vec::new(),
}
}
pub fn map_config_res<U, V>(
self,
f: impl FnOnce(Result<T, E>) -> Result<U, V>,
) -> ConfigParseResult<U, V> {
ConfigParseResult {
config: f(self.config),
includes: self.includes,
}
}
}
impl fmt::Display for ConfigIncludeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.main, f)
}
}
impl Error for ConfigIncludeError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
self.main.source()
}
}
impl Diagnostic for ConfigIncludeError {
fn code<'a>(&'a self) -> Option<Box<dyn fmt::Display + 'a>> {
self.main.code()
}
fn severity(&self) -> Option<miette::Severity> {
self.main.severity()
}
fn help<'a>(&'a self) -> Option<Box<dyn fmt::Display + 'a>> {
self.main.help()
}
fn url<'a>(&'a self) -> Option<Box<dyn fmt::Display + 'a>> {
self.main.url()
}
fn source_code(&self) -> Option<&dyn miette::SourceCode> {
self.main.source_code()
}
fn labels(&self) -> Option<Box<dyn Iterator<Item = miette::LabeledSpan> + '_>> {
self.main.labels()
}
fn diagnostic_source(&self) -> Option<&dyn Diagnostic> {
self.main.diagnostic_source()
}
fn related<'a>(&'a self) -> Option<Box<dyn Iterator<Item = &'a dyn Diagnostic> + 'a>> {
let main_related = self.main.related();
let includes_iter = self.includes.iter().map(|err| err as &'a dyn Diagnostic);
let iter: Box<dyn Iterator<Item = &'a dyn Diagnostic> + 'a> = match main_related {
Some(main) => Box::new(main.chain(includes_iter)),
None => Box::new(includes_iter),
};
Some(iter)
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -36,6 +36,17 @@ impl Default for Cursor {
}
}
#[derive(knuffel::Decode, Debug, Clone, PartialEq)]
pub struct ScreenshotPath(#[knuffel(argument)] pub Option<String>);
impl Default for ScreenshotPath {
fn default() -> Self {
Self(Some(String::from(
"~/Pictures/Screenshots/Screenshot from %Y-%m-%d %H-%M-%S.png",
)))
}
}
#[derive(knuffel::Decode, Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct HotkeyOverlay {
#[knuffel(child)]

View File

@@ -1,5 +1,5 @@
use std::fs;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
struct KdlCodeBlock {
filename: String,
@@ -84,7 +84,7 @@ fn wiki_docs_parses() {
must_fail,
} in code_blocks
{
if let Err(error) = niri_config::Config::parse(&filename, &code) {
if let Err(error) = niri_config::Config::parse(Path::new(&filename), &code).config {
if !must_fail {
errors.push(format!(
"Error parsing wiki KDL code block at {}:{}: {:?}",

View File

@@ -59,7 +59,7 @@ impl MappedLayer {
clock: Clock,
config: &Config,
) -> Self {
let mut shadow_config = config.resolve_layout().shadow;
let mut shadow_config = config.layout.shadow;
// Shadows for layer surfaces need to be explicitly enabled.
shadow_config.on = false;
shadow_config.merge_with(&rules.shadow);
@@ -76,7 +76,7 @@ impl MappedLayer {
}
pub fn update_config(&mut self, config: &Config) {
let mut shadow_config = config.resolve_layout().shadow;
let mut shadow_config = config.layout.shadow;
// Shadows for layer surfaces need to be explicitly enabled.
shadow_config.on = false;
shadow_config.merge_with(&self.rules.shadow);

View File

@@ -576,7 +576,7 @@ impl HitType {
impl Options {
fn from_config(config: &Config) -> Self {
Self {
layout: config.resolve_layout(),
layout: config.layout.clone(),
animations: config.animations.clone(),
gestures: config.gestures,
overview: config.overview,

View File

@@ -2357,9 +2357,9 @@ fn removing_all_outputs_preserves_empty_named_workspaces() {
#[test]
fn config_change_updates_cached_sizes() {
let mut config = Config::default();
let border = config.layout.border.get_or_insert_with(Default::default);
let border = &mut config.layout.border;
border.off = false;
border.width = Some(FloatOrInt(2.));
border.width = 2.;
let mut layout = Layout::new(Clock::default(), &config);
@@ -2371,7 +2371,7 @@ fn config_change_updates_cached_sizes() {
}
.apply(&mut layout);
config.layout.border.as_mut().unwrap().width = Some(FloatOrInt(4.));
config.layout.border.width = 4.;
layout.update_config(&config);
layout.verify_invariants();
@@ -2380,7 +2380,7 @@ fn config_change_updates_cached_sizes() {
#[test]
fn preset_height_change_removes_preset() {
let mut config = Config::default();
config.layout.preset_window_heights = Some(vec![PresetSize::Fixed(1), PresetSize::Fixed(2)]);
config.layout.preset_window_heights = vec![PresetSize::Fixed(1), PresetSize::Fixed(2)];
let mut layout = Layout::new(Clock::default(), &config);
@@ -2401,7 +2401,7 @@ fn preset_height_change_removes_preset() {
}
// Leave only one.
config.layout.preset_window_heights = Some(vec![PresetSize::Fixed(1)]);
config.layout.preset_window_heights = vec![PresetSize::Fixed(1)];
layout.update_config(&config);

View File

@@ -24,7 +24,7 @@ use niri::utils::spawning::{
REMOVE_ENV_RUST_BACKTRACE, REMOVE_ENV_RUST_LIB_BACKTRACE,
};
use niri::utils::{cause_panic, version, watcher, xwayland, IS_SYSTEMD_SERVICE};
use niri_config::ConfigPath;
use niri_config::{Config, ConfigPath};
use niri_ipc::socket::SOCKET_PATH_ENV;
use portable_atomic::Ordering;
use sd_notify::NotifyState;
@@ -101,7 +101,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
Sub::Validate { config } => {
tracy_client::Client::start();
config_path(config).load()?;
config_path(config).load().config?;
info!("config is valid");
return Ok(());
}
@@ -148,10 +148,11 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let config_path = config_path(cli.config);
env::remove_var("NIRI_CONFIG");
let (config_created_at, config_load_result) = config_path.load_or_create();
let config_errored = config_load_result.is_err();
let mut config = config_load_result
.map_err(|err| warn!("{err:?}"))
.unwrap_or_default();
let config_errored = config_load_result.config.is_err();
let mut config = config_load_result.config.unwrap_or_else(|err| {
warn!("{err:?}");
Config::load_default()
});
let spawn_at_startup = mem::take(&mut config.spawn_at_startup);
let spawn_sh_at_startup = mem::take(&mut config.spawn_sh_at_startup);

View File

@@ -3,7 +3,7 @@ use std::time::Duration;
use insta::assert_snapshot;
use niri_config::animations::{Curve, EasingParams, Kind};
use niri_config::{Config, FloatOrInt};
use niri_config::Config;
use niri_ipc::SizeChange;
use smithay::utils::{Point, Size};
use wayland_client::protocol::wl_surface::WlSurface;
@@ -74,7 +74,7 @@ fn set_up() -> Fixture {
});
let mut config = Config::default();
config.layout.gaps = Some(FloatOrInt(0.0));
config.layout.gaps = 0.0;
config.animations.window_resize.anim.kind = LINEAR;
config.animations.window_movement.0.kind = LINEAR;

View File

@@ -795,7 +795,7 @@ window-rule {
max-width 300
}
"##;
let config = Config::parse("test.kdl", config).unwrap();
let config = Config::parse_mem(config).unwrap();
let mut f = Fixture::with_config(config);
f.add_output(1, (1920, 1080));
f.add_output(2, (1280, 720));

View File

@@ -281,7 +281,7 @@ window-rule {{
snapshot_desc.push(format!("config:{config}"));
let config = Config::parse("config.kdl", &config).unwrap();
let config = Config::parse_mem(&config).unwrap();
let mut f = Fixture::with_config(config);
f.add_output(1, (1280, 720));
@@ -574,7 +574,7 @@ layout {
snapshot_desc.push(format!("config:{config}"));
let config = Config::parse("config.kdl", &config).unwrap();
let config = Config::parse_mem(&config).unwrap();
let mut f = Fixture::with_config(config);
f.add_output(1, (1280, 720));

View File

@@ -607,7 +607,7 @@ mod tests {
#[track_caller]
fn check(config: &str, action: Action) -> String {
let config = Config::parse("test.kdl", config).unwrap();
let config = Config::parse_mem(config).unwrap();
if let Some((key, title)) = format_bind(&config.binds.0, &action) {
let key = key.map(|key| key_name(false, ModKey::Super, &key));
let key = key.as_deref().unwrap_or("(not bound)");

View File

@@ -213,7 +213,7 @@ pub fn expand_home(path: &Path) -> anyhow::Result<Option<PathBuf>> {
}
pub fn make_screenshot_path(config: &Config) -> anyhow::Result<Option<PathBuf>> {
let Some(path) = &config.screenshot_path else {
let Some(path) = &config.screenshot_path.0 else {
return Ok(None);
};

View File

@@ -128,7 +128,7 @@ pub fn setup(state: &mut State, config_path: &ConfigPath) {
// Parsing the config actually takes > 20 ms on my beefy machine, so let's do it on the
// watcher thread.
let process = |path: &ConfigPath| {
path.load().map_err(|err| {
path.load().config.map_err(|err| {
warn!("{err:?}");
})
};