use crate::chart::Rgb;
pub(super) struct Fields<'a> {
pub(super) page: usize,
pub(super) pages: usize,
pub(super) sheet: &'a str,
pub(super) file: &'a str,
pub(super) date: &'a str,
pub(super) time: &'a str,
}
#[derive(Debug, Clone, Default, PartialEq)]
pub(super) struct PieceFmt {
pub(super) font: Option<String>,
pub(super) size_pt: Option<f32>,
pub(super) bold: bool,
pub(super) italic: bool,
pub(super) underline: bool,
pub(super) strike: bool,
pub(super) color: Option<Rgb>,
}
#[derive(Debug, Clone, PartialEq)]
pub(super) struct Piece {
pub(super) text: String,
pub(super) fmt: PieceFmt,
}
pub(super) type Line = Vec<Piece>;
const MAX_SIZE_PT: f32 = 409.0;
enum Step {
Text(String),
Section(usize),
Nothing,
}
pub(super) fn sections(code: &str, fields: &Fields<'_>) -> [Vec<Line>; 3] {
let mut out: [Vec<Line>; 3] = Default::default();
let mut section = 1;
let mut fmt = PieceFmt::default();
let mut chars = code.chars().peekable();
while let Some(ch) = chars.next() {
let lines = &mut out[section];
if lines.is_empty() {
lines.push(Vec::new());
}
let text = match ch {
'\r' | '\n' => {
if ch == '\r' && chars.peek() == Some(&'\n') {
chars.next();
}
lines.push(Vec::new());
continue;
}
'&' => match step(&mut chars, &mut fmt, fields) {
Step::Text(text) => text,
Step::Section(index) => {
section = index;
fmt = PieceFmt::default();
continue;
}
Step::Nothing => continue,
},
_ => ch.to_string(),
};
let line = lines.last_mut().expect("a line was pushed");
match line.last_mut() {
Some(piece) if piece.fmt == fmt => piece.text.push_str(&text),
_ => line.push(Piece { text, fmt: fmt.clone() }),
}
}
for lines in &mut out {
if lines
.iter()
.all(|line| line.iter().all(|piece| piece.text.trim().is_empty()))
{
lines.clear();
}
}
out
}
fn step(chars: &mut std::iter::Peekable<std::str::Chars<'_>>, fmt: &mut PieceFmt, fields: &Fields<'_>) -> Step {
let Some(code) = chars.next() else {
return Step::Nothing;
};
let text = match code {
'&' => "&".to_string(),
'L' => return Step::Section(0),
'C' => return Step::Section(1),
'R' => return Step::Section(2),
'P' => fields.page.to_string(),
'N' => fields.pages.to_string(),
'A' => fields.sheet.to_string(),
'F' => fields.file.to_string(),
'D' => fields.date.to_string(),
'T' => fields.time.to_string(),
'B' => {
fmt.bold = !fmt.bold;
return Step::Nothing;
}
'I' => {
fmt.italic = !fmt.italic;
return Step::Nothing;
}
'U' | 'E' => {
fmt.underline = !fmt.underline;
return Step::Nothing;
}
'S' => {
fmt.strike = !fmt.strike;
return Step::Nothing;
}
'"' => {
let name: String = chars.by_ref().take_while(|&ch| ch != '"').collect();
font(fmt, &name);
return Step::Nothing;
}
'K' => {
let value: String = chars.by_ref().take(6).collect();
fmt.color = rgb(&value);
return Step::Nothing;
}
digit if digit.is_ascii_digit() => {
let mut size = String::from(digit);
while let Some(&next) = chars.peek().filter(|next| next.is_ascii_digit()) {
size.push(next);
chars.next();
}
fmt.size_pt = size.parse::<f32>().ok().map(|size| size.clamp(1.0, MAX_SIZE_PT));
return Step::Nothing;
}
_ => return Step::Nothing,
};
Step::Text(text)
}
fn font(fmt: &mut PieceFmt, code: &str) {
let (name, style) = code.split_once(',').unwrap_or((code, ""));
let name = name.trim();
if !name.is_empty() && name != "-" {
fmt.font = Some(name.to_string());
}
let style = style.to_lowercase();
if !style.is_empty() {
fmt.bold = style.contains("bold") || style.contains("太字");
fmt.italic = style.contains("italic") || style.contains("oblique") || style.contains("斜体");
}
}
fn rgb(value: &str) -> Option<Rgb> {
if value.len() != 6 || !value.chars().all(|ch| ch.is_ascii_hexdigit()) {
return None;
}
let part = |at: usize| u8::from_str_radix(&value[at..at + 2], 16).ok();
Some([part(0)?, part(2)?, part(4)?])
}
pub(super) fn now() -> (String, String) {
let seconds = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map_or(0, |since| since.as_secs());
let days = i64::try_from(seconds / 86_400).unwrap_or(0);
let of_day = seconds % 86_400;
let shifted = days + 719_468;
let era = shifted.div_euclid(146_097);
let day_of_era = shifted.rem_euclid(146_097);
let year_of_era = (day_of_era - day_of_era / 1460 + day_of_era / 36_524 - day_of_era / 146_096) / 365;
let day_of_year = day_of_era - (365 * year_of_era + year_of_era / 4 - year_of_era / 100);
let month_index = (5 * day_of_year + 2) / 153;
let day = day_of_year - (153 * month_index + 2) / 5 + 1;
let month = if month_index < 10 {
month_index + 3
} else {
month_index - 9
};
let year = year_of_era + era * 400 + i64::from(month <= 2);
(
format!("{year:04}-{month:02}-{day:02}"),
format!("{:02}:{:02}", of_day / 3600, of_day % 3600 / 60),
)
}
#[cfg(test)]
mod tests {
use super::*;
fn fields() -> Fields<'static> {
Fields {
page: 2,
pages: 5,
sheet: "Data",
file: "book.xlsx",
date: "2026-01-02",
time: "09:30",
}
}
fn texts(code: &str) -> Vec<Vec<String>> {
sections(code, &fields())
.iter()
.map(|lines| {
lines
.iter()
.map(|line| line.iter().map(|piece| piece.text.as_str()).collect())
.collect()
})
.collect()
}
#[test]
fn sections_fill_in_their_fields() {
assert_eq!(
texts("&L様式第1-9号&C&P / &N ページ&R&A, &F &D &T"),
[
vec!["様式第1-9号".to_string()],
vec!["2 / 5 ページ".to_string()],
vec!["Data, book.xlsx 2026-01-02 09:30".to_string()],
]
);
assert_eq!(texts("Q&&A")[1], ["Q&A"]);
}
#[test]
fn codes_set_the_font_and_lines_break() {
let [_, centre, _] = sections("&C&\"Aptos,Bold\"&14&KFF0000 Label\r\nnext", &fields());
assert_eq!(centre.len(), 2);
let piece = ¢re[0][0];
assert_eq!(piece.text, " Label");
assert_eq!(piece.fmt.font.as_deref(), Some("Aptos"));
assert_eq!(piece.fmt.size_pt, Some(14.0));
assert!(piece.fmt.bold);
assert_eq!(piece.fmt.color, Some([255, 0, 0]));
assert_eq!(centre[1][0].text, "next");
}
#[test]
fn the_date_is_iso() {
let (date, time) = now();
assert_eq!(date.len(), 10);
assert_eq!(&date[4..5], "-");
assert_eq!(time.len(), 5);
}
}