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
use crate::expression::table::TableData;
use crate::{Environment, Int, libs};
use std::cmp::Ordering;
use std::collections::{BTreeMap, BTreeSet, HashMap};
use std::ops::Range;
use std::rc::Rc;
pub mod alias;
pub mod basic;
pub mod catcher;
pub mod cmd_excutor;
pub mod eval;
pub mod eval2;
pub mod eval3;
pub mod from;
pub mod overop;
pub mod pty;
pub mod table;
pub mod terminal;

use chrono::NaiveDateTime;
use regex_lite::Regex;
#[derive(Clone, PartialEq)]
pub enum Expression {
    // 所有嵌套节点改为Rc包裹
    Group(Rc<Self>),
    BinaryOp(String, Rc<Self>, Rc<Self>),
    UnaryOp(String, Rc<Self>, bool),
    RangeOp(String, Rc<Self>, Rc<Self>, Option<Rc<Self>>),
    Pipe(String, Rc<Self>, Rc<Self>),

    // 基础类型保持原样
    Symbol(String),
    Variable(String),
    Integer(Int),
    Float(f64),
    Bytes(Vec<u8>), // 这个保持值类型,因为Rc<Vec>反而增加复杂度
    String(String),
    StringTemplate(Vec<Self>),
    StringSafe(String),
    RegexDef(String),
    TimeDef(String),
    Regex(LumeRegex),
    Boolean(bool),
    None,

    // 集合类型使用Rc
    List(Rc<Vec<Self>>),
    BSet(Rc<BTreeSet<Self>>),
    HMap(Rc<HashMap<String, Self>>),
    Map(Rc<BTreeMap<String, Self>>),

    // 索引和切片优化
    Index(Rc<Self>, Rc<Self>),
    Property(Rc<Self>, Rc<Self>),

    // 其他变体保持不变
    Del(String),
    Declare(String, Rc<Self>),
    Assign(String, Rc<Self>),
    SetParent(String, Rc<Expression>), // 全局变量设置
    Export(String, Option<Rc<Self>>),  // 导出
    AliasDef(String, Rc<Self>),
    For(String, Option<String>, Rc<Self>, Rc<Self>),
    While(Rc<Self>, Rc<Self>),
    Loop(Rc<Self>),
    Match(Rc<Self>, Rc<Vec<(Vec<Self>, Self)>>),
    If(Rc<Self>, Rc<Self>, Rc<Self>),
    Apply(Rc<Self>, Rc<Vec<Self>>),
    Command(Rc<Self>, Rc<Vec<Self>>),
    SymbolRaw(String),
    Lambda(Vec<String>, Rc<Self>, Option<HashMap<String, Self>>),
    Function(
        String,
        Vec<(String, Option<Self>)>,
        Option<String>,
        Rc<Self>,
        Vec<(String, Option<Vec<Expression>>)>,
    ),
    Return(Rc<Self>),
    Break(Rc<Self>),
    Continue,
    Sequence(Vec<Expression>), // 表达式序列
    Block(Rc<Vec<Self>>),
    Quote(Rc<Self>),
    Catch(Rc<Self>, CatchType, Option<Rc<Self>>),
    Range(Range<Int>, usize),
    DateTime(NaiveDateTime),
    FileSize(FileSize),
    Table(TableData),
    Chain(Rc<Expression>, Vec<ChainCall>), // 链式调用
    PipeMethod(String, Rc<Vec<Self>>),
    DestructureAssign(Vec<DestructurePattern>, Rc<Expression>), // 解构赋值
    Use(Option<String>, String),
    ModuleCall(Vec<String>, Rc<Self>), //模块调用
    Blank,
    Shift,
}

