graphersal 0.1.0

In-memory graph traversal engine modelled on Apache TinkerPop Gremlin, with a Rhai DSL, optimizer and GraphSON/GraphML I/O
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
#[cfg(feature = "display")]
use std::collections::BTreeSet;

use crate::error::TraverserResult;
use crate::graph::schema::visualize::VisualizeFormat;
use crate::traversal::render::{CUT_MARKER, ItemLimit, RenderOptions, cut_nested};
use crate::value::ElementProperty;

/// Formats a single `ElementProperty` value for a table cell.
pub fn format_table_cell_value(prop: &ElementProperty) -> String {
    format_cell(prop, &ItemLimit::unlimited())
}

/// Formats a table cell, cutting every nested collection to the item limit and marking the cut
/// with `…` (for example `[1, 2, 3, …]`).
pub(crate) fn format_cell(prop: &ElementProperty, limit: &ItemLimit) -> String {
    match prop {
        ElementProperty::Null => "-".to_string(),
        ElementProperty::String(s) => s.to_string(),
        ElementProperty::Compressed(text) => text.text().into_string(),
        ElementProperty::Number(n) => format!("{}", n),
        ElementProperty::Boolean(b) => {
            if *b {
                "true".to_string()
            } else {
                "false".to_string()
            }
        }
        ElementProperty::Uuid(u) => crate::value::uuid::format_canonical(*u).to_string(),
        ElementProperty::Array(arr) => {
            let keep = limit.keep(arr.len());
            let mut items: Vec<String> = arr
                .iter()
                .take(keep)
                .map(|item| format_cell(item, limit))
                .collect();
            if keep < arr.len() {
                items.push(CUT_MARKER.to_string());
            }
            format!("[{}]", items.join(", "))
        }
        ElementProperty::Object(obj) => {
            let keep = limit.keep(obj.len());
            let mut items: Vec<String> = obj
                .iter()
                .take(keep)
                .map(|(k, v)| format!("{}: {}", k, format_cell(v, limit)))
                .collect();
            if keep < obj.len() {
                items.push(CUT_MARKER.to_string());
            }
            format!("{{{}}}", items.join(", "))
        }
        ElementProperty::Map(map) => {
            let keep = limit.keep(map.len());
            let mut items: Vec<String> = map
                .iter()
                .take(keep)
                .map(|(k, v)| {
                    format!(
                        "{}: {}",
                        crate::value::physical::to_display_text(k),
                        format_cell(v, limit)
                    )
                })
                .collect();
            if keep < map.len() {
                items.push(CUT_MARKER.to_string());
            }
            format!("{{{}}}", items.join(", "))
        }
        ElementProperty::Vertex(v) => {
            let id = v
                .get("id")
                .map(format_table_cell_value)
                .unwrap_or_else(|| "-".to_string());
            format!("v[{}]", id)
        }
        ElementProperty::Edge(e) => {
            let id = e
                .get("id")
                .map(format_table_cell_value)
                .unwrap_or_else(|| "-".to_string());
            let from = e
                .get("_from")
                .map(format_table_cell_value)
                .unwrap_or_else(|| "-".to_string());
            let label = e
                .get("label")
                .map(format_table_cell_value)
                .unwrap_or_else(|| "-".to_string());
            let to = e
                .get("_to")
                .map(format_table_cell_value)
                .unwrap_or_else(|| "-".to_string());
            format!("e[{}][{}-{}->{}]", id, from, label, to)
        }
    }
}

