lumesh 0.18.2

a lighting shell ⚡ bash alternative
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
// use{get_list_arg, get_string_arg};
use crate::{
    Environment, Expression, RuntimeError,
    expression::table::TableData,
    libs::{
        BuiltinInfo,
        bin::into_lib,
        helper::{check_exact_args_len, get_string_ref},
        lazy_module::LazyModule,
    },
    parse, reg_info, reg_lazy,
};
use regex_lite::Regex;
use std::{collections::BTreeMap, sync::OnceLock};
use tinyjson::JsonValue;
static SELECT_RE: OnceLock<Regex> = OnceLock::new();

pub fn regist_lazy() -> LazyModule {
    reg_lazy!({
        // 数据格式解析
        toml, json, csv,
        // 表达式解析
        script,
        // 解析第三方命令输出(into库)
        // 命令输出解析
        cmd,
        // 数据查询
        jq,
    })
}

pub fn regist_info() -> BTreeMap<&'static str, BuiltinInfo> {
    reg_info!({
        // 数据格式解析
        toml => "parse TOML string", "<toml_string>"
        json => "parse JSON string", "<json_string>"
        csv => "parse CSV string, headers row required", "<csv_string>"

        // 表达式解析
        script => "parse script text to expression (unevaluated)", "<script_string>"

        // 命令输出解析
        cmd => "parse cmd output into table", "<output> [split_regex] [headers...]"

        // 数据查询
        jq => "jq-like query on json string. e.g. '.a|.[]|select(.n>1)'", "<json_string> <query_string>"
    })
}

// TOML Parser Functions

fn toml(
    args: Vec<Expression>,
    _env: &mut Environment,
    ctx: &Expression,
) -> Result<Expression, RuntimeError> {
    check_exact_args_len("toml", &args, 1, ctx)?;
    let text_str = get_string_ref(&args[0], ctx)?;

    toml::from_str::<serde_json::Value>(text_str)
        .map(toml_to_expr)
        .map_err(|e| {
            RuntimeError::common(format!("Toml parser error:\n{e}").into(), ctx.clone(), 0)
        })
}

fn toml_to_expr(val: serde_json::Value) -> Expression {
    match val {
        serde_json::Value::Bool(b) => Expression::Boolean(b),
        serde_json::Value::Number(n) => {
            if let Some(i) = n.as_i64() {
                Expression::Integer(i)
            } else {
                Expression::Float(n.as_f64().unwrap_or(0.0))
            }
        }
        serde_json::Value::String(s) => Expression::String(s),
        serde_json::Value::Array(a) => {
            if let Some((headers, rows)) = try_convert_toml_array_to_table(&a) {
                Expression::Table(TableData::new(headers, rows))
            } else {
                Expression::from(a.into_iter().map(toml_to_expr).collect::<Vec<Expression>>())
            }
        }
        serde_json::Value::Object(o) => Expression::from(
            o.into_iter()
                .map(|(k, v)| (k, toml_to_expr(v)))
                .collect::<BTreeMap<String, Expression>>(),
        ),
        serde_json::Value::Null => Expression::None,
    }
}

// Helper function for TOML array of tables
fn try_convert_toml_array_to_table(
    arr: &[serde_json::Value],
) -> Option<(Vec<String>, Vec<Vec<Expression>>)> {
    if arr.is_empty() {
        return None;
    }

    // Check if all elements are tables
    if !arr
        .iter()
        .all(|v| matches!(v, serde_json::Value::Object(_)))
    {
        return None;
    }

    // Similar logic to JSON version...
    let mut all_keys = std::collections::BTreeSet::new();
    for item in arr {
        if let serde_json::Value::Object(table) = item {
            for key in table.keys() {
                all_keys.insert(key.clone());
            }
        }
    }

    let headers: Vec<String> = all_keys.into_iter().collect();
    let mut rows = Vec::new();

    for item in arr {
        if let serde_json::Value::Object(table) = item {
            let row: Vec<Expression> = headers
                .iter()
                .map(|key| {
                    table
                        .get(key)
                        .map(|v| toml_to_expr(v.clone()))
                        .unwrap_or(Expression::None)
                })
                .collect();
            rows.push(row);
        }
    }

    Some((headers, rows))
}

// JSON Parser Functions
fn json(
    args: Vec<Expression>,
    _env: &mut Environment,
    ctx: &Expression,
) -> Result<Expression, RuntimeError> {
    check_exact_args_len("json", &args, 1, ctx)?;
    let text_str = get_string_ref(&args[0], ctx)?;

    if text_str.is_empty() {
        return Ok(Expression::None);
    }

    text_str
        .parse::<JsonValue>()
        .map(json_to_expr)
        .map_err(|e| {
            RuntimeError::common(format!("Json parser error:\n{e}").into(), ctx.clone(), 0)
        })
}

