Lumesh Parse Module

wiki libs

Lumesh Parsing Functions Documentation


Function Overview

Function Category Function Name Call Format Description
Data Parsing json parse.json json_str Parse a JSON string into Lumesh data structure
Data Parsing toml parse.toml toml_str Parse a TOML string into Lumesh data structure
Data Parsing csv parse.csv csv_str Parse a CSV string into Lumesh data structure
Code Parsing expr parse.expr lumesh_code Parse a Lumesh code string into an executable expression
Command Parsing cmd parse.cmd [headers...] output Parse command line output into structured data
Data Serialization to_json parse.to_json expr Serialize Lumesh data structure into a JSON string
Data Serialization to_toml parse.to_toml expr Serialize Lumesh data structure into a TOML string
Data Serialization to_csv parse.to_csv expr Serialize Lumesh data structure into a CSV string
JSON Query jq parse.jq json_str Perform common JSON queries, supporting pipes, attributes, indices, and select

Detailed Descriptions and Examples

1. parse.toml

Parameters:

  • toml_str: String type, containing valid TOML content

Output Example:

parse.toml "name = 'Alice'\nage = 30"

# Returns:
+---------------------+
| KEY VALUE |
+=====================+
| name "Alice" |
| age 30 |
+---------------------+

To convert back to a string, use parse.to_toml on the expression.


2. parse.json

Parameters:

  • json_str: String type, containing valid JSON content

Nested Structure Output:

parse.json '{"name":"Alice","hobbies":["reading","swimming"]}'
# or
parse.json "{\"name\":\"Alice\",\"hobbies\":[\"reading\",\"swimming\"]}"

# Returns:
+---------------------------------+
| KEY VALUE |
+=================================+
| age 30 |
| hobbies ┌─────────┬──────────┐ |
| │ reading │ swimming │ |
| └─────────┴──────────┘ |
| name "Alice" |
+---------------------------------+

To convert back to a string, use parse.to_json on the expression.


3. parse.csv

Parameters:

  • csv_str: String type, containing valid CSV content

Nested Structure Output:

parse.csv "Product Name,Category,Price,Stock
Laptop,Electronics,999.99,50
Smartphone,Electronics,499.99,150
Desk Chair,Furniture,89.99,75
Coffee Maker,Appliances,29.99,200
Notebook,Stationery,2.99,500
"

# Output
+------------------------------------------+
| CATEGORY PRICE PRODUCT NAME STOCK |
+==========================================+
| Electronics 999.99 Laptop 50 |
| Electronics 499.99 Smartphone 150 |
| Furniture 89.99 Desk Chair 75 |
| Appliances 29.99 Coffee Maker 200 |
| Stationery 2.99 Notebook 500 |
+------------------------------------------+

To convert back to a string, use parse.to_csv on the expression.


4. parse.expr

Parameters:

  • lumesh_code: String type, containing valid Lumesh syntax

Dynamic Code Execution:

let code = "x -> math.max(x, 5)"
let action = parse.expr(code)
action(2) #=> 5

5. parse.cmd

Parameter Modes:

Mode Example Description
Auto Detection parse.cmd output Smartly detects headers and data rows
List Headers parse.cmd [h1, h2] output Specify a list of headers
Multiple Parameters parse.cmd h1 h2 ... output Directly specify multiple headers

Table Output Example:

parse.cmd(["ID", "NAME", "AGE"], "1 Alice 30\n2 Bob 25")

# Returns:
+------------------------------+
| ID NAME AGE |
+==============================+
| 1 "Alice" 30 |
| 2 "Bob" 25 |
+------------------------------+

Complex Structure Output:

parse.cmd "ITEM  QTY  PRICE
Apple 10 2.5
Banana 5 1.8"

# Returns:
+---------------------------------+
| ITEM QTY PRICE |
+=================================+
| Apple 10 2.5 |
| Banana 5 1.8 |
+---------------------------------+

6. parse.jq

Parameters:

  • query_str: String type, containing valid JSON queries, supporting pipes, attributes, indices, and select
    Pipe: |
    Attribute Access: .name
    Wildcard: .[] returns the entire array
    Select statements support basic comparison operators: >, <, >=, <=, ==, !=
  • json_str: String type, containing valid JSON content
let a = '[{"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}]'

parse.jq '[0]|.name' a # Output: "Alice"
parse.jq 'select(.age>28)' a # Output: [{"name":"Alice","age":30}]

Special Case Handling

Pre-structured Data Recognition

parse.cmd "1, 2, 3"
# Directly returns a list:
+-------------+
| C0 C1 C2 |
+=============+
| 1, 2, 3 |
+-------------+

Header Auto-conversion

parse.cmd "CPU%  MEM(MB) MAJ:MIN\n50    1024   259:0"
# After header conversion:
+-----------------------+
| CPU% MAJ_MIN MEM_MB |
+=======================+
| 50 259:0 1024 |
+-----------------------+

Empty Input Handling

parse.toml ""   ; => none
parse.cmd "" ; => [] (empty list)

# Output:
+------------+
| KEY VALUE |
+============+
+------------+

Best Practices

  1. parse.cmd is generally used to convert the results of system commands from strings to structured tables.
ps aux | head -n4 | parse.cmd()

# Output:
+-------------------------------------------------------------------------------------+
| %CPU %MEM COMMAND PID RSS START STAT TIME TTY USER VSZ |
+=====================================================================================+
| 0.0 0.0 /sbin/init 1 1932 09:05 S 0:00 ? root 2608 |
| 0.0 0.0 [kthreadd] 2 0 09:05 S 0:00 ? root 0 |
| 0.0 0.0 [pool_workqueue_release] 3 0 09:05 S 0:00 ? root 0 |
+-------------------------------------------------------------------------------------+
  1. parse.cmd is often used in conjunction with where and select statements.
ps aux | parse.cmd() | where(COMMAND ~: "lume") | select "%CPU" COMMAND RSS

# Output:
+----------------------------------+
| %CPU COMMAND RSS |
+==================================+
| 0.0 ./target/debug/lume 10476 |
| 0.0 ./target/debug/lume 12148 |
+----------------------------------+
  1. Lumesh’s built-in module commands do not require parse.cmd conversion and can directly use where and select.
fs.ls -l -k | where(size > 8)

# Output
+---------------------------------------------------------------+
| MODE MODIFIED NAME SIZE TYPE |
+===============================================================+
| 420 2025-06-01 06:12:18.911115732 Cargo.lock 47 file |
| 420 2025-05-30 10:26:40.122231239 CHANGELOG.md 10 file |
+---------------------------------------------------------------+