use crate::model::Item;
#[derive(Debug, Clone, PartialEq)]
pub struct FieldConflict {
pub field: String,
pub values: [String; 2],
}
#[derive(Debug, Clone)]
pub struct Merged {
pub item: Item,
pub fields: Vec<FieldConflict>,
pub body: Option<[String; 2]>,
}
enum Pick<T> {
Clean(T),
Conflict(T, T),
}
fn pick<T: PartialEq + Clone>(base: Option<&T>, ours: &T, theirs: &T) -> Pick<T> {
if ours == theirs || base == Some(theirs) {
Pick::Clean(ours.clone())
} else if base == Some(ours) {
Pick::Clean(theirs.clone())
} else {
Pick::Conflict(ours.clone(), theirs.clone())
}
}
fn merge_set(base: Option<&Vec<String>>, ours: &[String], theirs: &[String]) -> Vec<String> {
let empty = vec![];
let base = base.unwrap_or(&empty);
let removed = |x: &String| base.contains(x) && (!ours.contains(x) || !theirs.contains(x));
let mut out: Vec<String> = vec![];
for x in ours.iter().chain(theirs) {
if !removed(x) && !out.contains(x) {
out.push(x.clone());
}
}
out
}
pub fn merge_item(
base: Option<&Item>,
ours: &Item,
theirs: &Item,
merge_text: &dyn Fn(&str, &str, &str) -> Option<String>,
) -> Merged {
let mut item = ours.clone();
let mut fields = vec![];
let mut conflict = |name: &str, o: String, t: String| {
fields.push(FieldConflict {
field: name.into(),
values: [o, t],
});
};
match pick(base.map(|b| &b.title), &ours.title, &theirs.title) {
Pick::Clean(v) => item.title = v,
Pick::Conflict(o, t) => conflict("title", o, t),
}
match pick(base.map(|b| &b.kind), &ours.kind, &theirs.kind) {
Pick::Clean(v) => item.kind = v,
Pick::Conflict(o, t) => conflict("kind", o, t),
}
match pick(base.map(|b| &b.source), &ours.source, &theirs.source) {
Pick::Clean(v) => item.source = v,
Pick::Conflict(o, t) => conflict("source", o, t),
}
match pick(base.map(|b| &b.status), &ours.status, &theirs.status) {
Pick::Clean(v) => {
let theirs_won = v == theirs.status && v != ours.status;
item.finished = if theirs_won {
theirs.finished.clone()
} else {
ours.finished.clone()
};
item.status = v;
}
Pick::Conflict(o, t) => conflict("status", o, t),
}
let scores: [(&str, fn(&Item) -> u8); 4] = [
("impact", |i| i.impact),
("urgency", |i| i.urgency),
("risk", |i| i.risk),
("effort", |i| i.effort),
];
for (name, get) in scores {
match pick(base.map(get).as_ref(), &get(ours), &get(theirs)) {
Pick::Clean(v) => match name {
"impact" => item.impact = v,
"urgency" => item.urgency = v,
"risk" => item.risk = v,
_ => item.effort = v,
},
Pick::Conflict(o, t) => conflict(name, o.to_string(), t.to_string()),
}
}
item.needs = merge_set(base.map(|b| &b.needs), &ours.needs, &theirs.needs);
item.docs = merge_set(base.map(|b| &b.docs), &ours.docs, &theirs.docs);
let mut body = None;
match pick(base.map(|b| &b.body), &ours.body, &theirs.body) {
Pick::Clean(v) => item.body = v,
Pick::Conflict(o, t) => {
let b = base.map(|b| b.body.as_str()).unwrap_or("");
match merge_text(b, &o, &t) {
Some(merged) => item.body = merged,
None => body = Some([o, t]),
}
}
}
Merged { item, fields, body }
}