| 1 |
use crate::errors::{ErrorKind, Result}; |
| 2 |
use crate::one::property::PropertyType; |
| 3 |
use crate::onestore::object::Object; |
| 4 |
|
| 5 |
/// A layout alignment specification. |
| 6 |
/// |
| 7 |
/// See [\[MS-ONE\] 2.3.2] |
| 8 |
/// |
| 9 |
/// [\[MS-ONE\] 2.3.2]: https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-one/bd99face-5839-4276-863d-a4f2cbb81246 |
| 10 |
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)] |
| 11 |
pub struct LayoutAlignment { |
| 12 |
alignment_horizontal: HorizontalAlignment, |
| 13 |
alignment_margin_horizontal: HorizontalAlignmentMargin, |
| 14 |
alignment_vertical: VerticalAlignment, |
| 15 |
alignment_margin_vertical: VerticalAlignmentMargin, |
| 16 |
} |
| 17 |
|
| 18 |
impl LayoutAlignment { |
| 19 |
pub fn alignment_horizontal(&self) -> HorizontalAlignment { |
| 20 |
self.alignment_horizontal |
| 21 |
} |
| 22 |
|
| 23 |
pub fn alignment_margin_horizontal(&self) -> HorizontalAlignmentMargin { |
| 24 |
self.alignment_margin_horizontal |
| 25 |
} |
| 26 |
|
| 27 |
pub fn alignment_vertical(&self) -> VerticalAlignment { |
| 28 |
self.alignment_vertical |
| 29 |
} |
| 30 |
|
| 31 |
pub fn alignment_margin_vertical(&self) -> VerticalAlignmentMargin { |
| 32 |
self.alignment_margin_vertical |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
impl LayoutAlignment { |
| 37 |
pub(crate) fn parse( |
| 38 |
prop_type: PropertyType, |
| 39 |
object: &Object, |
| 40 |
) -> Result<Option<LayoutAlignment>> { |
| 41 |
let value = match object.props().get(prop_type) { |
| 42 |
Some(value) => value.to_u32().ok_or_else(|| { |
| 43 |
ErrorKind::MalformedOneNoteFileData("layout alignment is not a u32".into()) |
| 44 |
})?, |
| 45 |
None => return Ok(None), |
| 46 |
}; |
| 47 |
|
| 48 |
if (value >> 31) & 0x1 != 0 { |
| 49 |
return Ok(None); |
| 50 |
} |
| 51 |
|
| 52 |
let alignment_horizontal = HorizontalAlignment::parse(value & 0x7)?; |
| 53 |
let alignment_margin_horizontal = HorizontalAlignmentMargin::parse((value >> 3) & 0x1)?; |
| 54 |
let alignment_vertical = VerticalAlignment::parse((value >> 16) & 0x1)?; |
| 55 |
let alignment_margin_vertical = VerticalAlignmentMargin::parse((value >> 19) & 0x1)?; |
| 56 |
|
| 57 |
Ok(Some(LayoutAlignment { |
| 58 |
alignment_horizontal, |
| 59 |
alignment_margin_horizontal, |
| 60 |
alignment_vertical, |
| 61 |
alignment_margin_vertical, |
| 62 |
})) |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)] |
| 67 |
pub enum HorizontalAlignment { |
| 68 |
Unknown, |
| 69 |
Left, |
| 70 |
Center, |
| 71 |
Right, |
| 72 |
BiDiNormal, |
| 73 |
BiDiReverse, |
| 74 |
} |
| 75 |
|
| 76 |
impl HorizontalAlignment { |
| 77 |
pub(crate) fn parse(value: u32) -> Result<HorizontalAlignment> { |
| 78 |
match value { |
| 79 |
0 => Ok(HorizontalAlignment::Unknown), |
| 80 |
1 => Ok(HorizontalAlignment::Left), |
| 81 |
2 => Ok(HorizontalAlignment::Center), |
| 82 |
3 => Ok(HorizontalAlignment::Right), |
| 83 |
4 => Ok(HorizontalAlignment::BiDiNormal), |
| 84 |
5 => Ok(HorizontalAlignment::BiDiReverse), |
| 85 |
_ => Err(ErrorKind::MalformedOneNoteFileData( |
| 86 |
format!("invalid horizontal alignment: {}", value).into(), |
| 87 |
) |
| 88 |
.into()), |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)] |
| 94 |
pub enum HorizontalAlignmentMargin { |
| 95 |
Right, |
| 96 |
Left, |
| 97 |
} |
| 98 |
|
| 99 |
impl HorizontalAlignmentMargin { |
| 100 |
pub(crate) fn parse(value: u32) -> Result<HorizontalAlignmentMargin> { |
| 101 |
match value { |
| 102 |
0 => Ok(HorizontalAlignmentMargin::Right), |
| 103 |
1 => Ok(HorizontalAlignmentMargin::Left), |
| 104 |
_ => Err(ErrorKind::MalformedOneNoteFileData( |
| 105 |
format!("invalid horizontal alignment margin: {}", value).into(), |
| 106 |
) |
| 107 |
.into()), |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)] |
| 113 |
pub enum VerticalAlignment { |
| 114 |
Bottom, |
| 115 |
Top, |
| 116 |
} |
| 117 |
|
| 118 |
impl VerticalAlignment { |
| 119 |
pub(crate) fn parse(value: u32) -> Result<VerticalAlignment> { |
| 120 |
match value { |
| 121 |
0 => Ok(VerticalAlignment::Bottom), |
| 122 |
1 => Ok(VerticalAlignment::Top), |
| 123 |
_ => Err(ErrorKind::MalformedOneNoteFileData( |
| 124 |
format!("invalid vertical alignment: {}", value).into(), |
| 125 |
) |
| 126 |
.into()), |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)] |
| 132 |
pub enum VerticalAlignmentMargin { |
| 133 |
Bottom, |
| 134 |
Top, |
| 135 |
} |
| 136 |
|
| 137 |
impl VerticalAlignmentMargin { |
| 138 |
pub(crate) fn parse(value: u32) -> Result<VerticalAlignmentMargin> { |
| 139 |
match value { |
| 140 |
0 => Ok(VerticalAlignmentMargin::Bottom), |
| 141 |
1 => Ok(VerticalAlignmentMargin::Top), |
| 142 |
_ => Err(ErrorKind::MalformedOneNoteFileData( |
| 143 |
format!("invalid vertical alignment margin: {}", value).into(), |
| 144 |
) |
| 145 |
.into()), |
| 146 |
} |
| 147 |
} |
| 148 |
} |