mnml-rs 0.2.14

A NvChad-style terminal IDE in Rust — vim or standard editing, LSP, git, and an embedded HTTP client.
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
//! Lookup picker — fills env vars from real API responses.
//!
//! Workflow (Ctrl+; in the TUI):
//!   1. PickingFile  pick a saved lookup file under <ws>/.rqst/lookups/
//!   2. Loading      fire the lookup curl, parse the JSON response
//!   3. PickingItem  pick an item rendered as "label (id)"
//!   4. EnteringVar  type the env var name to receive the id
//!
//! Item parsing heuristic — assumes a list-shaped response:
//!   bare array               → use it
//!   { "data":   [...] }      → use it
//!   { "items":  [...] }      → use it
//!   { <single-key>: [...] }  → use it
//! For each entry, the id field is the first of:
//!   `id`, `Id`, `<*>Id` (camelCase), `_id`
//! and the label is the first of:
//!   `name`, `displayName`, `label`, `title`, `label`, otherwise the id.

use serde_json::Value;
use std::path::{Path, PathBuf};

const SKIP_DIRS: &[&str] = &["target", "node_modules"];

#[derive(Debug, Clone, PartialEq)]
pub enum Stage {
    PickingFile,
    Loading,
    PickingItem,
    EnteringVar,
}

#[derive(Debug, Clone)]
pub struct LookupItem {
    pub id: String,
    pub label: String,
}

#[derive(Debug)]
pub struct LookupPicker {
    pub stage: Stage,
    pub workspace: PathBuf,

    // Stage 1 state
    pub files: Vec<PathBuf>,
    pub file_filter: String,
    pub file_cursor: usize,

    // Stage 2 state — set when transitioning to Loading
    pub loading_file: Option<PathBuf>,

    // Stage 3 state — populated when items land
    pub items: Vec<LookupItem>,
    pub item_filter: String,
    pub item_cursor: usize,

    // Stage 4 state
    pub picked: Option<LookupItem>,
    pub var_name_input: String,
    pub var_name_suggestion: Option<String>,
}

impl LookupPicker {
    pub fn open(workspace: &Path) -> Self {
        let files = scan_lookups(workspace);
        Self {
            stage: Stage::PickingFile,
            workspace: workspace.to_path_buf(),
            files,
            file_filter: String::new(),
            file_cursor: 0,
            loading_file: None,
            items: Vec::new(),
            item_filter: String::new(),
            item_cursor: 0,
            picked: None,
            var_name_input: String::new(),
            var_name_suggestion: None,
        }
    }

    pub fn filtered_files(&self) -> Vec<usize> {
        let q = self.file_filter.to_lowercase();
        self.files
            .iter()
            .enumerate()
            .filter(|(_, p)| {
                let rel = relative_label(p, &self.workspace);
                rel.to_lowercase().contains(&q)
            })
            .map(|(i, _)| i)
            .collect()
    }

    pub fn filtered_items(&self) -> Vec<usize> {
        let q = self.item_filter.to_lowercase();
        self.items
            .iter()
            .enumerate()
            .filter(|(_, it)| {
                it.label.to_lowercase().contains(&q) || it.id.to_lowercase().contains(&q)
            })
            .map(|(i, _)| i)
            .collect()
    }

    pub fn selected_file(&self) -> Option<&PathBuf> {
        let filtered = self.filtered_files();
        filtered
            .get(self.file_cursor)
            .and_then(|&i| self.files.get(i))
    }

    pub fn selected_item(&self) -> Option<&LookupItem> {
        let filtered = self.filtered_items();
        filtered
            .get(self.item_cursor)
            .and_then(|&i| self.items.get(i))
    }
}

