Skip to content

Commit

Permalink
Deprecate TOML profile files in favour of JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
staticintlucas committed May 19, 2024
1 parent 14f5599 commit 41b5801
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 52 deletions.
2 changes: 2 additions & 0 deletions keyset-profile/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ workspace = true
[features]
toml = ["serde", "dep:toml"]
json = ["serde", "dep:serde_json"]
serde = ["dep:serde"]

[dependencies]
geom.workspace = true
Expand All @@ -38,3 +39,4 @@ toml = { workspace = true, optional = true }
assert_matches.workspace = true
indoc.workspace = true
isclose = { workspace = true, features = ["euclid"] }
serde_json.workspace = true
7 changes: 4 additions & 3 deletions keyset-profile/src/de/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ mod tests {
use std::error::Error as _;

use assert_matches::assert_matches;
use indoc::indoc;

use crate::Profile;

Expand All @@ -61,11 +60,12 @@ mod tests {
fn error_fmt() {
#[cfg(feature = "toml")]
{
#[allow(deprecated)]
let error = Profile::from_toml("null").unwrap_err();

assert_eq!(
format!("{error}"),
indoc!(
indoc::indoc!(
"
TOML parse error at line 1, column 5
|
Expand All @@ -91,12 +91,13 @@ mod tests {
fn error_source() {
#[cfg(feature = "toml")]
{
#[allow(deprecated)]
let error = Profile::from_toml("null").unwrap_err();

assert!(error.source().is_some());
assert_eq!(
format!("{}", error.source().unwrap()),
indoc!(
indoc::indoc!(
"
TOML parse error at line 1, column 5
|
Expand Down
24 changes: 13 additions & 11 deletions keyset-profile/src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,10 @@ mod tests {

#[test]
fn deserialize_type() {
let cyl: Type = toml::from_str("type = 'cylindrical'\ndepth = 0.5").unwrap();
let sph: Type = toml::from_str("type = 'spherical'\ndepth = 0.8").unwrap();
let chc: Type = toml::from_str("type = 'chiclet'").unwrap();
let flt: Type = toml::from_str("type = 'flat'").unwrap();
let cyl: Type = serde_json::from_str(r#"{ "type": "cylindrical", "depth": 0.5 }"#).unwrap();
let sph: Type = serde_json::from_str(r#"{ "type": "spherical", "depth": 0.8 }"#).unwrap();
let chc: Type = serde_json::from_str(r#"{ "type": "chiclet" }"#).unwrap();
let flt: Type = serde_json::from_str(r#"{ "type": "flat" }"#).unwrap();

assert_matches!(cyl, Type::Cylindrical { depth } if depth.is_close(Length::<Mm>::new(0.5) * DOT_PER_MM));
assert_matches!(sph, Type::Spherical { depth } if depth.is_close(Length::<Mm>::new(0.8) * DOT_PER_MM));
Expand All @@ -273,33 +273,35 @@ mod tests {

#[test]
fn deserialize_scoop_props() {
let scoop_props: ScoopProps = toml::from_str("depth = 0.8").unwrap();
let scoop_props: ScoopProps = serde_json::from_str(r#"{ "depth": 0.8 }"#).unwrap();

assert_is_close!(scoop_props.depth, Length::<Mm>::new(0.8) * DOT_PER_MM);
}

#[test]
fn deserialize_bar_props() {
let bar_props: BarProps =
toml::from_str("width = 3.85\nheight = 0.4\ny-offset = 5.05").unwrap();
serde_json::from_str(r#"{ "width": 3.85, "height": 0.4, "y-offset": 5.05 }"#).unwrap();

assert_is_close!(bar_props.size, Size::<Mm>::new(3.85, 0.4) * DOT_PER_MM);
assert_is_close!(bar_props.y_offset, Length::<Mm>::new(5.05) * DOT_PER_MM);
}

#[test]
fn deserialize_bump_props() {
let bar_props: BumpProps = toml::from_str("diameter = 0.4\ny-offset = -0.2").unwrap();
let bar_props: BumpProps =
serde_json::from_str(r#"{ "diameter": 0.4, "y-offset": -0.2 }"#).unwrap();

assert_is_close!(bar_props.diameter, Length::<Mm>::new(0.4) * DOT_PER_MM);
assert_is_close!(bar_props.y_offset, Length::<Mm>::new(-0.2) * DOT_PER_MM);
}

#[test]
fn deserialize_top_surface() {
let surf: TopSurface =
toml::from_str("width = 11.81\nheight = 13.91\nradius = 1.52\ny-offset = -1.62")
.unwrap();
let surf: TopSurface = serde_json::from_str(
r#"{ "width": 11.81, "height": 13.91, "radius": 1.52, "y-offset": -1.62 }"#,
)
.unwrap();

assert_is_close!(surf.size, Size::new(11.81, 13.91) * DOT_PER_MM);
assert_is_close!(surf.radius, Length::new(1.52) * DOT_PER_MM);
Expand All @@ -309,7 +311,7 @@ mod tests {
#[test]
fn deserialize_bottom_surface() {
let surf: BottomSurface =
toml::from_str("width = 18.29\nheight = 18.29\nradius = 0.38").unwrap();
serde_json::from_str(r#"{ "width": 18.29, "height": 18.29, "radius": 0.38 }"#).unwrap();

assert_is_close!(surf.size, Size::splat(18.29) * DOT_PER_MM);
assert_is_close!(surf.radius, Length::new(0.38) * DOT_PER_MM);
Expand Down
6 changes: 6 additions & 0 deletions keyset-profile/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,10 @@ pub struct Profile {
impl Profile {
#[cfg(feature = "toml")]
#[inline]
#[deprecated(
since = "0.4.0",
note = "TOML profile files are deprecated, use JSON files instead"
)]
pub fn from_toml(s: &str) -> de::Result<Self> {
toml::from_str(s).map_err(de::Error::from)
}
Expand Down Expand Up @@ -633,6 +637,7 @@ mod tests {
fn test_profile_from_toml() {
use geom::DOT_PER_MM;

#[allow(deprecated)]
let profile = Profile::from_toml(PROFILE_TOML).unwrap();

assert!(
Expand Down Expand Up @@ -699,6 +704,7 @@ mod tests {
#[cfg(feature = "toml")]
#[test]
fn test_profile_from_invalid_toml() {
#[allow(deprecated)]
let result = Profile::from_toml("null");
assert!(result.is_err());
assert_eq!(
Expand Down
2 changes: 1 addition & 1 deletion keyset/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ color.workspace = true
drawing = { workspace = true, features = ["pdf", "png", "svg"] }
font.workspace = true
key = { workspace = true, features = ["kle"] }
profile = { workspace = true, features = ["toml"] }
profile = { workspace = true, features = ["json"] }

[dev-dependencies]
fontdb = "0.17"
88 changes: 51 additions & 37 deletions keyset/examples/basic-60.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,45 +15,59 @@ fn main() {
[{"w": 1.25, "f": 3, "a": 6}, "Shift", {"f": 4, "a": 4}, "|\n\\", {"f": 5}, "Z", "X", "C", "V", "B", "N", "M", {"f": 4}, "<\n,", ">\n.", "?\n/", {"w": 2.75, "f": 3, "a": 6}, "Shift"],
[{"w": 1.5}, "Ctrl", "Win", {"w": 1.5}, "Alt", {"p": "space", "w": 7}, "", {"p": "", "w": 1.5}, "AltGr", "Win", {"w": 1.5}, "Ctrl"]
]"#;
let profile = "
type = 'cylindrical'
depth = 0.5
let profile = r#"{
"type": "cylindrical",
"depth": 0.5,
[bottom]
width = 18.29
height = 18.29
radius = 0.38
"bottom": {
"width": 18.29,
"height": 18.29,
"radius": 0.38
},
"top": {
"width": 11.81,
"height": 13.91,
"radius": 1.52,
"y-offset": -1.62
},
[top]
width = 11.81
height = 13.91
radius = 1.52
y-offset = -1.62
"legend": {
"5": {
"size": 4.84,
"width": 9.45,
"height": 11.54,
"y-offset": 0
},
"4": {
"size": 3.18,
"width": 9.53,
"height": 9.56,
"y-offset": 0.40
},
"3": {
"size": 2.28,
"width": 9.45,
"height": 11.30,
"y-offset": -0.12
}
},
[legend.5]
size = 4.84
width = 9.45
height = 11.54
y-offset = 0
[legend.4]
size = 3.18
width = 9.53
height = 9.56
y-offset = 0.40
[legend.3]
size = 2.28
width = 9.45
height = 11.30
y-offset = -0.12
[homing]
default = 'scoop'
scoop = { depth = 1.5 }
bar = { width = 3.85, height = 0.4, y-offset = 5.05 }
bump = { diameter = 0.4, y-offset = -0.2 }
";
"homing": {
"default": "scoop",
"scoop": {
"depth": 1.5
},
"bar": {
"width": 3.85,
"height": 0.4,
"y-offset": 5.05
},
"bump": {
"diameter": 0.4,
"y-offset": -0.2
}
}
}"#;

let font = {
let mut fontdb = Database::new();
Expand All @@ -76,7 +90,7 @@ fn main() {
};

let keys = key::kle::from_json(kle).unwrap();
let profile = profile::Profile::from_toml(profile).unwrap();
let profile = profile::Profile::from_json(profile).unwrap();
let font = font::Font::from_ttf(font).unwrap();
let options = drawing::Options::new().profile(&profile).font(&font);
let drawing = options.draw(&keys);
Expand Down

0 comments on commit 41b5801

Please sign in to comment.