#[derive(Debug, Clone)]
pub struct SectionRange {
pub title: String,
pub kind: SectionKind,
pub start_line: usize, pub end_line: usize, }
#[derive(Debug, Clone, PartialEq)]
pub enum SectionKind {
Version,
Well,
Curves,
Parameter,
Other,
Data,
Custom(String),
}
pub fn discover_sections(lines: &[&str]) -> Vec<SectionRange> {
let mut sections = Vec::new();
let mut i = 0;
while i < lines.len() {
let trimmed = lines[i].trim();
if trimmed.starts_with('~') {
let title = trimmed[1..].to_string();
let kind = classify_section(&title);
let start_line = i + 1;
let mut end_line = lines.len();
for j in start_line..lines.len() {
let t = lines[j].trim();
if t.starts_with('~') {
end_line = j;
break;
}
}
sections.push(SectionRange {
title,
kind,
start_line,
end_line,
});
i = start_line;
} else {
i += 1;
}
}
sections
}
fn classify_section(title: &str) -> SectionKind {
let upper = title.to_uppercase();
let first_word = upper.split_whitespace().next().unwrap_or("");
if upper.contains("LOG_DEFINITION") || upper.contains("LOG DEFINITION")
|| upper.contains("CURVE_DEFINITION") || upper.contains("CURVE DEFINITION") {
return SectionKind::Curves;
}
if upper.contains("LOG_PARAMETER") || upper.contains("LOG PARAMETER")
|| upper.contains("CURVE_PARAMETER") || upper.contains("CURVE PARAMETER") {
return SectionKind::Parameter;
}
if upper.contains("_DATA")
|| upper.starts_with("LOG_ASCII") || upper.starts_with("LOG ASCII")
|| upper.starts_with("CURVE_ASCII") || upper.starts_with("CURVE ASCII") {
if !upper.starts_with("A") {
return SectionKind::Data;
}
}
match first_word {
"V" | "VERSION" => SectionKind::Version,
"W" | "WELL" => SectionKind::Well,
"C" | "CURVE" | "CURVES" => SectionKind::Curves,
"P" | "PARAMETER" | "PARAMS" => SectionKind::Parameter,
"O" | "OTHER" => SectionKind::Other,
"A" | "ASCII" => SectionKind::Data,
_ => {
if first_word.len() == 1 {
match first_word.chars().next().unwrap() {
'V' => SectionKind::Version,
'W' => SectionKind::Well,
'C' => SectionKind::Curves,
'P' => SectionKind::Parameter,
'O' => SectionKind::Other,
'A' => SectionKind::Data,
_ => SectionKind::Custom(title.to_string()),
}
} else {
SectionKind::Custom(title.to_string())
}
}
}
}