/// Resolves the final headers to display based on discovered columns and user options.
///
/// Without an explicit inclusion list, more than `max_items` columns (a `group()` row with 100k
/// keys) are cut to `max_items`, followed by a `…` column.
#[cfg(feature = "display")]
fn resolve_columns(
    discovered: Vec<String>,
    options: &RenderOptions,
    limit: &ItemLimit,
) -> Vec<String> {
    let mut explicit = false;
    let mut cols = match &options.columns {
        None => discovered,
        Some(col_specs) => {
            if col_specs.is_empty() {
                discovered
            } else {
                let mut exclusions = BTreeSet::new();
                let mut inclusions = Vec::new();

                for spec in col_specs {
                    let trimmed = spec.trim();
                    if let Some(stripped) = trimmed
                        .strip_prefix('!')
                        .or_else(|| trimmed.strip_prefix('-'))
                    {
                        exclusions.insert(stripped.trim().to_string());
                    } else {
                        inclusions.push(trimmed.to_string());
                    }
                }

                if inclusions.is_empty() {
                    // Only exclusions were specified -> filter discovered columns
                    discovered
                        .into_iter()
                        .filter(|col| !exclusions.contains(col))
                        .collect()
                } else {
                    // Explicit inclusion list -> filter out any explicit exclusions
                    explicit = true;
                    inclusions
                        .into_iter()
                        .filter(|col| !exclusions.contains(col))
                        .collect()
                }
            }
        }
    };

    if !explicit {
        let keep = limit.keep(cols.len());
        if keep < cols.len() {
            cols.truncate(keep);
            cols.push(CUT_MARKER.to_string());
        }
    }

    if options.row_numbers && !cols.iter().any(|c| c == "#") {
        cols.insert(0, "#".to_string());
    }

    cols
}

/// The cells of a map row: each entry's column header (the key's display text) and value.
///
/// Two keys of one map can print the same text (`"29"` and `29`). They stay separate cells: the
/// string key's header is then shown quoted (`"29"`), so no entry is lost.
#[cfg(feature = "display")]
fn map_cells(map: &crate::value::ValueMap) -> Vec<(String, &ElementProperty)> {
    let shown: Vec<String> = map
        .keys()
        .map(|key| crate::value::physical::to_display_text(key).to_string())
        .collect();
    map.iter()
        .zip(shown.iter())
        .map(|((key, value), text)| {
            let collides = shown.iter().filter(|other| *other == text).count() > 1;
            match (key, collides) {
                (ElementProperty::String(_), true) => (format!("\"{text}\""), value),
                _ => (text.clone(), value),
            }
        })
        .collect()
}

/// Discovers the ordered column schema for a slice of `ElementProperty` items.
#[cfg(feature = "display")]
fn discover_columns(elements: &[ElementProperty]) -> Vec<String> {
    if elements.is_empty() {
        return Vec::new();
    }

    // Check kind of elements
    let is_vertex = elements
        .iter()
        .any(|e| matches!(e, ElementProperty::Vertex(_)));
    let is_edge = elements
        .iter()
        .any(|e| matches!(e, ElementProperty::Edge(_)));
    let is_object = elements
        .iter()
        .any(|e| matches!(e, ElementProperty::Object(_)));
    let is_map = elements
        .iter()
        .any(|e| matches!(e, ElementProperty::Map(_)));

    if is_vertex {
        let mut fixed_headers = vec!["id".to_string(), "label".to_string()];
        let mut prop_keys = BTreeSet::new();

        for el in elements {
            if let ElementProperty::Vertex(v) = el {
                for key in v.keys() {
                    if key != "id" && key != "label" {
                        prop_keys.insert(key.to_string());
                    }
                }
            }
        }

        fixed_headers.extend(prop_keys);
        fixed_headers
    } else if is_edge {
        let mut fixed_headers = vec![
            "id".to_string(),
            "label".to_string(),
            "_from".to_string(),
            "_to".to_string(),
        ];
        let mut prop_keys = BTreeSet::new();

        for el in elements {
            if let ElementProperty::Edge(e) = el {
                for key in e.keys() {
                    if key != "id" && key != "label" && key != "_from" && key != "_to" {
                        prop_keys.insert(key.to_string());
                    }
                }
            }
        }

        fixed_headers.extend(prop_keys);
        fixed_headers
    } else if is_object {
        let mut prop_keys = BTreeSet::new();
        for el in elements {
            if let ElementProperty::Object(obj) = el {
                for key in obj.keys() {
                    prop_keys.insert(key.to_string());
                }
            }
        }
        prop_keys.into_iter().collect()
    } else if is_map {
        // A map with a non-string key: one column per key, headed by the key's display text, in the
        // order the keys first appear. Rows are never merged by position.
        let mut columns: Vec<String> = Vec::new();
        for el in elements {
            if let ElementProperty::Map(map) = el {
                for (header, _) in map_cells(map) {
                    if !columns.contains(&header) {
                        columns.push(header);
                    }
                }
            }
        }
        columns
    } else {
        // Scalar value
        vec!["value".to_string()]
    }
}