#[derive(Debug, Clone)]
pub struct LumeRegex {
    pub regex: Regex,
}
impl PartialEq for LumeRegex {
    fn eq(&self, other: &LumeRegex) -> bool {
        self.regex.as_str() == other.regex.as_str()
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct ChainCall {
    pub method: String,
    pub args: Vec<Expression>,
}

#[derive(Debug, Clone, PartialEq)]
pub enum DestructurePattern {
    Identifier(String),
    Renamed((String, String)),
    Rest(String), // ...rest 语法
                  // Nested(Box<Expression>),          // 嵌套解构
                  // Default(String, Box<Expression>), // 默认值
}

#[derive(Debug, Clone, PartialEq)]
pub enum SizeUnit {
    B,
    K,
    M,
    G,
    T,
    P,
    None,
}
impl SizeUnit {
    pub fn from_str(unit: &str) -> Self {
        match unit.to_uppercase().as_str() {
            "B" | "" => SizeUnit::B,
            "K" | "KB" => SizeUnit::K,
            "M" | "MB" => SizeUnit::M,
            "G" | "GB" => SizeUnit::G,
            "T" | "TB" => SizeUnit::T,
            "P" | "PB" => SizeUnit::P,
            _ => SizeUnit::B,
        }
    }
}

#[derive(Debug, Clone)]
pub struct FileSize {
    size: u64, // 文件大小,以字节为单位
    unit: SizeUnit,
}
impl FileSize {
    #[inline]
    pub fn new(size: u64, unit: SizeUnit) -> Self {
        Self { size, unit }
    }
    #[inline]
    pub fn from(size: u64, unit_str: &str) -> Self {
        Self {
            size,
            unit: SizeUnit::from_str(unit_str),
        }
    }
    pub fn from_float(size: f64, unit_str: &str) -> Self {
        let shift: u32 = match SizeUnit::from_str(unit_str) {
            SizeUnit::None | SizeUnit::B => 0,
            SizeUnit::K => 10,
            SizeUnit::M => 20,
            SizeUnit::G => 30,
            SizeUnit::T => 40,
            SizeUnit::P => 50,
        };
        // 直接转换为字节数存储,避免截断精度
        Self {
            size: (size * (1u64 << shift) as f64) as u64,
            unit: SizeUnit::B,
        }
    }
    #[inline]
    pub fn from_bytes(size: u64) -> Self {
        Self {
            size,
            unit: SizeUnit::B,
        }
    }

    #[inline]
    pub fn to_bytes(&self) -> u64 {
        let mut size = self.size;
        // 根据单位进行转换
        size <<= match &self.unit {
            SizeUnit::None | SizeUnit::B => 0,
            SizeUnit::K => 10,
            SizeUnit::M => 20,
            SizeUnit::G => 30,
            SizeUnit::T => 40,
            SizeUnit::P => 50,
        };
        size
    }
    pub fn to_human_readable(&self) -> String {
        let size = self.to_bytes();
        // 定义单位基数
        const KB: u64 = 1 << 10;
        const MB: u64 = 1 << 20;
        const GB: u64 = 1 << 30;
        const TB: u64 = 1 << 40;
        const PB: u64 = 1 << 50;
        let units = [
            ("P", PB, 50),
            ("T", TB, 40),
            ("G", GB, 30),
            ("M", MB, 20),
            ("K", KB, 10),
            ("B", 0, 0),
        ];

        // 查找合适的单位
        let (unit_str, _, shift) = units
            .iter()
            .find(|(_, base, _)| size >= *base)
            .unwrap_or(units.last().unwrap());
        let scaled_size = size >> shift;

        // 格式化输出
        if *unit_str == "B" {
            format!("{scaled_size}")
        } else if *unit_str == "K" {
            format!("{scaled_size}K")
        } else {
            let frac_size = (size >> (shift - 10)) & 1023; // 计算小数部分
            format!(
                "{:.2}{}",
                scaled_size as f64 + frac_size as f64 * 0.0009765625,
                unit_str
            )
        }
    }
}
impl PartialOrd for FileSize {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        self.to_bytes().partial_cmp(&other.to_bytes())
    }
}
impl PartialEq for FileSize {
    fn eq(&self, other: &Self) -> bool {
        self.to_bytes().eq(&other.to_bytes())
    }
}

#[derive(Debug, Clone, PartialEq)]
pub enum CatchType {
    Ignore,
    PrintStd,
    PrintErr,
    PrintOver,
    TerminateOnErr,
    OnError,
    ToBoolean,
    OnSuccess,
    OnEmpty,
    TerminateOnEmpty,
}

impl PartialOrd for Expression {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        // ===== 字符串之间比较 =====
        // 先拦截所有“文本类”变体之间的比较,替代原来十几条两两分支
        if let (Some(a), Some(b)) = (self.as_str(), other.as_str()) {
            return a.partial_cmp(b);
        }

