use serde::{Deserialize, Serialize};
use crate::source::{CollisionSource, ElementSource};
use crate::{Length, Orientation};
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct PageSource {
#[serde(default)]
pub preset: Option<String>,
#[serde(default)]
pub width: Option<Length>,
#[serde(default)]
pub height: Option<Length>,
#[serde(default)]
pub orientation: Orientation,
#[serde(default)]
pub margin: EdgeSource,
#[serde(default)]
pub bleed: EdgeSource,
#[serde(default)]
pub safe: EdgeSource,
#[serde(default)]
pub crop_marks: bool,
#[serde(default)]
pub collision: Option<CollisionSource>,
#[serde(default)]
pub master: PageLayerSource,
#[serde(default)]
pub first: PageLayerSource,
#[serde(default)]
pub continuation: PageLayerSource,
#[serde(default)]
pub last: PageLayerSource,
}
impl PageSource {
pub(crate) fn has_layer_elements(&self) -> bool {
self.element_lists()
.iter()
.any(|elements| !elements.is_empty())
}
pub(crate) fn element_lists(&self) -> [&[ElementSource]; 12] {
[
&self.master.background,
&self.master.header,
&self.master.footer,
&self.first.background,
&self.first.header,
&self.first.footer,
&self.continuation.background,
&self.continuation.header,
&self.continuation.footer,
&self.last.background,
&self.last.header,
&self.last.footer,
]
}
pub(crate) fn element_lists_mut(&mut self) -> [&mut Vec<ElementSource>; 12] {
[
&mut self.master.background,
&mut self.master.header,
&mut self.master.footer,
&mut self.first.background,
&mut self.first.header,
&mut self.first.footer,
&mut self.continuation.background,
&mut self.continuation.header,
&mut self.continuation.footer,
&mut self.last.background,
&mut self.last.header,
&mut self.last.footer,
]
}
pub(crate) fn placed_element_lists(&self) -> [(crate::PagePlacement, &[ElementSource]); 12] {
use crate::{PageBand::*, PageRole::*};
[
(placement(Master, Background), &self.master.background),
(placement(Master, Header), &self.master.header),
(placement(Master, Footer), &self.master.footer),
(placement(First, Background), &self.first.background),
(placement(First, Header), &self.first.header),
(placement(First, Footer), &self.first.footer),
(
placement(Continuation, Background),
&self.continuation.background,
),
(placement(Continuation, Header), &self.continuation.header),
(placement(Continuation, Footer), &self.continuation.footer),
(placement(Last, Background), &self.last.background),
(placement(Last, Header), &self.last.header),
(placement(Last, Footer), &self.last.footer),
]
}
}
const fn placement(role: crate::PageRole, band: crate::PageBand) -> crate::PagePlacement {
crate::PagePlacement { role, band }
}
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct PageLayerSource {
pub background: Vec<ElementSource>,
pub header: Vec<ElementSource>,
pub footer: Vec<ElementSource>,
}
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct EdgeSource {
pub top: Option<Length>,
pub right: Option<Length>,
pub bottom: Option<Length>,
pub left: Option<Length>,
}