/// Extracts a cell string value for a given row element and column header.
#[cfg(feature = "display")]
fn extract_cell_value(
    row_idx: usize,
    col: &str,
    element: &ElementProperty,
    limit: &ItemLimit,
) -> String {
    if col == "#" {
        return (row_idx + 1).to_string();
    }
    if col == CUT_MARKER {
        return CUT_MARKER.to_string();
    }

    match element {
        ElementProperty::Vertex(v) => {
            if let Some(val) = v.get(col) {
                format_cell(val, limit)
            } else {
                "-".to_string()
            }
        }
        ElementProperty::Edge(e) => {
            if let Some(val) = e.get(col) {
                format_cell(val, limit)
            } else {
                "-".to_string()
            }
        }
        ElementProperty::Object(obj) => {
            if let Some(val) = obj.get(col) {
                format_cell(val, limit)
            } else {
                "-".to_string()
            }
        }
        ElementProperty::Map(map) => map_cells(map)
            .into_iter()
            .find(|(header, _)| header == col)
            .map_or_else(|| "-".to_string(), |(_, val)| format_cell(val, limit)),
        scalar => {
            if col == "value" {
                format_cell(scalar, limit)
            } else {
                "-".to_string()
            }
        }
    }
}

/// Formats traversal elements as a table or markdown according to options.
///
/// The caller has already cut the rows to `max_rows`; nested collections are cut to `limit`
/// here, with a `…` marker in the cell.
pub(crate) fn format_elements_as_table(
    elements: &[ElementProperty],
    format: VisualizeFormat,
    options: &RenderOptions,
    limit: &ItemLimit,
) -> TraverserResult<String> {
    if elements.is_empty() {
        return Ok(String::new());
    }

    #[cfg(feature = "display")]
    {
        use tabled::builder::Builder;
        use tabled::settings::Modify;
        use tabled::settings::Style;
        use tabled::settings::Width;
        use tabled::settings::object::Segment;

        let discovered = discover_columns(elements);
        let columns = resolve_columns(discovered, options, limit);

        if columns.is_empty() {
            return Ok(String::new());
        }

        let mut builder = Builder::default();
        builder.push_record(columns.iter().map(|c| c.as_str()));

        for (idx, el) in elements.iter().enumerate() {
            let row: Vec<String> = columns
                .iter()
                .map(|col| extract_cell_value(idx, col, el, limit))
                .collect();
            builder.push_record(row);
        }

        let mut table = builder.build();

        match format {
            VisualizeFormat::Markdown => {
                table.with(Style::markdown());
            }
            _ => {
                table.with(Style::rounded());
            }
        }

        if let Some(max_width) = options.max_width {
            table.with(Modify::new(Segment::all()).with(Width::wrap(max_width)));
        }

        if let Some(truncate) = options.truncate {
            table.with(Modify::new(Segment::all()).with(Width::truncate(truncate).suffix("...")));
        }

        Ok(table.to_string())
    }

    #[cfg(not(feature = "display"))]
    {
        let _ = (format, options);
        Ok(format_elements_as_lines(elements, limit))
    }
}

/// Renders one element per line as compact JSON text, for output without a table renderer.
///
/// Human-facing, not an exchange format: a value holding a non-finite float, which JSON cannot
/// represent, is printed as that float (`NaN`, `inf`, `-inf`) instead of failing the output.
/// Nested collections are cut to `limit` without a marker, as in JSON output.
pub(crate) fn format_elements_as_lines(elements: &[ElementProperty], limit: &ItemLimit) -> String {
    let mut buf = String::new();
    for (i, el) in elements.iter().enumerate() {
        if i > 0 {
            buf.push('\n');
        }
        let cut;
        let el = if limit.is_limited() {
            cut = cut_nested(el, limit);
            &cut
        } else {
            el
        };
        match crate::value::physical::to_json_text(el) {
            Ok(text) => buf.push_str(&text),
            Err(crate::value::physical::PhysicalError::NonFiniteFloat(f)) => {
                buf.push_str(&f.to_string())
            }
            Err(err) => buf.push_str(&err.to_string()),
        }
    }
    buf
}