        match (self, other) {
            // ===== 同类型简单比较 =====
            (Self::None, Self::None) => Some(Ordering::Equal),
            (Self::Blank, Self::Blank) => Some(Ordering::Equal),
            // (Self::String(a), Self::String(b)) => a.partial_cmp(b),
            (Self::Bytes(a), Self::Bytes(b)) => a.partial_cmp(b),
            (Self::Boolean(a), Self::Boolean(b)) => a.partial_cmp(b),
            (Self::DateTime(a), Self::DateTime(b)) => a.partial_cmp(b),
            (Self::FileSize(a), Self::FileSize(b)) => a.partial_cmp(b),
            (Self::Table(a), Self::Table(b)) => a.rows().partial_cmp(b.rows()),

            // ===== 数值类型互比 =====
            (Self::Integer(a), Self::Integer(b)) => a.partial_cmp(b),
            (Self::Float(a), Self::Float(b)) => a.partial_cmp(b),
            (Self::Float(a), Self::Integer(b)) => a.partial_cmp(&(*b as f64)),
            (Self::Integer(a), Self::Float(b)) => (&(*a as f64)).partial_cmp(b),
            (Self::Range(r, s), Self::Range(r2, s2)) => {
                match (r.start.partial_cmp(&r2.start), r.end.partial_cmp(&r2.end)) {
                    // 首尾相同,step越大的,越小
                    (Some(Ordering::Equal), Some(Ordering::Equal)) => s2.partial_cmp(s),
                    (Some(Ordering::Equal), other_end) => other_end,
                    // 尾部相同,首部越大的,越小
                    (other_start, Some(Ordering::Equal)) => other_start.map(|o| o.reverse()),
                    // 首尾都不相同的,按首部比较
                    (other_start, _) => other_start,
                }
            }
            // ===== 字符串与数字互比 =====
            (Self::String(a), Self::Integer(b)) => {
                if let Ok(ai) = a.parse::<i64>() {
                    ai.partial_cmp(b)
                } else {
                    a.parse::<f64>().ok()?.partial_cmp(&(*b as f64))
                }
            }
            (Self::Integer(a), Self::String(b)) => {
                if let Ok(bi) = b.parse::<i64>() {
                    a.partial_cmp(&bi)
                } else {
                    (*a as f64).partial_cmp(&b.parse::<f64>().ok()?)
                }
            }
            (Self::String(a), Self::Float(b)) => a.parse::<f64>().ok()?.partial_cmp(b),
            (Self::Float(a), Self::String(b)) => a.partial_cmp(&b.parse::<f64>().ok()?),

            // bytes
            (Self::Bytes(a), Self::String(b)) => a.as_slice().partial_cmp(b.as_bytes()),
            (Self::String(a), Self::Bytes(b)) => a.as_bytes().partial_cmp(b.as_slice()),

            // 文件大小与数值比较
            (Self::FileSize(a), Self::String(b)) => {
                a.partial_cmp(&libs::from_size_str(b, other).ok()?)
            }
            (Self::String(a), Self::FileSize(b)) => {
                libs::from_size_str(a, self).ok()?.partial_cmp(b)
            }
            (Self::FileSize(a), Self::Integer(b)) => {
                Some(if *b < 0 {
                    Ordering::Greater // u64 >= 0 > i64 负数
                } else {
                    a.to_bytes().cmp(&(*b as u64))
                })
            }
            (Self::Integer(a), Self::FileSize(b)) => {
                Some(if *a < 0 {
                    Ordering::Less // u64 >= 0 > i64 负数
                } else {
                    (*a as u64).cmp(&b.to_bytes())
                })
            }
            (Self::FileSize(a), Self::Float(b)) => {
                if *b < 0.0 {
                    Some(Ordering::Greater)
                } else {
                    (a.to_bytes() as f64).partial_cmp(b)
                }
            }
            (Self::Float(a), Self::FileSize(b)) => {
                if *a < 0.0 {
                    Some(Ordering::Less)
                } else {
                    a.partial_cmp(&(b.to_bytes() as f64))
                }
            }

            // 时间
            (Self::DateTime(a), Self::Integer(b)) => a.and_utc().timestamp().partial_cmp(b),
            (Self::Integer(a), Self::DateTime(b)) => a.partial_cmp(&b.and_utc().timestamp()),
            (Self::DateTime(a), Self::String(_)) => {
                a.partial_cmp(&libs::from_time_str(other.clone(), other).ok()?)
            }

            (Self::String(_), Self::DateTime(b)) => {
                libs::from_time_str(self.clone(), self).ok()?.partial_cmp(b)
            }
            // ===== 集合类型 =====
            (Self::List(a), Self::List(b)) => a.as_slice().partial_cmp(b.as_slice()),

            (Self::BSet(a), Self::BSet(b)) => a.partial_cmp(b),

            (Self::Map(a), Self::Map(b)) => a.partial_cmp(b),
            (Self::HMap(a), Self::HMap(b)) => {
                let mut keys: Vec<_> = a.keys().chain(b.keys()).collect();
                keys.sort();
                keys.dedup();
                for k in keys {
                    let cmp = a.get(k).partial_cmp(&b.get(k));
                    if cmp != Some(Ordering::Equal) {
                        return cmp;
                    }
                }
                Some(Ordering::Equal)
            }

            // ===== 其他种类不比较 =====
            _ => None,
        }
    }
}

impl Ord for Expression {
    fn cmp(&self, other: &Self) -> Ordering {
        self.partial_cmp(other)
            .unwrap_or_else(|| self.type_name().cmp(&other.type_name()))
    }
}

impl Eq for Expression {}

pub enum BoxedIterator {
    Range(std::iter::StepBy<std::ops::Range<Int>>),
    Vec(std::vec::IntoIter<Expression>),
    Map(std::collections::hash_map::IntoIter<String, Expression>),
    MapEntries(std::vec::IntoIter<Expression>),
    // Other(Box<dyn Iterator<Item=Expression>>)
}

impl Iterator for BoxedIterator {
    type Item = Expression;

    fn next(&mut self) -> Option<Self::Item> {
        match self {
            Self::Range(iter) => iter.next().map(Expression::Integer),
            Self::Vec(iter) => iter.next(),
            Self::Map(iter) => iter
                .next()
                .map(|(k, v)| Expression::from(vec![Expression::String(k), v])),
            Self::MapEntries(iter) => iter.next(),
        }
    }
}