From 3d4a040d32b1136673bb8591599511de31212339 Mon Sep 17 00:00:00 2001 From: Eskaan Date: Mon, 5 Jun 2023 13:39:22 +0200 Subject: [PATCH 1/2] Replace optional values with defaults when unset --- leftwm-layouts/src/layouts/layout.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/leftwm-layouts/src/layouts/layout.rs b/leftwm-layouts/src/layouts/layout.rs index 185059b..3f9e55d 100644 --- a/leftwm-layouts/src/layouts/layout.rs +++ b/leftwm-layouts/src/layouts/layout.rs @@ -82,17 +82,21 @@ pub struct Layout { pub name: LayoutName, /// Flips the entire result of tiles as a whole if specified to be anything other than [`Flip::None`] + #[serde(default)] pub flip: Flip, /// Rotate the entire result of tiles as a whole, if specified to be anything other than [`Rotation::North`] + #[serde(default)] pub rotate: Rotation, /// Defines the layouts behavior if certain "columns" (eg. main, stack, or second-stack) are empty. /// See [`Reserve`] for more information. + #[serde(default)] pub reserve: Reserve, /// Configuration concerning the [`Main`], [`Stack`], and [`SecondStack`] columns. /// See [`Columns`] for more information. + #[serde(default)] pub columns: Columns, } @@ -320,6 +324,7 @@ impl Default for Layout { /// in a `MainAndStack` layout configuration, the [`Flip`] property could be set to [`Flip::Vertical`], /// which results in the columns being flipped, **but not their contents**. #[derive(Serialize, Deserialize, Debug, PartialEq, Clone)] +#[serde(default)] pub struct Columns { /// How the columns should be flipped, does not apply to their contents pub flip: Flip, @@ -332,6 +337,7 @@ pub struct Columns { /// will not have a main column. For example, in single-column /// layouts like `EvenVertical`, `Monocle`, etc. /// See [`Main`] for more information. + #[serde(default = "default_opt_main")] pub main: Option
, /// Configurations concerning the `stack` column. @@ -350,6 +356,10 @@ pub struct Columns { pub second_stack: Option, } +fn default_opt_main() -> Option
{ + Some(Main::default()) +} + impl Default for Columns { fn default() -> Self { Self { @@ -426,6 +436,7 @@ impl Default for Stack { /// Configurations concerning the `second_stack` column #[derive(Serialize, Deserialize, Debug, PartialEq, Clone)] +#[serde(default)] pub struct SecondStack { /// Flip modifier to apply only to the `second_stack` columns' contents pub flip: Flip, From 696b2ba28629c926d82387dc6fc78a06e38fb666 Mon Sep 17 00:00:00 2001 From: Eskaan Date: Mon, 5 Jun 2023 14:45:12 +0200 Subject: [PATCH 2/2] Optimize implementation --- leftwm-layouts/src/layouts/layout.rs | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/leftwm-layouts/src/layouts/layout.rs b/leftwm-layouts/src/layouts/layout.rs index 3f9e55d..931ef61 100644 --- a/leftwm-layouts/src/layouts/layout.rs +++ b/leftwm-layouts/src/layouts/layout.rs @@ -76,27 +76,24 @@ type LayoutName = String; /// The [`Layout`] allows to describe various types of "fixed" layouts used by a dynamic tiling manager. /// Those include layouts like `MainAndStack`, `Fibonacci`, `Dwindle`, `CenterMain`, etc. #[derive(Serialize, Deserialize, Debug, PartialEq, Clone)] +#[serde(default)] pub struct Layout { /// Name and identifier of the layout. /// This is user chosen and no two layouts can have the same name. pub name: LayoutName, /// Flips the entire result of tiles as a whole if specified to be anything other than [`Flip::None`] - #[serde(default)] pub flip: Flip, /// Rotate the entire result of tiles as a whole, if specified to be anything other than [`Rotation::North`] - #[serde(default)] pub rotate: Rotation, /// Defines the layouts behavior if certain "columns" (eg. main, stack, or second-stack) are empty. /// See [`Reserve`] for more information. - #[serde(default)] pub reserve: Reserve, /// Configuration concerning the [`Main`], [`Stack`], and [`SecondStack`] columns. /// See [`Columns`] for more information. - #[serde(default)] pub columns: Columns, } @@ -337,7 +334,6 @@ pub struct Columns { /// will not have a main column. For example, in single-column /// layouts like `EvenVertical`, `Monocle`, etc. /// See [`Main`] for more information. - #[serde(default = "default_opt_main")] pub main: Option
, /// Configurations concerning the `stack` column. @@ -356,10 +352,6 @@ pub struct Columns { pub second_stack: Option, } -fn default_opt_main() -> Option
{ - Some(Main::default()) -} - impl Default for Columns { fn default() -> Self { Self { @@ -374,6 +366,7 @@ impl Default for Columns { /// Configurations concerning the `main` column #[derive(Serialize, Deserialize, Debug, PartialEq, Clone)] +#[serde(default)] pub struct Main { /// The default amount of windows to occupy the `main` column (default: `1`) pub count: usize, @@ -409,6 +402,7 @@ impl Default for Main { /// Configurations concerning the `stack` column #[derive(Serialize, Deserialize, Debug, PartialEq, Clone)] +#[serde(default)] pub struct Stack { /// Flip modifier to apply only to the `stack` columns' contents pub flip: Flip,