feat(stdlib): Mid M6 log levels and UUID v4

Close the mid-tier stdlib waves with filtered one-line logs and cryptographic UUID minting in lib/, plus golds and public demos.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
cflmyandCursor committed 2026-09-07 20:37:28 +08:00
1 parent 7d86ed1d9c
commit f4b39dfe80
20 files changed
+430 -15

No files matched your search

+20 -2
View File
@@ -3,8 +3,8 @@
use std::collections::HashMap;
use crate::host::{
cli, csv, encoding, fs, hash, json, math, net, path, re, secrets, sys, text_ops, time,
writeback, HostContext,
cli, csv, encoding, fs, hash, json, log, math, net, path, re, secrets, sys, text_ops, time,
uuid_ops, writeback, HostContext,
};
#[cfg(feature = "exec-host")]
use crate::host::{foreign, subtask};
@@ -173,6 +173,9 @@ pub enum HostFn {
ListZip = 164,
ListFlatten = 165,
CliParse = 166,
LogSetLevel = 167,
LogLine = 168,
UuidV4 = 169,
}
@@ -336,6 +339,9 @@ impl HostFn {
164 => Self::ListZip,
165 => Self::ListFlatten,
166 => Self::CliParse,
167 => Self::LogSetLevel,
168 => Self::LogLine,
169 => Self::UuidV4,
_ => return None,
})
}
@@ -502,6 +508,9 @@ impl HostFn {
"host_list_zip" | "list_zip" => Self::ListZip,
"host_list_flatten" | "list_flatten" => Self::ListFlatten,
"host_cli_parse" | "cli_parse" => Self::CliParse,
"host_log_set_level" | "log_set_level" => Self::LogSetLevel,
"host_log_line" | "log_line" => Self::LogLine,
"host_uuid_v4" | "uuid_v4" => Self::UuidV4,
_ => return None,
})
}
@@ -655,6 +664,9 @@ impl HostFn {
Self::ListZip => "host_list_zip",
Self::ListFlatten => "host_list_flatten",
Self::CliParse => "host_cli_parse",
Self::LogSetLevel => "host_log_set_level",
Self::LogLine => "host_log_line",
Self::UuidV4 => "host_uuid_v4",
Self::WritebackRecord => "host_writeback_record",
Self::WritebackGet => "host_writeback_get",
Self::WritebackClear => "host_writeback_clear",
@@ -774,6 +786,9 @@ impl HostFn {
Self::ListChunk => &["list", "size"],
Self::ListZip => &["a", "b"],
Self::CliParse => &[],
Self::LogSetLevel => &["level"],
Self::LogLine => &["level", "text"],
Self::UuidV4 => &[],
Self::WritebackRecord => &["value"],
Self::WritebackGet | Self::WritebackClear => &[],
Self::WritebackList => &[],
@@ -1254,6 +1269,9 @@ pub fn call_host(
}
HostFn::ListFlatten => super::collection::list_flatten(require(bound, "list")?),
HostFn::CliParse => cli::parse(ctx, bound.get("args")),
HostFn::LogSetLevel => log::set_level(ctx, require(bound, "level")?),
HostFn::LogLine => log::line(ctx, require(bound, "level")?, require(bound, "text")?),
HostFn::UuidV4 => uuid_ops::v4(),
HostFn::OuterCallLine => Ok(Value::Int(
ctx.call_site_lines
.first()
+82
View File
@@ -0,0 +1,82 @@
//! Log level gate for Mid M6 (`lib/log`).
use crate::host::HostContext;
use crate::value::Value;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum Level {
Debug = 10,
Info = 20,
Warn = 30,
Error = 40,
}
impl Level {
pub fn parse(s: &str) -> Result<Self, String> {
match s.trim().to_ascii_lowercase().as_str() {
"debug" | "调试" => Ok(Self::Debug),
"info" | "信息" => Ok(Self::Info),
"warn" | "warning" | "警告" => Ok(Self::Warn),
"error" | "错误" => Ok(Self::Error),
_ => Err(format!("log: unknown level {s:?}")),
}
}
pub fn label(self) -> &'static str {
match self {
Self::Debug => "DEBUG",
Self::Info => "INFO",
Self::Warn => "WARN",
Self::Error => "ERROR",
}
}
}
fn as_text<'a>(v: &'a Value, label: &str) -> Result<&'a str, String> {
match v {
Value::Text(s) => Ok(s.as_str()),
_ => Err(format!("{label} needs text")),
}
}
pub fn set_level(ctx: &mut HostContext, level: &Value) -> Result<Value, String> {
let s = as_text(level, "level")?;
ctx.log_min_level = Level::parse(s)? as u8;
Ok(Value::None)
}
/// Return a ready-to-print line, or `None` if filtered out.
pub fn line(ctx: &HostContext, level: &Value, text: &Value) -> Result<Value, String> {
let lvl = Level::parse(as_text(level, "level")?)?;
let msg = as_text(text, "text")?;
if (lvl as u8) < ctx.log_min_level {
return Ok(Value::None);
}
Ok(Value::Text(format!("{} {}", lvl.label(), msg)))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn filters_debug() {
let mut ctx = HostContext::default();
assert_eq!(ctx.log_min_level, Level::Info as u8);
let filtered = line(
&ctx,
&Value::Text("debug".into()),
&Value::Text("x".into()),
)
.unwrap();
assert_eq!(filtered, Value::None);
set_level(&mut ctx, &Value::Text("debug".into())).unwrap();
let shown = line(
&ctx,
&Value::Text("debug".into()),
&Value::Text("x".into()),
)
.unwrap();
assert_eq!(shown, Value::Text("DEBUG x".into()));
}
}
+5
View File
@@ -16,12 +16,14 @@ pub mod foreign;
mod fs;
mod hash;
pub(crate) mod json;
mod log;
pub mod math;
mod net;
mod path;
mod re;
mod secrets;
mod text_ops;
mod uuid_ops;
#[cfg(feature = "plugin-host")]
pub mod plugin;
#[cfg(not(feature = "plugin-host"))]
@@ -100,6 +102,8 @@ pub struct HostContext {
#[cfg_attr(not(feature = "exec-host"), allow(dead_code))]
pub(crate) subtasks: HashMap<u64, subtask::Handle>,
pub subtask_seq: u64,
/// Minimum log level (`lib/log`); default Info (20). See `host::log::Level`.
pub log_min_level: u8,
}
impl Default for HostContext {
@@ -122,6 +126,7 @@ impl Default for HostContext {
call_site_lines: Vec::new(),
subtasks: HashMap::new(),
subtask_seq: 0,
log_min_level: 20, // Info
}
}
}
+45
View File
@@ -0,0 +1,45 @@
//! UUID v4 for Mid M6 (`lib/uuid`).
use crate::value::Value;
pub fn v4() -> Result<Value, String> {
let mut bytes = [0u8; 16];
getrandom::getrandom(&mut bytes).map_err(|e| format!("uuid.v4: {e}"))?;
// RFC 4122 version 4 + variant 10xx
bytes[6] = (bytes[6] & 0x0f) | 0x40;
bytes[8] = (bytes[8] & 0x3f) | 0x80;
Ok(Value::Text(format!(
"{:02x}{:02x}{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}",
bytes[0],
bytes[1],
bytes[2],
bytes[3],
bytes[4],
bytes[5],
bytes[6],
bytes[7],
bytes[8],
bytes[9],
bytes[10],
bytes[11],
bytes[12],
bytes[13],
bytes[14],
bytes[15],
)))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn v4_shape() {
let v = v4().unwrap();
let Value::Text(s) = v else { panic!("text") };
assert_eq!(s.len(), 36);
assert_eq!(&s[14..15], "4");
let variant = u8::from_str_radix(&s[19..20], 16).unwrap();
assert!(variant == 8 || variant == 9 || variant == 0xa || variant == 0xb);
}
}