use indicatrix_cut_core::{Design, native::DesignMetadata};
#[derive(Debug, Clone, Default, PartialEq)]
pub struct SheetDetails {
pub title: String,
pub subtitle: String,
pub author: String,
pub date_text: String,
pub shape: String,
pub comments: Vec<String>,
pub ri_range: Option<(f64, f64)>,
pub size_range_mm: Option<(f64, f64)>,
}
fn author_of(line: &str) -> Option<&str> {
let line = line.trim();
let head = line.get(..3)?;
if !head.eq_ignore_ascii_case("by ") {
return None;
}
let name = line[3..].trim();
(!name.is_empty()).then_some(name)
}
impl SheetDetails {
#[must_use]
pub fn from_design(design: &Design) -> Self {
let headers = &design.meta.headers;
let title = headers
.iter()
.map(|line| line.trim())
.find(|line| !line.is_empty() && author_of(line).is_none())
.unwrap_or_default()
.to_string();
let author = headers
.iter()
.find_map(|line| author_of(line))
.unwrap_or_default()
.to_string();
let comments = design
.meta
.footnotes
.iter()
.map(|line| line.trim().to_string())
.filter(|line| !line.is_empty())
.collect();
Self {
title,
author,
comments,
..Self::default()
}
}
#[must_use]
pub fn from_metadata(design: &Design, metadata: &DesignMetadata, date_text: &str) -> Self {
let mut details = Self::from_design(design);
let title = metadata.title.trim();
if !title.is_empty() {
details.title = title.to_string();
}
let designer = metadata.designer.trim();
if !designer.is_empty() {
details.author = designer.to_string();
}
let shape = metadata.shape.trim();
if !shape.is_empty() {
details.shape = shape.to_string();
}
details.comments.extend(
metadata
.notes
.lines()
.map(str::trim)
.filter(|line| !line.is_empty())
.map(str::to_string),
);
details.date_text = date_text.trim().to_string();
details
}
}
const fn month_name(month: u32) -> Option<&'static str> {
Some(match month {
1 => "January",
2 => "February",
3 => "March",
4 => "April",
5 => "May",
6 => "June",
7 => "July",
8 => "August",
9 => "September",
10 => "October",
11 => "November",
12 => "December",
_ => return None,
})
}
#[must_use]
pub fn month_year_text(year: i64, month: u32) -> String {
month_name(month).map_or_else(String::new, |name| format!("{name} {year}"))
}