Skip to content

Commit

Permalink
Fix Tree deserialization using JSON (#85)
Browse files Browse the repository at this point in the history
* Closes #84
  • Loading branch information
hastri authored Sep 16, 2024
1 parent 07616f3 commit e52fe85
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,28 @@ pub struct Tree<Pane> {
pub tiles: Tiles<Pane>,

/// When finite, this values contains the exact height of this tree
#[cfg_attr(
feature = "serde",
serde(deserialize_with = "deserialize_f32_null_as_infinity")
)]
height: f32,

/// When finite, this values contains the exact width of this tree
#[cfg_attr(
feature = "serde",
serde(deserialize_with = "deserialize_f32_null_as_infinity")
)]
width: f32,
}

#[cfg(feature = "serde")]
fn deserialize_f32_null_as_infinity<'de, D: serde::Deserializer<'de>>(
des: D,
) -> Result<f32, D::Error> {
use serde::Deserialize;
Ok(Option::<f32>::deserialize(des)?.unwrap_or(f32::INFINITY))
}

impl<Pane: std::fmt::Debug> std::fmt::Debug for Tree<Pane> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
// Print a hierarchical view of the tree:
Expand Down

0 comments on commit e52fe85

Please sign in to comment.