///|
pub struct MboxEntry {
envelope : String
raw_message : String
} derive(Eq, Debug)
///|
pub fn parse_mbox(input : String) -> Array[MboxEntry] {
let lines = split_lines(input)
let out : Array[MboxEntry] = []
let mut envelope = ""
let mut current = ""
let mut started = false
for line in lines {
if starts_with(line, "From ") {
if started {
out.push({ envelope, raw_message: current })
}
envelope = line
current = ""
started = true
} else if started {
current = current + unescape_mbox_line(line) + "\n"
}
}
if started {
out.push({ envelope, raw_message: current })
}
out
}
///|
fn unescape_mbox_line(line : String) -> String {
if starts_with(line, ">From ") {
slice(line, 1, line.length())
} else {
line
}
}
///|
pub fn parse_mbox_messages(input : String) -> Array[MailMessage] {
let out : Array[MailMessage] = []
for entry in parse_mbox(input) {
out.push(parse_message(entry.raw_message).message)
}
out
}
///|
pub fn mbox_message_count(input : String) -> Int {
parse_mbox(input).length()
}
///|
pub fn render_mbox(messages : Array[String]) -> String {
let mut out = ""
let mut i = 0
for raw in messages {
out = out +
"From mailkit@example.local Sat Jan 01 00:00:0" +
i.to_string() +
" 2026\n"
for line in split_lines(raw) {
if starts_with(line, "From ") {
out = out + ">" + line + "\n"
} else {
out = out + line + "\n"
}
}
i = i + 1
}
out
}