Skip to content

Commit

Permalink
Fix serializing StyleType::Custom.
Browse files Browse the repository at this point in the history
`StyleType` should serialize as a TOML table to account for
`StyleType::Custom` taking a value.

I added two attributes to `StyleType` to hint that the `toml` crate
should serialize it as a table instead of a simple key = value.
  • Loading branch information
joshuamegnauth54 committed Jun 30, 2023
1 parent d35c578 commit cc1db3e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/configs/types/config_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ use crate::{Language, StyleType};

#[derive(Serialize, Deserialize, Default)]
pub struct ConfigSettings {
pub style: Arc<StyleType>,
pub language: Language,
pub notifications: Notifications,
// This field should be last so that the TOML for StyleType can serialize properly
pub style: Arc<StyleType>,
}
48 changes: 46 additions & 2 deletions src/gui/styles/types/custom_style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ mod tests {
deserialize_from_path, serialize_to_path, CustomPalette, CustomStyle, Palette,
PaletteExtension,
};
use crate::translations::types::language::Language;
use crate::{translations::types::language::Language, StyleType};
use iced::color;
use serde::{Deserialize, Serialize};
use serde_test::{assert_tokens, Token};
Expand Down Expand Up @@ -200,11 +200,55 @@ mod tests {
})
}

// Test that split deserialization works for `CustomStyle`.
// This is different than testing that `StyleType` properly deserializes.
#[test]
fn test_styletype_split_de() {
fn test_customstyle_split_de() {
let style_test = catppuccin_style();
// This is only used for the test which requires an &'static str.
let path: &'static str = Box::leak(style_path().into_boxed_str());
assert_tokens(&style_test, &[Token::String(path)]);
}

// Ensure that StyleType itself still deserializes properly
#[test]
fn test_styletype_unit_split_de() {
// Unit variant without a struct
assert_tokens(
&StyleType::DeepSea,
&[
Token::Struct {
name: "StyleType",
len: 1,
},
Token::Str("style"),
Token::Str("DeepSea"),
Token::StructEnd,
],
);
}

// Test that StyleType::Custom successfully deserializes.
// Originally, StyleType::Custom did not ser/de correctly because of how TOML
// handles enums.
#[test]
fn test_styletype_custom_split_de() {
// CustomStyle
// This is only used for the test so leaking it is fine.
let path = &*Box::leak(style_path().into_boxed_str());
assert_tokens(
&StyleType::Custom(catppuccin_style().0),
&[
Token::Struct {
name: "StyleType",
len: 2,
},
Token::Str("style"),
Token::Str("Custom"),
Token::Str("path"),
Token::Str(path),
Token::StructEnd,
],
);
}
}
1 change: 1 addition & 0 deletions src/gui/styles/types/style_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use super::custom_style::{deserialize_from_path, serialize_to_path, CustomStyle}

/// Used to specify the kind of style of the application
#[derive(Clone, Serialize, Deserialize, Debug, Hash, PartialEq)]
#[serde(tag = "style", content = "path")]
pub enum StyleType {
Night,
Day,
Expand Down

0 comments on commit cc1db3e

Please sign in to comment.