Make duration splitting independent of handling policy

This commit is contained in:
Dandaedre 2026-11-04 16:34:29 +01:00
parent 8d2b2eefd0
commit 993db72668
9 changed files with 168 additions and 40 deletions

View File

@ -1,6 +1,6 @@
use crate::{
model::*,
time::{clock, clock_range, duration, normalize, select_durations},
time::{clock, clock_range, duration, normalize, select_duration_parts},
};
fn role(header: &str) -> Option<&'static str> {
@ -547,7 +547,7 @@ fn infer_inner(
"Use strict, minimum, maximum, sum, margin or split",
));
}
if duration_mode == "split"
if (profile.duration_split || duration_mode == "split")
&& profile
.duration_separator
.as_deref()
@ -667,10 +667,11 @@ fn infer_inner(
"start" => clock_range(&cell).is_some(),
"start_end" => clock_range(&cell).is_some_and(|(_, end)| end.is_some()),
"end" => clock(&cell).is_some(),
_ => select_durations(
_ => select_duration_parts(
&cell,
unit.as_deref(),
profile.duration_mode.as_deref(),
profile.duration_split,
profile.duration_separator.as_deref(),
)
.is_some(),
@ -696,6 +697,7 @@ fn infer_inner(
duration_unit: unit,
duration_mode: profile.duration_mode.clone(),
duration_separator: profile.duration_separator.clone(),
duration_split: profile.duration_split,
evidence: evidence.clone(),
column_usage,
column_labels,

View File

@ -69,12 +69,15 @@ fn duration_mode_label(mode: &str) -> &'static str {
"maximum" => "Maximum",
"sum" => "Sum",
"margin" => "Add margin",
"split" => "Split into entries",
_ => "Use the whole cell",
}
}
fn duration_handling_controls(ui: &mut egui::Ui, profile: &mut Profile, id: egui::Id) {
if profile.duration_mode.as_deref() == Some("split") {
profile.duration_mode = Some("sum".into());
profile.duration_split = true;
}
ui.indent(id.with("indent"), |ui| {
ui.horizontal_wrapped(|ui| {
ui.weak("Duration handling");
@ -91,7 +94,6 @@ fn duration_handling_controls(ui: &mut egui::Ui, profile: &mut Profile, id: egui
("maximum", "Maximum"),
("sum", "Sum"),
("margin", "Add margin"),
("split", "Split into entries"),
] {
ui.selectable_value(&mut mode, value.into(), label);
}
@ -99,7 +101,8 @@ fn duration_handling_controls(ui: &mut egui::Ui, profile: &mut Profile, id: egui
.response
.on_hover_text("Choose how duration values are extracted when one cell contains several values. Add margin uses the largest value for the event and keeps the remainder as a gap.");
profile.duration_mode = (mode != "strict").then_some(mode.clone());
if mode == "split" {
ui.checkbox(&mut profile.duration_split, "Split into multiple entries");
if profile.duration_split {
let separator = profile
.duration_separator
.get_or_insert_with(|| "\\n".into());
@ -567,7 +570,7 @@ impl App {
self.decisions_changed();
}
}
ui.label("Optional JSON profile: columns (start/start_end/end/duration/title), column_usage (notes/metadata/ignore), duration_unit, duration_mode, duration_separator, row_kinds, titles, starts, ends, durations, untimed_as_notes, ignore_warnings. Column letters and source rows are shown in diagnostics.");
ui.label("Optional JSON profile: columns (start/start_end/end/duration/title), column_usage (notes/metadata/ignore), duration_unit, duration_mode, duration_split, duration_separator, row_kinds, titles, starts, ends, durations, untimed_as_notes, ignore_warnings. Column letters and source rows are shown in diagnostics.");
if ui
.add(
egui::TextEdit::multiline(&mut self.profile)

View File

@ -593,6 +593,7 @@ fn column_settings_changed(current: &Profile, analyzed: &Profile) -> bool {
|| current.duration_unit != analyzed.duration_unit
|| current.duration_mode != analyzed.duration_mode
|| current.duration_separator != analyzed.duration_separator
|| current.duration_split != analyzed.duration_split
}
fn schedule_settings_changed(current: &Profile, analyzed: &Profile) -> bool {
@ -3430,6 +3431,19 @@ mod tests {
let profile: Profile = serde_json::from_str(&app.profile).unwrap();
assert_eq!(profile.duration_mode.as_deref(), Some("maximum"));
assert!(app.dirty);
let labels = render(&mut app, &ctx, size, vec![]);
let toggle = labels
.iter()
.find(|(text, _)| text == "Split into multiple entries")
.unwrap()
.1
.center();
click_size(&mut app, &ctx, toggle, size);
let profile: Profile = serde_json::from_str(&app.profile).unwrap();
assert!(profile.duration_split);
assert_eq!(profile.duration_mode.as_deref(), Some("maximum"));
let labels = render(&mut app, &ctx, size, vec![]);
assert!(labels.iter().any(|(text, _)| text == "Separator"));
}
#[test]

View File

@ -69,10 +69,12 @@ pub struct Profile {
pub expected_headers: BTreeMap<String, String>,
/// minutes, seconds, hh:mm, mm:ss, or excel.
pub duration_unit: Option<String>,
/// strict, minimum, maximum, sum, margin, or split.
/// strict, minimum, maximum, sum, or margin (legacy split is accepted).
pub duration_mode: Option<String>,
/// Separator used by split mode. Escaped forms such as \\n and \\t are accepted.
pub duration_separator: Option<String>,
#[serde(default)]
pub duration_split: bool,
/// Explicitly retain rows without times as notes on the preceding event.
pub untimed_as_notes: bool,
/// Try excluding rows with unresolved-content warnings, then validate again.
@ -113,6 +115,8 @@ pub struct Mapping {
pub duration_mode: Option<String>,
#[serde(default)]
pub duration_separator: Option<String>,
#[serde(default)]
pub duration_split: bool,
pub evidence: Vec<String>,
#[serde(default)]
pub column_usage: BTreeMap<String, String>,

View File

@ -1,7 +1,7 @@
use crate::{
detect,
model::*,
time::{SelectedDurations, clock, clock_range, duration, normalize, select_durations},
time::{SelectedDurations, clock, clock_range, duration, normalize, select_duration_parts},
};
use std::sync::atomic::{AtomicBool, Ordering};
use uuid::Uuid;
@ -53,13 +53,15 @@ fn duration_plan(
.map(|value| SelectedDurations {
events: vec![value],
margin: 0,
margins: vec![],
})
} else {
map.duration.and_then(|c| {
select_durations(
select_duration_parts(
&table.cell(row, c),
map.duration_unit.as_deref(),
profile.duration_mode.as_deref(),
profile.duration_split,
profile.duration_separator.as_deref(),
)
})
@ -443,13 +445,13 @@ pub fn construct(
3,
"SPLIT_MERGED_TIMING",
at(table, r),
"A split duration cannot be combined with a merged timing block. Unmerge it or use Minimum, Maximum or Sum.",
"A split duration cannot be combined with a merged timing block. Unmerge it or disable Split into multiple entries.",
));
continue;
}
let plan = duration_plan.as_ref().expect("checked above");
if let Some(end) = given_end {
let selected: i64 = plan.events.iter().sum();
let selected: i64 = plan.events.iter().sum::<i64>() + plan.margin;
if end - start != selected {
errors.push(Diagnostic::new(
4,
@ -488,7 +490,19 @@ pub fn construct(
.push("Start derived from the preceding virtual entry".into());
}
result.entries.push(e);
part_start = part_end;
let margin = plan.margins.get(part).copied().unwrap_or(0);
if margin > 0 {
gaps.push(Gap {
after_row: Some(r + 1),
before_row: None,
start_ms: Some(part_end),
end_ms: Some(part_end + margin),
title: "Duration margin".into(),
source_rows: vec![source_row(table, r)],
display_timer: false,
});
}
part_start = part_end + margin;
}
cursor = Some(part_start);
continue;

View File

@ -83,6 +83,8 @@ pub struct SelectedDurations {
pub events: Vec<i64>,
/// Untimed interval inserted after the event or split entries.
pub margin: i64,
/// Margin after each virtual entry when splitting.
pub margins: Vec<i64>,
}
fn separator(value: Option<&str>) -> &str {
@ -183,29 +185,11 @@ pub fn select_durations(
return duration(cell, unit).map(|value| SelectedDurations {
events: vec![value],
margin: 0,
margins: vec![],
});
}
if mode == "split" {
let events: Vec<i64> = cell
.text
.split(separator(split_separator))
.filter_map(|part| {
let lines: Vec<Vec<i64>> = part
.lines()
.map(|line| explicit_duration_values(line, unit))
.filter(|values| !values.is_empty())
.collect();
let largest = lines.iter().map(Vec::len).max()?;
Some(
lines
.iter()
.filter(|values| values.len() == largest)
.flatten()
.sum(),
)
})
.collect();
return (!events.is_empty()).then_some(SelectedDurations { events, margin: 0 });
return select_duration_parts(cell, unit, Some("sum"), true, split_separator);
}
let groups: Vec<Vec<i64>> = cell
.text
@ -226,14 +210,17 @@ pub fn select_durations(
"minimum" => SelectedDurations {
events: vec![*values.iter().min()?],
margin: 0,
margins: vec![],
},
"maximum" => SelectedDurations {
events: vec![*values.iter().max()?],
margin: 0,
margins: vec![],
},
"sum" => SelectedDurations {
events: vec![values.iter().sum()],
margin: 0,
margins: vec![],
},
"margin" => {
let mut event = 0;
@ -246,12 +233,57 @@ pub fn select_durations(
SelectedDurations {
events: vec![event],
margin,
margins: vec![],
}
}
_ => return None,
};
(!selected.events.is_empty()).then_some(selected)
}
/// Apply the selected handling independently to each separated entry.
pub fn select_duration_parts(
cell: &Cell,
unit: Option<&str>,
mode: Option<&str>,
split: bool,
split_separator: Option<&str>,
) -> Option<SelectedDurations> {
let legacy = mode == Some("split");
let mode = if legacy { Some("sum") } else { mode };
if !split && !legacy {
return select_durations(cell, unit, mode, None);
}
let separator = separator(split_separator);
if separator.is_empty() {
return None;
}
let mut result = SelectedDurations {
events: vec![],
margin: 0,
margins: vec![],
};
for part in cell.text.split(separator) {
if part.trim().is_empty()
|| clock_range(&Cell {
text: part.trim().into(),
..Cell::default()
})
.is_some_and(|(_, end)| end.is_some())
{
continue;
}
let part = Cell {
text: part.into(),
..cell.clone()
};
let selected = select_durations(&part, unit, mode, None)?;
result.events.extend(selected.events);
result.margins.push(selected.margin);
result.margin += selected.margin;
}
(!result.events.is_empty()).then_some(result)
}
fn unsigned_duration(cell: &Cell, unit: Option<&str>) -> Option<i64> {
let mut s = normalize(cell.text.trim()).replace(',', ".");
if cell.numeric && cell.time_style {

View File

@ -1343,3 +1343,58 @@ fn warning_policy_excludes_rows_rechecks_and_can_be_disabled() {
assert!(!a.diagnostics.is_empty());
assert!(a.automatically_ignored_rows.is_empty());
}
#[test]
fn split_combines_with_each_duration_policy_and_keeps_each_margin() {
let t = table(&[
&["Start", "Duration", "Title"],
&["08:00", "20 min + 5 min\n10 min + 2 min", "Talks"],
]);
for (mode, durations, margins) in [
("minimum", [5, 2], [0, 0]),
("maximum", [20, 10], [0, 0]),
("sum", [25, 12], [0, 0]),
("margin", [20, 10], [5, 2]),
] {
let profile = Profile {
duration_mode: Some(mode.into()),
duration_split: true,
..Profile::default()
};
let a = analyze(&t, &profile);
assert!(a.diagnostics.is_empty(), "{mode}: {:?}", a.diagnostics);
let r = a.rundown.unwrap();
assert_eq!(r.entries.len(), 2);
assert_eq!(r.entries[0].duration_ms, durations[0] * 60_000);
assert_eq!(r.entries[1].duration_ms, durations[1] * 60_000);
assert_eq!(
r.entries[1].start_ms,
r.entries[0].end_ms + margins[0] * 60_000
);
if mode == "margin" {
assert_eq!(r.gaps.len(), 2);
for (gap, minutes) in r.gaps.iter().zip(margins) {
assert_eq!(
gap.end_ms.unwrap() - gap.start_ms.unwrap(),
minutes * 60_000
);
}
}
assert!(export::ontime(&r).is_ok());
}
let mut invalid = t;
invalid.rows[1][1].text = "20 min\nunreadable duration".into();
let a = analyze(
&invalid,
&Profile {
duration_mode: Some("maximum".into()),
duration_split: true,
..Profile::default()
},
);
assert!(
a.diagnostics
.iter()
.any(|d| d.code == "INCONSISTENT_COLUMN_MEANING")
);
}

View File

@ -297,7 +297,7 @@ errors must remain visible with direct corrective actions.
errors. Test ambiguous cells clarified by other rows, incompatible meanings in
one column, structural rows and misleading headers/units.
- [x] Keep one Duration column role while offering nested whole-cell, minimum,
maximum, sum, margin and split extraction policies. Let a Start mapping select
maximum, sum and margin extraction policies, plus an independent split toggle. Let a Start mapping select
only the first clock in a mixed range; retain margin gaps and split entries in
the IR.
- [x] Stage 2: identify untimed gaps and populate draft IR constraints. Test named

View File

@ -122,8 +122,9 @@ visually distinct from labels and ordinary actions; the active tab uses the stro
bounds. **Start** selects only the first clock from a cell, so a mixed
`08h45 - 09h10` value can provide the 08:45 anchor while the selected duration
column determines the timer. A duration column has a nested **Duration handling**
choice instead of extra column roles: whole cell, minimum, maximum, sum, add
margin or split into entries. Selecting one duration column explicitly resolves
choice instead of extra column roles: whole cell, minimum, maximum, sum or add
margin. A separate **Split into multiple entries** toggle applies that choice
to each separated group; Add margin then creates a gap after each entry. Selecting one duration column explicitly resolves
duplicate TIMER headers.
**Notes** appends a column's values to Ontime event notes. **Metadata**
creates an Ontime custom field named from the source header. **Ignore** excludes
@ -264,7 +265,8 @@ Pass `--profile mapping.json`, or load/edit the same JSON in the desktop app:
{
"header_row": 1,
"columns": {"start": "A", "duration": "B", "title": "G"},
"duration_mode": "split",
"duration_mode": "maximum",
"duration_split": true,
"duration_separator": "\\n\\n",
"column_usage": {"C": "notes", "D": "metadata", "F": "ignore"},
"expected_headers": {"A": "Horaires", "B": "Durée"},
@ -294,12 +296,14 @@ explicit mappings still undergo it.
`duration_mode` is optional and remains attached to the single `duration` role.
`minimum`, `maximum` and `sum` select from explicit duration values in mixed text.
`margin` uses the largest value in each duration group for the event and preserves
the remainder as an untimed gap. `split` creates sequential virtual events from
groups separated by `duration_separator`; `\\n`, `\\n\\n` and `\\t` mean newline,
the remainder as an untimed gap. `duration_split: true` creates sequential virtual
events and applies the selected handling to each group separated by
`duration_separator`; `\\n`, `\\n\\n` and `\\t` mean newline,
blank line and tab. The default `strict` behavior requires the complete cell to be
one duration. For non-split policies, a one-value summary line followed by a more
detailed multi-value line is treated as an alternative expression, avoiding double
counting.
counting. Legacy profiles using `duration_mode: "split"` are read as Sum with
splitting enabled.
`column_usage` maps column letters to `notes`, `metadata` or `ignore`. Every choice
prevents that column from being inferred as a start, start–end, end, duration or