summaryrefslogtreecommitdiff
path: root/src/layout
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2020-02-04 21:36:29 +0100
committerLaurenz <laurmaedje@gmail.com>2020-02-04 21:36:29 +0100
commit751812f45141a7b2022d0dba138457f3c21950b0 (patch)
tree8f5125f5c475313c460f4a98ec174c11cb0e9c02 /src/layout
parente63ce52ae0d929506a1fa238477f039d14d53813 (diff)
Serialize layouts with serde 🔠
Diffstat (limited to 'src/layout')
-rw-r--r--src/layout/actions.rs37
-rw-r--r--src/layout/mod.rs33
2 files changed, 32 insertions, 38 deletions
diff --git a/src/layout/actions.rs b/src/layout/actions.rs
index 314084e5..0abef5f9 100644
--- a/src/layout/actions.rs
+++ b/src/layout/actions.rs
@@ -1,11 +1,11 @@
//! Drawing and configuration actions composing layouts.
-use std::io::{self, Write};
use std::fmt::{self, Debug, Formatter};
+use serde::ser::{Serialize, Serializer, SerializeTuple};
use toddle::query::FontIndex;
use crate::size::{Size, Size2D};
-use super::{Layout, Serialize};
+use super::Layout;
use self::LayoutAction::*;
@@ -24,12 +24,33 @@ pub enum LayoutAction {
}
impl Serialize for LayoutAction {
- fn serialize<W: Write>(&self, f: &mut W) -> io::Result<()> {
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer {
match self {
- MoveAbsolute(s) => write!(f, "m {:.4} {:.4}", s.x.to_pt(), s.y.to_pt()),
- SetFont(i, s) => write!(f, "f {} {} {}", i.id, i.variant, s.to_pt()),
- WriteText(s) => write!(f, "w {}", s),
- DebugBox(s) => write!(f, "b {} {}", s.x.to_pt(), s.y.to_pt()),
+ LayoutAction::MoveAbsolute(pos) => {
+ let mut tup = serializer.serialize_tuple(2)?;
+ tup.serialize_element(&0u8)?;
+ tup.serialize_element(&pos)?;
+ tup.end()
+ }
+ LayoutAction::SetFont(index, size) => {
+ let mut tup = serializer.serialize_tuple(4)?;
+ tup.serialize_element(&1u8)?;
+ tup.serialize_element(index)?;
+ tup.serialize_element(size)?;
+ tup.end()
+ }
+ LayoutAction::WriteText(text) => {
+ let mut tup = serializer.serialize_tuple(2)?;
+ tup.serialize_element(&2u8)?;
+ tup.serialize_element(text)?;
+ tup.end()
+ }
+ LayoutAction::DebugBox(size) => {
+ let mut tup = serializer.serialize_tuple(2)?;
+ tup.serialize_element(&3u8)?;
+ tup.serialize_element(&size)?;
+ tup.end()
+ }
}
}
}
@@ -40,7 +61,7 @@ impl Debug for LayoutAction {
match self {
MoveAbsolute(s) => write!(f, "move {} {}", s.x, s.y),
SetFont(i, s) => write!(f, "font {}_{} {}", i.id, i.variant, s),
- WriteText(s) => write!(f, "write \"{}\"", s),
+ WriteText(s) => write!(f, "write {:?}", s),
DebugBox(s) => write!(f, "box {} {}", s.x, s.y),
}
}
diff --git a/src/layout/mod.rs b/src/layout/mod.rs
index b29d87e3..01d402db 100644
--- a/src/layout/mod.rs
+++ b/src/layout/mod.rs
@@ -1,8 +1,8 @@
//! Layouting types and engines.
-use std::io::{self, Write};
use std::fmt::{self, Display, Formatter};
use smallvec::SmallVec;
+use serde::Serialize;
use toddle::query::FontIndex;
use crate::size::{Size, Size2D, SizeBox};
@@ -32,11 +32,12 @@ pub mod prelude {
pub type MultiLayout = Vec<Layout>;
/// A finished box with content at fixed positions.
-#[derive(Debug, Clone, PartialEq)]
+#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct Layout {
/// The size of the box.
pub dimensions: Size2D,
/// How to align this layout in a parent container.
+ #[serde(skip)]
pub alignment: LayoutAlignment,
/// The actions composing this layout.
pub actions: Vec<LayoutAction>,
@@ -57,34 +58,6 @@ impl Layout {
}
}
-/// Layout components that can be serialized.
-pub trait Serialize {
- /// Serialize the data structure into an output writable.
- fn serialize<W: Write>(&self, f: &mut W) -> io::Result<()>;
-}
-
-impl Serialize for Layout {
- fn serialize<W: Write>(&self, f: &mut W) -> io::Result<()> {
- writeln!(f, "{:.4} {:.4}", self.dimensions.x.to_pt(), self.dimensions.y.to_pt())?;
- writeln!(f, "{}", self.actions.len())?;
- for action in &self.actions {
- action.serialize(f)?;
- writeln!(f)?;
- }
- Ok(())
- }
-}
-
-impl Serialize for MultiLayout {
- fn serialize<W: Write>(&self, f: &mut W) -> io::Result<()> {
- writeln!(f, "{}", self.len())?;
- for layout in self {
- layout.serialize(f)?;
- }
- Ok(())
- }
-}
-
/// A vector of layout spaces, that is stack allocated as long as it only
/// contains at most 2 spaces.
pub type LayoutSpaces = SmallVec<[LayoutSpace; 2]>;