// TODO: add bset if needed
fn json_to_expr(val: JsonValue) -> Expression {
    match val {
        JsonValue::Null => Expression::None,
        JsonValue::Boolean(b) => Expression::Boolean(b),
        JsonValue::Number(n) => {
            if n.fract() == 0.0 {
                Expression::Integer(n as i64)
            } else {
                Expression::Float(n)
            }
        }
        JsonValue::String(s) => Expression::String(s),
        JsonValue::Array(a) => {
            // Check if this is an array of objects that could be a table
            if let Some((headers, rows)) = try_convert_array_to_table(&a) {
                Expression::Table(TableData::new(headers, rows))
            } else {
                Expression::from(a.into_iter().map(json_to_expr).collect::<Vec<Expression>>())
            }
        }
        JsonValue::Object(o) => Expression::from(
            o.into_iter()
                .map(|(k, v)| (k, json_to_expr(v)))
                .collect::<BTreeMap<String, Expression>>(),
        ),
    }
}

// Helper function to detect and convert array of objects to table
fn try_convert_array_to_table(arr: &[JsonValue]) -> Option<(Vec<String>, Vec<Vec<Expression>>)> {
    if arr.is_empty() {
        return None;
    }

    // Check if all elements are objects
    if !arr.iter().all(|v| matches!(v, JsonValue::Object(_))) {
        return None;
    }

    // Collect all unique keys from all objects
    let mut all_keys = std::collections::BTreeSet::new();
    for item in arr {
        if let JsonValue::Object(obj) = item {
            for key in obj.keys() {
                all_keys.insert(key.clone());
            }
        }
    }

    let headers: Vec<String> = all_keys.into_iter().collect();
    let mut rows = Vec::new();

    for item in arr {
        if let JsonValue::Object(obj) = item {
            let row: Vec<Expression> = headers
                .iter()
                .map(|key| {
                    obj.get(key)
                        .map(|v| json_to_expr(v.clone()))
                        .unwrap_or(Expression::None)
                })
                .collect();
            rows.push(row);
        }
    }

    Some((headers, rows))
}

// Expression Parser
fn script(
    args: Vec<Expression>,
    _env: &mut Environment,
    ctx: &Expression,
) -> Result<Expression, RuntimeError> {
    check_exact_args_len("script", &args, 1, ctx)?;
    let script = get_string_ref(&args[0], ctx)?;

    if script.is_empty() {
        return Ok(Expression::None);
    }

    parse(script).map_err(|e| {
        RuntimeError::common(format!("Script parser error:\n{e}").into(), ctx.clone(), 0)
    })
}

// Command Output Parser
fn cmd(
    args: Vec<Expression>,
    env: &mut Environment,
    ctx: &Expression,
) -> Result<Expression, RuntimeError> {
    into_lib::table(args, env, ctx)
}

// CSV Reader and Converter Functions
fn csv(
    args: Vec<Expression>,
    env: &mut Environment,
    ctx: &Expression,
) -> Result<Expression, RuntimeError> {
    check_exact_args_len("csv", &args, 1, ctx)?;
    let text = get_string_ref(&args[0], ctx)?;

    // 获取自定义分隔符
    let delimiter = match env.get("IFS") {
        Some(Expression::String(fs)) if fs != "\n" => fs.as_bytes()[0],
        _ => ",".as_bytes()[0].to_owned(), // 默认分隔符
    };

    // 设置 CSV 解析器的分隔符
    let mut reader = csv::ReaderBuilder::new()
        .has_headers(true)
        .delimiter(delimiter) // 将字符串转换为字节并取第一个字符
        .from_reader(text.as_bytes());

    let headers = reader
        .headers()
        .map_err(|e| {
            RuntimeError::common(format!("Csv header error:\n{e}").into(), ctx.clone(), 0)
        })?
        .iter()
        .map(|s| s.to_string())
        .collect::<Vec<_>>();

    let mut table = TableData::with_header(headers);
    for rec in reader.records() {
        let record = rec.map_err(|e| {
            RuntimeError::common(format!("CSV parse error: {e}").into(), ctx.clone(), 0)
        })?;

        let row: Vec<Expression> = record
            .iter()
            .map(|field| Expression::String(field.to_string()))
            .collect();
        table.push_row(row);
    }

    Ok(Expression::Table(table))
}

// 定义操作步骤的枚举
#[derive(Debug)]
enum JqStep {
    Field(String),
    Index(usize),
    Wildcard,
    Function(String, String),
}

