config: Move workspace into its own module
Browse files at this commit82697773f8e5507dc3982645f805a011996f7cb4Parent: f64cb6c4 changed files +67 -66
- niri-config/src/lib.rs +2 -0
- niri-config/src/misc.rs +0 -64
- niri-config/src/workspace.rs +63 -0
- src/layout/tests.rs +2 -2
niri-config/src/lib.rsmodified+2 -0
| @@ -20,6 +20,7 @@ pub mod misc; | |||
| 20 | pub mod output; | 20 | pub mod output; |
| 21 | pub mod utils; | 21 | pub mod utils; |
| 22 | pub mod window_rule; | 22 | pub mod window_rule; |
| 23 | +pub mod workspace; | ||
| 23 | 24 | ||
| 24 | pub use crate::animations::{ | 25 | pub use crate::animations::{ |
| 25 | Animation, AnimationCurve, AnimationKind, Animations, EasingParams, SpringParams, | 26 | Animation, AnimationCurve, AnimationKind, Animations, EasingParams, SpringParams, |
| @@ -35,6 +36,7 @@ pub use crate::misc::*; | |||
| 35 | pub use crate::output::{Output, OutputName, Outputs, Position, Vrr}; | 36 | pub use crate::output::{Output, OutputName, Outputs, Position, Vrr}; |
| 36 | pub use crate::utils::FloatOrInt; | 37 | pub use crate::utils::FloatOrInt; |
| 37 | pub use crate::window_rule::{FloatingPosition, RelativeTo, WindowRule}; | 38 | pub use crate::window_rule::{FloatingPosition, RelativeTo, WindowRule}; |
| 39 | +pub use crate::workspace::Workspace; | ||
| 38 | 40 | ||
| 39 | #[derive(knuffel::Decode, Debug, PartialEq)] | 41 | #[derive(knuffel::Decode, Debug, PartialEq)] |
| 40 | pub struct Config { | 42 | pub struct Config { |
niri-config/src/misc.rsmodified+0 -64
| @@ -1,5 +1,3 @@ | |||
| 1 | -use knuffel::errors::DecodeError; | ||
| 2 | - | ||
| 3 | use crate::appearance::{Color, WorkspaceShadow, DEFAULT_BACKDROP_COLOR}; | 1 | use crate::appearance::{Color, WorkspaceShadow, DEFAULT_BACKDROP_COLOR}; |
| 4 | use crate::FloatOrInt; | 2 | use crate::FloatOrInt; |
| 5 | 3 | ||
| @@ -105,65 +103,3 @@ impl Default for XwaylandSatellite { | |||
| 105 | } | 103 | } |
| 106 | } | 104 | } |
| 107 | } | 105 | } |
| 108 | - | ||
| 109 | -#[derive(knuffel::Decode, Debug, Clone, PartialEq, Eq)] | ||
| 110 | -pub struct Workspace { | ||
| 111 | - #[knuffel(argument)] | ||
| 112 | - pub name: WorkspaceName, | ||
| 113 | - #[knuffel(child, unwrap(argument))] | ||
| 114 | - pub open_on_output: Option<String>, | ||
| 115 | -} | ||
| 116 | - | ||
| 117 | -#[derive(Debug, Clone, PartialEq, Eq)] | ||
| 118 | -pub struct WorkspaceName(pub String); | ||
| 119 | - | ||
| 120 | -impl<S: knuffel::traits::ErrorSpan> knuffel::DecodeScalar<S> for WorkspaceName { | ||
| 121 | - fn type_check( | ||
| 122 | - type_name: &Option<knuffel::span::Spanned<knuffel::ast::TypeName, S>>, | ||
| 123 | - ctx: &mut knuffel::decode::Context<S>, | ||
| 124 | - ) { | ||
| 125 | - if let Some(type_name) = &type_name { | ||
| 126 | - ctx.emit_error(DecodeError::unexpected( | ||
| 127 | - type_name, | ||
| 128 | - "type name", | ||
| 129 | - "no type name expected for this node", | ||
| 130 | - )); | ||
| 131 | - } | ||
| 132 | - } | ||
| 133 | - | ||
| 134 | - fn raw_decode( | ||
| 135 | - val: &knuffel::span::Spanned<knuffel::ast::Literal, S>, | ||
| 136 | - ctx: &mut knuffel::decode::Context<S>, | ||
| 137 | - ) -> Result<WorkspaceName, DecodeError<S>> { | ||
| 138 | - #[derive(Debug)] | ||
| 139 | - struct WorkspaceNameSet(Vec<String>); | ||
| 140 | - match &**val { | ||
| 141 | - knuffel::ast::Literal::String(ref s) => { | ||
| 142 | - let mut name_set: Vec<String> = match ctx.get::<WorkspaceNameSet>() { | ||
| 143 | - Some(h) => h.0.clone(), | ||
| 144 | - None => Vec::new(), | ||
| 145 | - }; | ||
| 146 | - | ||
| 147 | - if name_set.iter().any(|name| name.eq_ignore_ascii_case(s)) { | ||
| 148 | - ctx.emit_error(DecodeError::unexpected( | ||
| 149 | - val, | ||
| 150 | - "named workspace", | ||
| 151 | - format!("duplicate named workspace: {s}"), | ||
| 152 | - )); | ||
| 153 | - return Ok(Self(String::new())); | ||
| 154 | - } | ||
| 155 | - | ||
| 156 | - name_set.push(s.to_string()); | ||
| 157 | - ctx.set(WorkspaceNameSet(name_set)); | ||
| 158 | - Ok(Self(s.clone().into())) | ||
| 159 | - } | ||
| 160 | - _ => { | ||
| 161 | - ctx.emit_error(DecodeError::unsupported( | ||
| 162 | - val, | ||
| 163 | - "workspace names must be strings", | ||
| 164 | - )); | ||
| 165 | - Ok(Self(String::new())) | ||
| 166 | - } | ||
| 167 | - } | ||
| 168 | - } | ||
| 169 | -} | ||
niri-config/src/workspace.rsadded+63 -0
| @@ -0,0 +1,63 @@ | |||
| 1 | +use knuffel::errors::DecodeError; | ||
| 2 | + | ||
| 3 | +#[derive(knuffel::Decode, Debug, Clone, PartialEq, Eq)] | ||
| 4 | +pub struct Workspace { | ||
| 5 | + #[knuffel(argument)] | ||
| 6 | + pub name: WorkspaceName, | ||
| 7 | + #[knuffel(child, unwrap(argument))] | ||
| 8 | + pub open_on_output: Option<String>, | ||
| 9 | +} | ||
| 10 | + | ||
| 11 | +#[derive(Debug, Clone, PartialEq, Eq)] | ||
| 12 | +pub struct WorkspaceName(pub String); | ||
| 13 | + | ||
| 14 | +impl<S: knuffel::traits::ErrorSpan> knuffel::DecodeScalar<S> for WorkspaceName { | ||
| 15 | + fn type_check( | ||
| 16 | + type_name: &Option<knuffel::span::Spanned<knuffel::ast::TypeName, S>>, | ||
| 17 | + ctx: &mut knuffel::decode::Context<S>, | ||
| 18 | + ) { | ||
| 19 | + if let Some(type_name) = &type_name { | ||
| 20 | + ctx.emit_error(DecodeError::unexpected( | ||
| 21 | + type_name, | ||
| 22 | + "type name", | ||
| 23 | + "no type name expected for this node", | ||
| 24 | + )); | ||
| 25 | + } | ||
| 26 | + } | ||
| 27 | + | ||
| 28 | + fn raw_decode( | ||
| 29 | + val: &knuffel::span::Spanned<knuffel::ast::Literal, S>, | ||
| 30 | + ctx: &mut knuffel::decode::Context<S>, | ||
| 31 | + ) -> Result<WorkspaceName, DecodeError<S>> { | ||
| 32 | + #[derive(Debug)] | ||
| 33 | + struct WorkspaceNameSet(Vec<String>); | ||
| 34 | + match &**val { | ||
| 35 | + knuffel::ast::Literal::String(ref s) => { | ||
| 36 | + let mut name_set: Vec<String> = match ctx.get::<WorkspaceNameSet>() { | ||
| 37 | + Some(h) => h.0.clone(), | ||
| 38 | + None => Vec::new(), | ||
| 39 | + }; | ||
| 40 | + | ||
| 41 | + if name_set.iter().any(|name| name.eq_ignore_ascii_case(s)) { | ||
| 42 | + ctx.emit_error(DecodeError::unexpected( | ||
| 43 | + val, | ||
| 44 | + "named workspace", | ||
| 45 | + format!("duplicate named workspace: {s}"), | ||
| 46 | + )); | ||
| 47 | + return Ok(Self(String::new())); | ||
| 48 | + } | ||
| 49 | + | ||
| 50 | + name_set.push(s.to_string()); | ||
| 51 | + ctx.set(WorkspaceNameSet(name_set)); | ||
| 52 | + Ok(Self(s.clone().into())) | ||
| 53 | + } | ||
| 54 | + _ => { | ||
| 55 | + ctx.emit_error(DecodeError::unsupported( | ||
| 56 | + val, | ||
| 57 | + "workspace names must be strings", | ||
| 58 | + )); | ||
| 59 | + Ok(Self(String::new())) | ||
| 60 | + } | ||
| 61 | + } | ||
| 62 | + } | ||
| 63 | +} | ||
src/layout/tests.rsmodified+2 -2
| @@ -1,8 +1,8 @@ | |||
| 1 | use std::cell::{Cell, OnceCell, RefCell}; | 1 | use std::cell::{Cell, OnceCell, RefCell}; |
| 2 | 2 | ||
| 3 | +use niri_config::workspace::WorkspaceName; | ||
| 3 | use niri_config::{ | 4 | use niri_config::{ |
| 4 | - FloatOrInt, OutputName, TabIndicatorLength, TabIndicatorPosition, WorkspaceName, | 5 | + FloatOrInt, OutputName, TabIndicatorLength, TabIndicatorPosition, WorkspaceReference, |
| 5 | - WorkspaceReference, | ||
| 6 | }; | 6 | }; |
| 7 | use proptest::prelude::*; | 7 | use proptest::prelude::*; |
| 8 | use proptest_derive::Arbitrary; | 8 | use proptest_derive::Arbitrary; |