use crate::inline::plain;
#[derive(Debug, Default, Clone)]
pub struct ResumeDoc {
pub name: String,
pub contact: Vec<String>,
pub sections: Vec<Section>,
}
#[derive(Debug, Default, Clone)]
pub struct Section {
pub label: String,
pub kind: String,
pub entries: Vec<Entry>,
pub notes: Vec<String>,
pub rows: Vec<(String, String)>,
}
#[derive(Debug, Default, Clone)]
pub struct Entry {
pub role: String,
pub aside: String,
pub company: String,
pub extra: String,
pub meta: String,
pub bullets: Vec<String>,
}
pub fn parse(md: &str) -> ResumeDoc {
let mut doc = ResumeDoc::default();
for raw in md.lines() {
let line = raw.trim_end();
if line.trim().is_empty() {
continue;
}
if let Some(rest) = line.strip_prefix("# ") {
doc.name = plain(rest);
} else if let Some(rest) = line.strip_prefix("> ") {
doc.contact.push(rest.trim().to_string());
} else if let Some(rest) = line.strip_prefix("## ") {
let (label, kind) = split_kind(rest.trim());
doc.sections.push(Section {
label,
kind,
..Default::default()
});
} else if let Some(rest) = line.strip_prefix("### ") {
if let Some(sec) = doc.sections.last_mut() {
let (left, right) = match rest.split_once('|') {
Some((l, r)) => (l.trim(), r.trim()),
None => (rest.trim(), ""),
};
sec.entries.push(Entry {
role: plain(left),
aside: plain(right),
..Default::default()
});
}
} else if let Some(rest) = line.strip_prefix("- ") {
let item = rest.trim().to_string();
if let Some(sec) = doc.sections.last_mut() {
if sec.kind == "skills" {
sec.rows.push(skills_row(&item));
} else if let Some(entry) = sec.entries.last_mut() {
entry.bullets.push(item);
} else {
sec.notes.push(item);
}
}
} else {
let line = line.trim();
let took_company = doc
.sections
.last_mut()
.and_then(|s| s.entries.last_mut())
.filter(|e| e.company.is_empty() && line.starts_with("**"))
.map(|e| {
let (company, extra, meta) = company_line(line);
e.company = company;
e.extra = extra;
e.meta = meta;
})
.is_some();
if !took_company {
match doc.sections.last_mut() {
Some(sec) => sec.notes.push(line.to_string()),
None => doc.contact.push(line.to_string()),
}
}
}
}
doc
}
fn split_kind(label: &str) -> (String, String) {
if let Some(rest) = label.strip_suffix('}')
&& let Some(open) = rest.rfind('{')
{
let kind = &rest[open + 1..];
if !kind.is_empty() && kind.chars().all(|c| c.is_alphanumeric() || c == '_') {
return (rest[..open].trim().to_string(), kind.to_string());
}
}
(label.to_string(), "entries".to_string())
}
fn company_line(line: &str) -> (String, String, String) {
let parts = split_pipes_outside_bold(line);
let head = parts[0].as_str();
let meta = parts[1..].join(" | ");
if let Some(rest) = head.strip_prefix("**")
&& let Some(close) = rest.find("**")
{
let company = rest[..close].to_string();
let after = rest[close + 2..].trim();
if after.is_empty() {
return (company, String::new(), meta);
}
if after.len() >= 2 && after.starts_with('*') && after.ends_with('*') {
let inner = &after[1..after.len() - 1];
if !inner.is_empty() && !inner.contains('*') {
return (company, inner.to_string(), meta);
}
}
}
(plain(head), String::new(), meta)
}
fn split_pipes_outside_bold(line: &str) -> Vec<String> {
let chars: Vec<char> = line.chars().collect();
let mut parts: Vec<String> = Vec::new();
let mut cur = String::new();
let mut in_bold = false;
let mut i = 0;
while i < chars.len() {
if chars[i] == '*' && chars.get(i + 1) == Some(&'*') {
in_bold = !in_bold;
cur.push_str("**");
i += 2;
continue;
}
if chars[i] == '|' && !in_bold {
parts.push(cur.trim().to_string());
cur.clear();
} else {
cur.push(chars[i]);
}
i += 1;
}
parts.push(cur.trim().to_string());
parts
}
fn skills_row(item: &str) -> (String, String) {
if let Some(rest) = item.strip_prefix("**")
&& let Some(close) = rest.find("**")
{
let mut label = rest[..close].trim_end().to_string();
if label.ends_with(':') {
label.pop();
}
let mut value = rest[close + 2..].trim_start();
if let Some(v) = value.strip_prefix(':') {
value = v.trim_start();
}
return (label, value.to_string());
}
(String::new(), item.to_string())
}
#[cfg(test)]
mod tests {
use super::*;
const SAMPLE: &str = "\
# Jane Doe
> jane@doe.dev | 555-0100
> Portland, OR
## Experience
### Staff Engineer
**Acme Corp** *Platform* | 2020 - Present | Remote
- Shipped the **thing**
- Kept it running
## Skills {skills}
- **Languages:** Rust, Python
## Education {education}
### State University | 2012 - 2016
**BS Computer Science** | Magna cum laude
";
#[test]
fn bullets_without_entry_become_notes() {
let doc = parse("# N\n\n## Summary\n- important point\n");
assert_eq!(doc.sections[0].notes, vec!["important point"]);
}
#[test]
fn note_with_bold_is_not_a_company_line() {
let doc = parse("# N\n\n## X\n### Role\nA note with **bold** words\n**RealCo** | 2020\n");
let e = &doc.sections[0].entries[0];
assert_eq!(e.company, "RealCo");
assert_eq!(e.meta, "2020");
assert_eq!(doc.sections[0].notes, vec!["A note with **bold** words"]);
}
#[test]
fn pipe_inside_bold_stays_in_company() {
let doc = parse("# N\n\n## X\n### Role\n**Acme | Corp** | 2020\n");
let e = &doc.sections[0].entries[0];
assert_eq!(e.company, "Acme | Corp");
assert_eq!(e.meta, "2020");
}
#[test]
fn parses_structure() {
let doc = parse(SAMPLE);
assert_eq!(doc.name, "Jane Doe");
assert_eq!(doc.contact.len(), 2);
assert_eq!(doc.sections.len(), 3);
let exp = &doc.sections[0];
assert_eq!(exp.kind, "entries");
let e = &exp.entries[0];
assert_eq!(e.role, "Staff Engineer");
assert_eq!(e.company, "Acme Corp");
assert_eq!(e.extra, "Platform");
assert_eq!(e.meta, "2020 - Present | Remote");
assert_eq!(e.bullets.len(), 2);
let skills = &doc.sections[1];
assert_eq!(skills.kind, "skills");
assert_eq!(skills.rows[0], ("Languages".into(), "Rust, Python".into()));
let edu = &doc.sections[2];
assert_eq!(edu.entries[0].role, "State University");
assert_eq!(edu.entries[0].aside, "2012 - 2016");
assert_eq!(edu.entries[0].company, "BS Computer Science");
assert_eq!(edu.entries[0].meta, "Magna cum laude");
}
}