fn jq(
    args: Vec<Expression>,
    _env: &mut Environment,
    ctx: &Expression,
) -> Result<Expression, RuntimeError> {
    check_exact_args_len("jq", &args, 2, ctx)?;
    let input = &args[0];
    let query = &args[1];

    let json_value = match input {
        Expression::String(s) => s.parse::<JsonValue>().map_err(|e| {
            RuntimeError::common(format!("Json parser error:\n{e}").into(), ctx.clone(), 0)
        })?,
        _ => {
            return Err(RuntimeError::common(
                "input must be a json string".into(),
                ctx.clone(),
                0,
            ));
        }
    };

    let query_result = match query {
        Expression::String(q) => {
            // 解析管道查询
            let pipeline = parse_jq_pipeline(q);
            apply_jq_pipeline(&pipeline, &json_value)
        }

        _ => {
            return Err(RuntimeError::common(
                "Query must be a string or function".into(),
                ctx.clone(),
                0,
            ));
        }
    };

    Ok(Expression::String(
        query_result.stringify().unwrap_or("".to_string()),
    ))
}

// 解析管道查询字符串
fn parse_jq_pipeline(query: &str) -> Vec<JqStep> {
    let mut steps = Vec::new();
    // 按管道符分割查询
    for part in query.split('|').map(|s| s.trim()) {
        if part.starts_with("select(") && part.ends_with(')') {
            // 处理select函数
            let arg = &part[7..part.len() - 1];
            steps.push(JqStep::Function("select".to_string(), arg.to_string()));
        } else if part == ".[]" {
            // 处理通配符
            steps.push(JqStep::Wildcard);
        } else if part.starts_with('[') && part.ends_with(']') {
            // 处理数组索引
            let index_str = &part[1..part.len() - 1];
            if let Ok(index) = index_str.parse::<usize>() {
                steps.push(JqStep::Index(index));
            }
        } else if part.starts_with('.') {
            // 处理字段访问
            let field_name = part.trim_start_matches('.').to_string();
            steps.push(JqStep::Field(field_name));
        }
    }
    steps
}

// 应用管道查询
fn apply_jq_pipeline(pipeline: &[JqStep], json_value: &JsonValue) -> JsonValue {
    let mut current_value = json_value.clone();
    for step in pipeline {
        current_value = apply_jq_step(step, &current_value);
    }
    current_value
}

// 应用单个查询步骤
fn apply_jq_step(step: &JqStep, json_value: &JsonValue) -> JsonValue {
    match step {
        JqStep::Field(field) => {
            if let JsonValue::Object(obj) = json_value {
                obj.get(field).cloned().unwrap_or(JsonValue::Null)
            } else {
                JsonValue::Null
            }
        }
        JqStep::Index(index) => {
            if let JsonValue::Array(arr) = json_value {
                if *index < arr.len() {
                    arr[*index].clone()
                } else {
                    JsonValue::Null
                }
            } else {
                JsonValue::Null
            }
        }
        JqStep::Wildcard => {
            if let JsonValue::Array(arr) = json_value {
                // 通配符返回整个数组
                JsonValue::Array(arr.clone())
            } else {
                JsonValue::Null
            }
        }
        JqStep::Function(func_name, arg) => {
            if func_name == "select" {
                apply_select_function(arg, json_value)
            } else {
                JsonValue::Null
            }
        }
    }
}

// 应用select函数
fn apply_select_function(condition: &str, json_value: &JsonValue) -> JsonValue {
    // 简化版条件解析:只支持数字比较
    let re =
        SELECT_RE.get_or_init(|| Regex::new(r"\.(\w+)\s*(>=|<=|==|!=|>|<|=)\s*(\d+)").unwrap());
    if let Some(caps) = re.captures(condition) {
        let field = caps.get(1).unwrap().as_str();
        let op = caps.get(2).unwrap().as_str();
        let value: i64 = caps.get(3).unwrap().as_str().parse().unwrap();

        if let JsonValue::Array(arr) = json_value {
            let filtered: Vec<JsonValue> = arr
                .iter()
                .filter(|item| {
                    if let JsonValue::Object(obj) = item {
                        if let Some(JsonValue::Number(n)) = obj.get(field) {
                            let n_int = *n as i64;
                            match op {
                                ">" => n_int > value,
                                "<" => n_int < value,
                                ">=" => n_int >= value,
                                "<=" => n_int <= value,
                                "==" | "=" => n_int == value,
                                "!=" => n_int != value,
                                _ => false,
                            }
                        } else {
                            false
                        }
                    } else {
                        false
                    }
                })
                .cloned()
                .collect();

            JsonValue::Array(filtered)
        } else {
            JsonValue::Null
        }
    } else {
        JsonValue::Null
    }
}