/// Recursive `.curl` / `.http` / `.rest` scan under
/// `<workspace>/.rqst/lookups`. http-2nd 2026-06-28 SEV-3a — the
/// app-side `http_lookup_open` used to do a flat read_dir and
/// accept three extensions; now both code paths agree (recursive
/// + all three extensions).
pub fn scan_lookups(workspace: &Path) -> Vec<PathBuf> {
    let lookup_dir = workspace.join(".rqst").join("lookups");
    if !lookup_dir.is_dir() {
        return Vec::new();
    }
    let mut out: Vec<PathBuf> = Vec::new();
    let mut stack: Vec<PathBuf> = vec![lookup_dir];
    while let Some(dir) = stack.pop() {
        let Ok(entries) = std::fs::read_dir(&dir) else {
            continue;
        };
        for entry in entries.flatten() {
            let path = entry.path();
            let name = entry.file_name();
            let name_str = name.to_string_lossy();
            if name_str.starts_with('.') || SKIP_DIRS.iter().any(|s| s == &name_str) {
                continue;
            }
            let is_dir = entry.file_type().map(|t| t.is_dir()).unwrap_or(false);
            if is_dir {
                stack.push(path);
            } else if path
                .extension()
                .and_then(|e| e.to_str())
                .is_some_and(|e| matches!(e, "curl" | "http" | "rest"))
            {
                out.push(path);
            }
        }
    }
    out.sort();
    out
}

pub fn relative_label(path: &Path, workspace: &Path) -> String {
    path.strip_prefix(workspace.join(".rqst").join("lookups"))
        .unwrap_or(path)
        .to_string_lossy()
        .into_owned()
}

/// Parse a JSON response into a list of (id, label) items. Returns None when
/// no list shape is found.
pub fn parse_items(body: &str) -> Option<Vec<LookupItem>> {
    let v: Value = serde_json::from_str(body.trim()).ok()?;
    let array = pick_array(&v)?;
    let mut items: Vec<LookupItem> = Vec::new();
    for entry in array {
        if let Some(item) = item_from_value(entry) {
            items.push(item);
        }
    }
    if items.is_empty() { None } else { Some(items) }
}

fn pick_array(v: &Value) -> Option<&Vec<Value>> {
    if let Some(arr) = v.as_array() {
        return Some(arr);
    }
    if let Some(obj) = v.as_object() {
        for key in &["data", "items", "results"] {
            if let Some(arr) = obj.get(*key).and_then(|x| x.as_array()) {
                return Some(arr);
            }
        }
        // Single-key object whose value is an array.
        if obj.len() == 1 {
            let only = obj.values().next().unwrap();
            if let Some(arr) = only.as_array() {
                return Some(arr);
            }
        }
    }
    None
}

fn item_from_value(v: &Value) -> Option<LookupItem> {
    let obj = v.as_object()?;
    let id_val = obj
        .get("id")
        .or_else(|| obj.get("Id"))
        .or_else(|| obj.get("_id"))
        .or_else(|| {
            obj.iter()
                .find(|(k, _)| {
                    let lk = k.to_ascii_lowercase();
                    (lk.ends_with("id") && lk.len() > 2) || lk == "id"
                })
                .map(|(_, v)| v)
        })?;
    let id = match id_val {
        Value::String(s) => s.clone(),
        Value::Number(n) => n.to_string(),
        _ => return None,
    };
    let label = ["name", "displayName", "label", "title", "summary"]
        .iter()
        .find_map(|k| obj.get(*k).and_then(|x| x.as_str()).map(String::from))
        .unwrap_or_else(|| id.clone());
    Some(LookupItem { id, label })
}

/// Suggest the env var name to ask for, given the chosen lookup file's
/// stem. e.g. `locations.curl` → `LOCATION_ID`, `delivery-partners.curl`
/// → `DELIVERY_PARTNER_ID`.
pub fn suggest_var_name(file: &Path) -> String {
    let stem = file
        .file_stem()
        .and_then(|s| s.to_str())
        .unwrap_or("ITEM")
        .trim_end_matches('s')
        .replace(['-', ' '], "_");
    format!("{}_ID", stem.to_ascii_uppercase())
}

/// Inverse of suggest_var_name: given a var like `LOCATION_ID`, find the
/// index of a lookup file whose stem matches. Tries the var with `_ID`
/// stripped, both with and without a trailing `s`.
pub fn match_lookup_for_var(var: &str, files: &[PathBuf]) -> Option<usize> {
    let core = var
        .strip_suffix("_ID")
        .or_else(|| var.strip_suffix("Id"))
        .unwrap_or(var)
        .to_ascii_lowercase()
        .replace('_', "-");
    let candidates = [
        format!("{core}s"), // locations
        core.clone(),       // location
    ];
    for cand in &candidates {
        if let Some(i) = files
            .iter()
            .position(|p| p.file_stem().and_then(|s| s.to_str()) == Some(cand.as_str()))
        {
            return Some(i);
        }
    }
    None
}

