Refactor executable library handbooks and improve document reading
This commit is contained in:
1 parent
16c44588d5
commit
c50c515c4a
191 files changed
+29572
-7816
No files matched your search
+30
-297
@@ -1,305 +1,38 @@
|
||||
//! Minimal TOML → Marqdo Value for Mid2 M8 (`lib/toml`).
|
||||
//! Subset: comments, keys, strings, ints, floats, bools, arrays of scalars,
|
||||
//! `[table]` / dotted tables. No datetime, inline tables, or array-of-tables.
|
||||
//! Standards-based TOML; datetime scalars become explicit ISO text at Value boundary.
|
||||
use crate::{host::as_text,value::Value};
|
||||
|
||||
use crate::host::as_text;
|
||||
use crate::host::json::json_to_value;
|
||||
use crate::value::Value;
|
||||
|
||||
|
||||
pub fn parse(text: &Value) -> Result<Value, String> {
|
||||
let raw = as_text(text, "text")?;
|
||||
let json = toml_to_json(raw)?;
|
||||
json_to_value(&json)
|
||||
pub fn parse(text: &Value) -> Result<Value,String> {
|
||||
let parsed: toml::Value = toml::from_str(as_text(text,"text")?).map_err(|e|format!("toml.parse: {e}"))?;
|
||||
fn convert(v: toml::Value) -> Result<Value,String> {
|
||||
Ok(match v {
|
||||
toml::Value::String(s) => Value::Text(s),
|
||||
toml::Value::Integer(n) => Value::Int(n),
|
||||
toml::Value::Float(n) if n.is_finite() => Value::Num(n),
|
||||
toml::Value::Float(_) => return Err("toml: non-finite numbers unsupported".into()),
|
||||
toml::Value::Boolean(b) => Value::Bool(b),
|
||||
toml::Value::Datetime(dt) => Value::Text(dt.to_string()),
|
||||
toml::Value::Array(xs) => Value::List(xs.into_iter().map(convert).collect::<Result<_,_>>()?),
|
||||
toml::Value::Table(fields) => Value::Map(fields.into_iter().map(|(k,v)|convert(v).map(|v|(k,v))).collect::<Result<_,_>>()?),
|
||||
})
|
||||
}
|
||||
convert(parsed)
|
||||
}
|
||||
|
||||
fn toml_to_json(src: &str) -> Result<serde_json::Value, String> {
|
||||
let mut root = serde_json::Map::new();
|
||||
let mut current_path: Vec<String> = Vec::new();
|
||||
for (lineno, line) in src.lines().enumerate() {
|
||||
let line_no = lineno + 1;
|
||||
let stripped = strip_comment(line);
|
||||
let t = stripped.trim();
|
||||
if t.is_empty() {
|
||||
continue;
|
||||
}
|
||||
if t.starts_with('[') {
|
||||
if !t.ends_with(']') {
|
||||
return Err(format!("toml.parse:{line_no}: bad table header"));
|
||||
}
|
||||
if t.starts_with("[[") {
|
||||
return Err(format!(
|
||||
"toml.parse:{line_no}: array-of-tables [[...]] not supported"
|
||||
));
|
||||
}
|
||||
let inner = t[1..t.len() - 1].trim();
|
||||
current_path = split_dotted_keys(inner, line_no)?;
|
||||
ensure_table_path(&mut root, ¤t_path, line_no)?;
|
||||
continue;
|
||||
}
|
||||
let (key, val) = t
|
||||
.split_once('=')
|
||||
.ok_or_else(|| format!("toml.parse:{line_no}: expected key = value"))?;
|
||||
let key = key.trim();
|
||||
if key.is_empty() {
|
||||
return Err(format!("toml.parse:{line_no}: empty key"));
|
||||
}
|
||||
let key_parts = split_dotted_keys(key, line_no)?;
|
||||
let value = parse_value(val.trim(), line_no)?;
|
||||
let parent = table_at_path(&mut root, ¤t_path, line_no)?;
|
||||
insert_dotted(parent, &key_parts, value, line_no)?;
|
||||
pub fn stringify(value:&Value)->Result<Value,String>{
|
||||
fn convert(v:&Value)->Result<toml::Value,String>{
|
||||
Ok(match v {
|
||||
Value::Text(s)=>toml::Value::String(s.clone()),
|
||||
Value::Int(n)=>toml::Value::Integer(*n),
|
||||
Value::Num(n) if n.is_finite()=>toml::Value::Float(*n),
|
||||
Value::Bool(b)=>toml::Value::Boolean(*b),
|
||||
Value::List(xs)=>toml::Value::Array(xs.iter().map(convert).collect::<Result<_,_>>()?),
|
||||
Value::Map(fields)=>toml::Value::Table(fields.iter().map(|(k,v)|convert(v).map(|v|(k.clone(),v))).collect::<Result<_,_>>()?),
|
||||
_=>return Err("toml.stringify supports text/finite numbers/bool/list/map; None has no TOML representation".into()),
|
||||
})
|
||||
}
|
||||
Ok(serde_json::Value::Object(root))
|
||||
if !matches!(value,Value::Map(_)){return Err("toml root must be map".into())}
|
||||
toml::to_string_pretty(&convert(value)?).map(Value::Text).map_err(|e|format!("toml.stringify: {e}"))
|
||||
}
|
||||
|
||||
fn strip_comment(line: &str) -> String {
|
||||
// Strip unquoted `#` comments.
|
||||
let mut out = String::new();
|
||||
let mut in_str = false;
|
||||
let mut chars = line.chars().peekable();
|
||||
while let Some(c) = chars.next() {
|
||||
if !in_str && c == '#' {
|
||||
break;
|
||||
}
|
||||
if c == '"' {
|
||||
out.push(c);
|
||||
in_str = !in_str;
|
||||
continue;
|
||||
}
|
||||
if in_str && c == '\\' {
|
||||
out.push(c);
|
||||
if let Some(n) = chars.next() {
|
||||
out.push(n);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
out.push(c);
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn split_dotted_keys(s: &str, line_no: usize) -> Result<Vec<String>, String> {
|
||||
let mut parts = Vec::new();
|
||||
let mut cur = String::new();
|
||||
let mut in_quote = false;
|
||||
let mut chars = s.chars().peekable();
|
||||
while let Some(c) = chars.next() {
|
||||
if c == '"' {
|
||||
in_quote = !in_quote;
|
||||
continue;
|
||||
}
|
||||
if c == '.' && !in_quote {
|
||||
if cur.is_empty() {
|
||||
return Err(format!("toml.parse:{line_no}: empty key segment"));
|
||||
}
|
||||
parts.push(std::mem::take(&mut cur));
|
||||
continue;
|
||||
}
|
||||
cur.push(c);
|
||||
}
|
||||
if in_quote {
|
||||
return Err(format!("toml.parse:{line_no}: unclosed quoted key"));
|
||||
}
|
||||
if !cur.is_empty() {
|
||||
parts.push(cur);
|
||||
}
|
||||
if parts.is_empty() {
|
||||
return Err(format!("toml.parse:{line_no}: empty path"));
|
||||
}
|
||||
Ok(parts)
|
||||
}
|
||||
|
||||
fn ensure_table_path(
|
||||
root: &mut serde_json::Map<String, serde_json::Value>,
|
||||
path: &[String],
|
||||
line_no: usize,
|
||||
) -> Result<(), String> {
|
||||
let mut cur = root;
|
||||
for seg in path {
|
||||
if !cur.contains_key(seg) {
|
||||
cur.insert(seg.clone(), serde_json::Value::Object(serde_json::Map::new()));
|
||||
}
|
||||
cur = match cur.get_mut(seg) {
|
||||
Some(serde_json::Value::Object(m)) => m,
|
||||
Some(_) => {
|
||||
return Err(format!("toml.parse:{line_no}: {seg} is not a table"));
|
||||
}
|
||||
None => unreachable!(),
|
||||
};
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn table_at_path<'a>(
|
||||
root: &'a mut serde_json::Map<String, serde_json::Value>,
|
||||
path: &[String],
|
||||
line_no: usize,
|
||||
) -> Result<&'a mut serde_json::Map<String, serde_json::Value>, String> {
|
||||
let mut cur = root;
|
||||
for seg in path {
|
||||
cur = match cur.get_mut(seg) {
|
||||
Some(serde_json::Value::Object(m)) => m,
|
||||
Some(_) => {
|
||||
return Err(format!("toml.parse:{line_no}: {seg} is not a table"));
|
||||
}
|
||||
None => {
|
||||
return Err(format!("toml.parse:{line_no}: missing table {seg}"));
|
||||
}
|
||||
};
|
||||
}
|
||||
Ok(cur)
|
||||
}
|
||||
|
||||
fn insert_dotted(
|
||||
parent: &mut serde_json::Map<String, serde_json::Value>,
|
||||
parts: &[String],
|
||||
value: serde_json::Value,
|
||||
line_no: usize,
|
||||
) -> Result<(), String> {
|
||||
if parts.len() == 1 {
|
||||
if parent.contains_key(&parts[0]) {
|
||||
return Err(format!(
|
||||
"toml.parse:{line_no}: duplicate key {}",
|
||||
parts[0]
|
||||
));
|
||||
}
|
||||
parent.insert(parts[0].clone(), value);
|
||||
return Ok(());
|
||||
}
|
||||
let head = &parts[0];
|
||||
let entry = parent
|
||||
.entry(head.clone())
|
||||
.or_insert_with(|| serde_json::Value::Object(serde_json::Map::new()));
|
||||
let obj = match entry {
|
||||
serde_json::Value::Object(m) => m,
|
||||
_ => {
|
||||
return Err(format!("toml.parse:{line_no}: {head} is not a table"));
|
||||
}
|
||||
};
|
||||
insert_dotted(obj, &parts[1..], value, line_no)
|
||||
}
|
||||
|
||||
fn parse_value(s: &str, line_no: usize) -> Result<serde_json::Value, String> {
|
||||
if s.is_empty() {
|
||||
return Err(format!("toml.parse:{line_no}: empty value"));
|
||||
}
|
||||
if s == "true" {
|
||||
return Ok(serde_json::Value::Bool(true));
|
||||
}
|
||||
if s == "false" {
|
||||
return Ok(serde_json::Value::Bool(false));
|
||||
}
|
||||
if s.starts_with('"') {
|
||||
return Ok(serde_json::Value::String(parse_basic_string(s, line_no)?));
|
||||
}
|
||||
if s.starts_with('\'') {
|
||||
return Ok(serde_json::Value::String(parse_literal_string(s, line_no)?));
|
||||
}
|
||||
if s.starts_with('[') {
|
||||
return parse_array(s, line_no);
|
||||
}
|
||||
if let Ok(i) = s.replace('_', "").parse::<i64>() {
|
||||
return Ok(serde_json::Value::Number(i.into()));
|
||||
}
|
||||
if let Ok(f) = s.replace('_', "").parse::<f64>() {
|
||||
if let Some(n) = serde_json::Number::from_f64(f) {
|
||||
return Ok(serde_json::Value::Number(n));
|
||||
}
|
||||
}
|
||||
Err(format!("toml.parse:{line_no}: unsupported value {s:?}"))
|
||||
}
|
||||
|
||||
fn parse_basic_string(s: &str, line_no: usize) -> Result<String, String> {
|
||||
if !s.ends_with('"') || s.len() < 2 {
|
||||
return Err(format!("toml.parse:{line_no}: bad string"));
|
||||
}
|
||||
let inner = &s[1..s.len() - 1];
|
||||
let mut out = String::new();
|
||||
let mut chars = inner.chars();
|
||||
while let Some(c) = chars.next() {
|
||||
if c == '\\' {
|
||||
match chars.next() {
|
||||
Some('n') => out.push('\n'),
|
||||
Some('t') => out.push('\t'),
|
||||
Some('r') => out.push('\r'),
|
||||
Some('\\') => out.push('\\'),
|
||||
Some('"') => out.push('"'),
|
||||
Some(other) => {
|
||||
return Err(format!(
|
||||
"toml.parse:{line_no}: bad escape \\{other}"
|
||||
));
|
||||
}
|
||||
None => return Err(format!("toml.parse:{line_no}: trailing escape")),
|
||||
}
|
||||
} else {
|
||||
out.push(c);
|
||||
}
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
fn parse_literal_string(s: &str, line_no: usize) -> Result<String, String> {
|
||||
if !s.ends_with('\'') || s.len() < 2 {
|
||||
return Err(format!("toml.parse:{line_no}: bad literal string"));
|
||||
}
|
||||
Ok(s[1..s.len() - 1].to_string())
|
||||
}
|
||||
|
||||
fn parse_array(s: &str, line_no: usize) -> Result<serde_json::Value, String> {
|
||||
if !s.ends_with(']') {
|
||||
return Err(format!("toml.parse:{line_no}: bad array"));
|
||||
}
|
||||
let inner = s[1..s.len() - 1].trim();
|
||||
if inner.is_empty() {
|
||||
return Ok(serde_json::Value::Array(vec![]));
|
||||
}
|
||||
let mut items = Vec::new();
|
||||
let mut cur = String::new();
|
||||
let mut depth = 0i32;
|
||||
let mut in_str = false;
|
||||
let mut str_ch = '"';
|
||||
let mut chars = inner.chars().peekable();
|
||||
while let Some(c) = chars.next() {
|
||||
if in_str {
|
||||
cur.push(c);
|
||||
if c == '\\' {
|
||||
if let Some(n) = chars.next() {
|
||||
cur.push(n);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if c == str_ch {
|
||||
in_str = false;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
match c {
|
||||
'"' | '\'' => {
|
||||
in_str = true;
|
||||
str_ch = c;
|
||||
cur.push(c);
|
||||
}
|
||||
'[' => {
|
||||
depth += 1;
|
||||
cur.push(c);
|
||||
}
|
||||
']' => {
|
||||
depth -= 1;
|
||||
cur.push(c);
|
||||
}
|
||||
',' if depth == 0 => {
|
||||
items.push(parse_value(cur.trim(), line_no)?);
|
||||
cur.clear();
|
||||
}
|
||||
_ => cur.push(c),
|
||||
}
|
||||
}
|
||||
if !cur.trim().is_empty() {
|
||||
items.push(parse_value(cur.trim(), line_no)?);
|
||||
}
|
||||
Ok(serde_json::Value::Array(items))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
Reference in new issue
Block a user