use fparkan_path::{normalize_relative, PathPolicy}; use fparkan_vfs::Vfs; use std::{collections::BTreeMap, sync::Arc}; pub(super) fn read(vfs: &dyn Vfs, path: &str) -> Result, String> { let path = normalize_relative( path.replace("\\\\", "\\").as_bytes(), PathPolicy::StrictLegacy, ) .map_err(|error| error.to_string())?; vfs.read(&path).map_err(|error| error.to_string()) } pub(super) fn object<'a>(text: &'a str, name: &str) -> Option> { let mut lines = text .lines() .map(|line| line.split('#').next().unwrap_or_default().trim()); for line in lines.by_ref() { let mut words = line.split_whitespace(); if words .next() .is_some_and(|word| word.eq_ignore_ascii_case("object")) && words .next() .is_some_and(|word| word.eq_ignore_ascii_case(name)) { return Some( lines .take_while(|line| !line.eq_ignore_ascii_case("end")) .filter_map(|line| line.split_once('=')) .map(|(key, value)| (key.trim().to_ascii_uppercase(), value.trim())) .collect(), ); } } None } pub(super) fn field<'a>(fields: &BTreeMap, key: &str) -> Result<&'a str, String> { let raw = fields.get(key).ok_or_else(|| format!("missing {key}"))?; if let Some(quoted) = raw.strip_prefix('"') { return quoted .split_once('"') .map(|(value, _)| value) .ok_or_else(|| format!("invalid quoted value for {key}")); } Ok(raw) } pub(super) fn number(fields: &BTreeMap, key: &str) -> Result { field(fields, key)? .parse() .map_err(|_| format!("invalid number for {key}")) }