/// Find the `{{var_name}}` placeholder that the cursor sits inside,
/// returning its name. Returns None if the cursor isn't between matching
/// `{{` and `}}` markers.
pub fn var_at_cursor(text: &str, cursor: usize) -> Option<String> {
    if cursor > text.len() {
        return None;
    }
    // Look backward from cursor for `{{`, forward for `}}`. Reject if
    // either marker is missing or another `{{` / `}}` is between.
    let before = &text[..cursor];
    let open_at = before.rfind("{{")?;
    // No `}}` between open_at and cursor.
    if before[open_at + 2..].contains("}}") {
        return None;
    }
    let after = &text[cursor..];
    let close_rel = after.find("}}")?;
    if after[..close_rel].contains("{{") {
        return None;
    }
    let inner = &text[open_at + 2..cursor + close_rel];
    let trimmed = inner.trim();
    if trimmed.is_empty()
        || !trimmed
            .chars()
            .all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '$')
    {
        return None;
    }
    Some(trimmed.to_string())
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn parse_items_handles_data_envelope() {
        let body = json!({
            "data": [
                { "id": 1, "name": "First" },
                { "id": 2, "name": "Second" }
            ]
        })
        .to_string();
        let items = parse_items(&body).unwrap();
        assert_eq!(items.len(), 2);
        assert_eq!(items[0].id, "1");
        assert_eq!(items[0].label, "First");
    }

    #[test]
    fn parse_items_handles_bare_array() {
        let body = json!([
            { "id": "a", "displayName": "Alpha" },
            { "id": "b", "title": "Beta" }
        ])
        .to_string();
        let items = parse_items(&body).unwrap();
        assert_eq!(items[0].label, "Alpha");
        assert_eq!(items[1].label, "Beta");
    }

    #[test]
    fn parse_items_falls_back_to_camel_id_field() {
        let body = json!({
            "data": [
                { "merchantId": 2148, "name": "Hot Pizza" }
            ]
        })
        .to_string();
        let items = parse_items(&body).unwrap();
        assert_eq!(items[0].id, "2148");
        assert_eq!(items[0].label, "Hot Pizza");
    }

    #[test]
    fn parse_items_label_falls_back_to_id_when_no_name() {
        let body = json!([{ "id": 42 }]).to_string();
        let items = parse_items(&body).unwrap();
        assert_eq!(items[0].id, "42");
        assert_eq!(items[0].label, "42");
    }

    #[test]
    fn parse_items_returns_none_for_non_list_shape() {
        let body = json!({ "x": 1, "y": 2 }).to_string();
        assert!(parse_items(&body).is_none());
        assert!(parse_items("not json").is_none());
    }

    #[test]
    fn match_lookup_for_var_finds_plural_and_singular() {
        let files = vec![
            PathBuf::from("/ws/.rqst/lookups/locations.curl"),
            PathBuf::from("/ws/.rqst/lookups/merchant.curl"),
            PathBuf::from("/ws/.rqst/lookups/delivery-partners.curl"),
        ];
        assert_eq!(match_lookup_for_var("LOCATION_ID", &files), Some(0));
        assert_eq!(match_lookup_for_var("MERCHANT_ID", &files), Some(1));
        assert_eq!(match_lookup_for_var("DELIVERY_PARTNER_ID", &files), Some(2));
        assert_eq!(match_lookup_for_var("UNKNOWN_ID", &files), None);
    }

    #[test]
    fn var_at_cursor_finds_surrounding_placeholder() {
        let text = "curl '{{BASE_URL}}/api/{{LOCATION_ID}}/x'";
        // cursor in middle of {{LOCATION_ID}}
        let pos = text.find("LOCATION").unwrap() + 4;
        assert_eq!(var_at_cursor(text, pos).as_deref(), Some("LOCATION_ID"));
        // cursor outside any var
        let pos = text.find("/api").unwrap();
        assert_eq!(var_at_cursor(text, pos), None);
        // cursor in the BASE_URL var
        let pos = text.find("BASE_URL").unwrap() + 1;
        assert_eq!(var_at_cursor(text, pos).as_deref(), Some("BASE_URL"));
    }

    #[test]
    fn var_at_cursor_rejects_unterminated_placeholders() {
        assert_eq!(var_at_cursor("{{LOCATION_ID", 5), None);
        assert_eq!(var_at_cursor("LOCATION_ID}}", 5), None);
    }

    #[test]
    fn suggest_var_name_strips_trailing_s_and_uppercases() {
        assert_eq!(
            suggest_var_name(Path::new(".rqst/lookups/locations.curl")),
            "LOCATION_ID"
        );
        assert_eq!(
            suggest_var_name(Path::new(".rqst/lookups/delivery-partners.curl")),
            "DELIVERY_PARTNER_ID"
        );
    }

    /// test-writer 2026-06-28 coverage gap: scan_lookups was
    /// rewritten from flat read_dir to DFS but had zero tests.
    #[test]
    fn scan_lookups_finds_files_in_subdirectories() {
        let d = tempfile::tempdir().unwrap();
        let lookups = d.path().join(".rqst").join("lookups");
        std::fs::create_dir_all(lookups.join("auth")).unwrap();
        std::fs::create_dir_all(lookups.join("billing").join("nested")).unwrap();
        std::fs::write(lookups.join("top.curl"), "GET /\n").unwrap();
        std::fs::write(lookups.join("auth").join("login.curl"), "GET /\n").unwrap();
        std::fs::write(
            lookups.join("billing").join("nested").join("ping.curl"),
            "GET /\n",
        )
        .unwrap();
        let found = scan_lookups(d.path());
        let suffixes: std::collections::HashSet<String> = found
            .iter()
            .map(|p| {
                p.strip_prefix(&lookups)
                    .unwrap()
                    .to_string_lossy()
                    .into_owned()
            })
            .collect();
        // Normalize path separators so the assertion works on both
        // Windows (`\`) and Unix (`/`).
        let s = std::path::MAIN_SEPARATOR;
        assert!(suffixes.contains("top.curl"));
        assert!(suffixes.contains(&format!("auth{s}login.curl")));
        assert!(suffixes.contains(&format!("billing{s}nested{s}ping.curl")));
        assert_eq!(found.len(), 3);
    }

    #[test]
    fn scan_lookups_skips_dotfiles_and_target_dirs() {
        let d = tempfile::tempdir().unwrap();
        let lookups = d.path().join(".rqst").join("lookups");
        std::fs::create_dir_all(lookups.join(".hidden")).unwrap();
        std::fs::create_dir_all(lookups.join("target")).unwrap();
        std::fs::create_dir_all(lookups.join("node_modules")).unwrap();
        std::fs::create_dir_all(lookups.join("real")).unwrap();
        std::fs::write(lookups.join(".hidden").join("x.curl"), "GET /\n").unwrap();
        std::fs::write(lookups.join("target").join("y.curl"), "GET /\n").unwrap();
        std::fs::write(lookups.join("node_modules").join("z.curl"), "GET /\n").unwrap();
        std::fs::write(lookups.join("real").join("ok.curl"), "GET /\n").unwrap();
        let found = scan_lookups(d.path());
        assert_eq!(found.len(), 1, "only `real/ok.curl` should match");
        assert!(found[0].ends_with("real/ok.curl"));
    }

    #[test]
    fn scan_lookups_accepts_all_three_extensions() {
        let d = tempfile::tempdir().unwrap();
        let lookups = d.path().join(".rqst").join("lookups");
        std::fs::create_dir_all(&lookups).unwrap();
        std::fs::write(lookups.join("a.curl"), "GET /\n").unwrap();
        std::fs::write(lookups.join("b.http"), "GET /\n").unwrap();
        std::fs::write(lookups.join("c.rest"), "GET /\n").unwrap();
        std::fs::write(lookups.join("d.txt"), "GET /\n").unwrap();
        let found = scan_lookups(d.path());
        assert_eq!(found.len(), 3, ".curl/.http/.rest match; .txt excluded");
    }

    #[test]
    fn scan_lookups_returns_empty_when_dir_absent() {
        let d = tempfile::tempdir().unwrap();
        let found = scan_lookups(d.path());
        assert!(found.is_empty());
    }
}