#![deny(clippy::float_arithmetic)]
use f_office_units::Length;
use f_office_units::measure::universal_measure;
use f_office_units::scale::EMUS_PER_INCH;
use f_office_warnings::{Name, Warnings};
use f_office_xml::enumeration::{Drawn, Values};
use f_office_xml::{Document, NodeId};
pub mod color;
pub mod custom;
mod dash;
pub mod effect;
pub mod gradient;
mod guide;
mod line_end;
mod preset;
pub mod shape;
pub mod theme;
mod warning;
pub use warning::{Feature, Warning};
#[must_use]
pub fn boolean(value: &str) -> bool {
matches!(value, "1" | "true")
}
#[must_use]
pub fn emus(value: &str) -> Option<i64> {
value
.parse::<i64>()
.ok()
.or_else(|| universal_measure(value, EMUS_PER_INCH))
.filter(|emus| (-COORDINATE_LIMIT..=COORDINATE_LIMIT).contains(emus))
}
#[must_use]
pub fn emu_length(value: &str) -> Option<Length> {
emus(value).and_then(Length::from_emus)
}
pub const COORDINATE_LIMIT: i64 = 27_273_042_316_900;
#[must_use]
pub fn integer(value: &str) -> Option<i64> {
value
.parse()
.ok()
.filter(|number| (i64::from(i32::MIN)..=i64::from(u32::MAX)).contains(number))
}
#[must_use]
pub fn chart_percent(value: &str) -> Option<i64> {
integer(value.strip_suffix('%').unwrap_or(value))
}
fn attribute_of<T>(
doc: &Document,
node: NodeId,
name: &str,
parse: impl FnOnce(&str) -> Option<T>,
warnings: &mut Warnings<Warning>,
) -> Option<T> {
let value = parse(doc.attribute(node, None, name)?);
if value.is_none() {
warnings.push(Warning::InvalidValue {
element: element_name(doc, node),
attribute: Name::new(name),
});
}
value
}
fn emus_of(
doc: &Document,
node: NodeId,
name: &str,
warnings: &mut Warnings<Warning>,
) -> Option<Length> {
attribute_of(doc, node, name, emu_length, warnings)
}
fn integer_of(
doc: &Document,
node: NodeId,
name: &str,
warnings: &mut Warnings<Warning>,
) -> Option<i64> {
attribute_of(doc, node, name, integer, warnings)
}
fn enumeration<T: Clone>(
doc: &Document,
node: NodeId,
name: &str,
(table, warnings): (&'static Values<T>, &mut Warnings<Warning>),
) -> Option<T> {
let value = doc.attribute(node, None, name)?;
case_of(value, (&element_name(doc, node), name), (table, warnings))
}
fn case_of<T: Clone>(
value: &str,
(element, name): (&Name, &str),
(table, warnings): (&'static Values<T>, &mut Warnings<Warning>),
) -> Option<T> {
let Some((case, spelling, drawn)) = table.find(value) else {
warnings.push(Warning::InvalidValue {
element: element.clone(),
attribute: Name::new(name),
});
return None;
};
if drawn == Drawn::AsAnother {
warnings.push(Warning::ValueNotSupported {
element: element.clone(),
attribute: Name::new(name),
value: Name::new(spelling),
});
}
Some(case.clone())
}
fn element_name(doc: &Document, node: NodeId) -> Name {
doc.element(node)
.map_or_default(|element| Name::new(element.name.local))
}
const A: &str = "http://schemas.openxmlformats.org/drawingml/2006/main";
#[cfg(test)]
mod tests {
use super::{chart_percent, emus, integer};
#[test]
fn reads_a_whole_number_or_a_universal_measure_of_emu() {
let cases = [
("914400", Some(914_400)),
("-457200", Some(-457_200)),
("1in", Some(914_400)),
("3pt", Some(38_100)),
("914400.7", None),
("27273042316900", Some(27_273_042_316_900)),
("-27273042316900", Some(-27_273_042_316_900)),
("27273042316901", None),
("-27273042316901", None),
("29826161in", Some(27_273_041_618_400)),
("29826162in", None),
("wide", None),
("914400x", None),
(" 914400 ", None),
];
for (text, expected) in cases {
assert_eq!(emus(text), expected, "{text}");
}
}
#[test]
fn reads_a_whole_number_with_a_sign_only() {
let cases = [
("5400000", Some(5_400_000)),
("-5400000", Some(-5_400_000)),
("+1100", Some(1_100)),
("1100.5", None),
("4294967295", Some(4_294_967_295)),
("-2147483648", Some(-2_147_483_648)),
("4294967296", None),
("-2147483649", None),
(" 1100 ", None),
("11pt", None),
("wide", None),
];
for (text, expected) in cases {
assert_eq!(integer(text), expected, "{text}");
}
}
#[test]
fn reads_a_share_of_a_bar_chart_with_or_without_a_percent_sign() {
let cases = [
("150", Some(150)),
("150%", Some(150)),
("-27%", Some(-27)),
("150%%", None),
("150.5%", None),
("wide", None),
];
for (text, expected) in cases {
assert_eq!(chart_percent(text), expected, "{text}");
}
}
#[test]
fn rounds_a_universal_measure_at_half_an_emu_up() {
assert_eq!(emus("0.005pt"), Some(64));
assert_eq!(emus("-0.005pt"), Some(-63));
}
}