use std::path::Path;
use std::process::Command;
use crate::inline::plain;
use crate::parse::ResumeDoc;
#[derive(Debug, Clone)]
pub struct Expected {
pub what: &'static str,
pub text: String,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Mode {
Raw,
Reading,
}
#[derive(Debug, Clone)]
pub struct OrderCheck {
pub mode: Mode,
pub ok: bool,
pub total: usize,
pub matched: usize,
pub first_miss: Option<Expected>,
}
pub fn expected_sequence(doc: &ResumeDoc, include_contact: bool) -> Vec<Expected> {
let mut v: Vec<Expected> = Vec::new();
let mut push = |what: &'static str, text: String| {
let t = norm(&text);
if !t.is_empty() {
v.push(Expected { what, text: t });
}
};
push("name", doc.name.clone());
if include_contact {
for c in &doc.contact {
push("contact", plain(c));
}
}
for sec in &doc.sections {
push("section label", sec.label.clone());
if sec.kind == "skills" {
for (label, value) in &sec.rows {
push("skills label", plain(label));
push("skills value", plain(value));
}
} else {
for e in &sec.entries {
push("role", e.role.clone());
push("aside", e.aside.clone());
push("company", e.company.clone());
push("extra", e.extra.clone());
push("meta", e.meta.clone());
for b in &e.bullets {
push("bullet", plain(b));
}
}
}
for n in &sec.notes {
push("note", plain(n));
}
}
v
}
pub fn pdftotext(path: &Path, mode: Mode) -> Option<String> {
let mut cmd = Command::new("pdftotext");
if mode == Mode::Raw {
cmd.arg("-raw");
}
cmd.arg("-q").arg(path).arg("-");
let out = cmd.output().ok()?;
if !out.status.success() {
return None;
}
Some(String::from_utf8_lossy(&out.stdout).into_owned())
}
pub fn check_order(expected: &[Expected], extracted: &str, mode: Mode) -> OrderCheck {
let haystack = norm(extracted);
let mut cursor = 0usize;
let mut matched = 0usize;
for exp in expected {
match haystack[cursor..].find(&exp.text) {
Some(pos) => {
cursor += pos + exp.text.len();
matched += 1;
}
None => {
return OrderCheck {
mode,
ok: false,
total: expected.len(),
matched,
first_miss: Some(exp.clone()),
};
}
}
}
OrderCheck {
mode,
ok: true,
total: expected.len(),
matched,
first_miss: None,
}
}
pub fn check_reading(doc: &ResumeDoc, extracted: &str) -> OrderCheck {
let hay = norm(extracted);
let total = expected_sequence(doc, false).len();
let mut matched = 0usize;
let fail = |matched: usize, what: &'static str, text: &str| OrderCheck {
mode: Mode::Reading,
ok: false,
total,
matched,
first_miss: Some(Expected {
what,
text: text.to_string(),
}),
};
let find = |from: usize, until: usize, needle: &str| -> Option<(usize, usize)> {
let until = until.min(hay.len());
if from >= until {
return None;
}
let pos = hay[from..until].find(needle)?;
if from + pos + needle.len() > until {
return None;
}
Some((from + pos, from + pos + needle.len()))
};
let mut cursor = 0usize;
let name = norm(&doc.name);
if !name.is_empty() {
match find(0, hay.len(), &name) {
Some((_, end)) => {
matched += 1;
cursor = end;
}
None => return fail(matched, "name", &name),
}
}
let mut window_starts: Vec<usize> = Vec::new();
let mut label_starts: Vec<usize> = Vec::new();
for sec in &doc.sections {
let label = norm(&sec.label);
if label.is_empty() {
window_starts.push(cursor);
label_starts.push(cursor);
continue;
}
match find(cursor, hay.len(), &label) {
Some((start, end)) => {
matched += 1;
window_starts.push(end);
label_starts.push(start);
cursor = end;
}
None => return fail(matched, "section label", &label),
}
}
for (i, sec) in doc.sections.iter().enumerate() {
let wstart = window_starts[i];
let wend = label_starts.get(i + 1).copied().unwrap_or(hay.len());
if sec.kind == "skills" {
let mut cur = wstart;
for (label, value) in &sec.rows {
for (what, text) in [
("skills label", norm(&plain(label))),
("skills value", norm(&plain(value))),
] {
if text.is_empty() {
continue;
}
match find(cur, wend, &text) {
Some((_, end)) => {
matched += 1;
cur = end;
}
None => return fail(matched, what, &text),
}
}
}
} else {
let mut role_cursor = wstart;
for e in &sec.entries {
let role = norm(&e.role);
let anchor = if role.is_empty() {
wstart
} else {
match find(role_cursor, wend, &role) {
Some((_, end)) => {
matched += 1;
role_cursor = end;
end
}
None => return fail(matched, "role", &role),
}
};
for (what, raw_text) in [
("aside", &e.aside),
("company", &e.company),
("extra", &e.extra),
("meta", &e.meta),
] {
let text = norm(raw_text);
if text.is_empty() {
continue;
}
if find(anchor, wend, &text).is_none() {
return fail(matched, what, &text);
}
matched += 1;
}
let mut bcur = anchor;
for b in &e.bullets {
let text = norm(&plain(b));
if text.is_empty() {
continue;
}
match find(bcur, wend, &text) {
Some((_, end)) => {
matched += 1;
bcur = end;
}
None => return fail(matched, "bullet", &text),
}
}
}
}
let mut ncur = wstart;
for n in &sec.notes {
let text = norm(&plain(n));
if text.is_empty() {
continue;
}
match find(ncur, wend, &text) {
Some((_, end)) => {
matched += 1;
ncur = end;
}
None => return fail(matched, "note", &text),
}
}
}
OrderCheck {
mode: Mode::Reading,
ok: true,
total,
matched,
first_miss: None,
}
}
pub fn verify(path: &Path, doc: &ResumeDoc) -> Option<(OrderCheck, OrderCheck)> {
let raw_text = pdftotext(path, Mode::Raw)?;
let reading_text = pdftotext(path, Mode::Reading)?;
let raw = check_order(&expected_sequence(doc, true), &raw_text, Mode::Raw);
let reading = check_reading(doc, &reading_text);
Some((raw, reading))
}
fn norm(s: &str) -> String {
s.split_whitespace().collect::<Vec<_>>().join(" ")
}