data-gov 0.4.0

Rust client library and CLI for data.gov - search, explore, and download U.S. government datasets
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
use std::path::PathBuf;
use std::str::FromStr;

/// REPL Commands
#[derive(Debug, Clone)]
pub enum ReplCommand {
    Search {
        query: String,
        limit: Option<i32>,
    },
    Show {
        dataset_id: Option<String>,
    },
    Download {
        /// Raw arguments — interpretation depends on session context.
        /// In a dataset: all args are resource selectors.
        /// Otherwise: first arg is dataset, rest are resource selectors.
        args: Vec<String>,
    },
    List {
        /// Explicit subject (`organizations`/`orgs`). When `None`, the command
        /// is context-dependent: at root it lists organizations, at an org it
        /// lists that org's datasets, and at a dataset it lists distributions.
        what: Option<String>,
    },
    /// Fetch the next page of the most recent listing or search.
    Next,
    Select {
        path: String,
    },
    SetDir {
        path: PathBuf,
    },
    Info,
    Help,
    Quit,
}

/// Cursor describing what was last listed so a subsequent `next` knows
/// what to advance.
#[derive(Debug, Clone)]
pub enum ListingCursor {
    /// Datasets in `org`, paginated via `after`.
    OrgDatasets {
        org: String,
        after: String,
        page_size: i32,
    },
    /// Search results for `query` (optionally filtered by org), paginated
    /// via `after`. Mirrors the args originally passed to `search`.
    SearchResults {
        query: String,
        organization: Option<String>,
        after: String,
        page_size: i32,
    },
}

/// Active session context set via `select /org/dataset`.
#[derive(Debug, Clone, Default)]
pub struct SessionContext {
    pub org: Option<String>,
    pub dataset: Option<String>,
    /// Pagination cursor from the most recent listing or search. Populated
    /// when the previous response carried an `after` cursor; consumed by
    /// the `next` command.
    pub last_listing: Option<ListingCursor>,
}

impl SessionContext {
    /// Navigate the context, similar to `cd` in a filesystem.
    ///
    /// Absolute paths (leading `/`):
    /// - `/org/dataset` — set both org and dataset
    /// - `/org` or `/org/` — set org, clear dataset
    /// - `/` — clear both (go to root)
    ///
    /// Relative paths (no leading `/`):
    /// - At root: `org` sets the org
    /// - At org: `dataset` sets the dataset
    /// - At dataset: error (nowhere deeper to go)
    ///
    /// Special:
    /// - `..` — go up one level (dataset→org, org→root)
    pub fn apply_navigate(&mut self, path: &str) -> Result<(), String> {
        if path.starts_with('/') {
            return self.apply_absolute(path);
        }
        self.apply_relative(path)
    }

    /// Handle absolute path navigation (leading `/`).
    fn apply_absolute(&mut self, path: &str) -> Result<(), String> {
        let inner = &path[1..]; // strip leading '/'

        if inner.is_empty() {
            // `/` — clear everything
            self.org = None;
            self.dataset = None;
            return Ok(());
        }

        // `/org` or `/org/dataset`
        match inner.split_once('/') {
            None => {
                self.org = Some(inner.to_string());
                self.dataset = None;
            }
            Some((org, rest)) => {
                let dataset = rest.trim_end_matches('/');
                self.org = Some(org.to_string());
                if dataset.is_empty() {
                    self.dataset = None;
                } else {
                    self.dataset = Some(dataset.to_string());
                }
            }
        }

        Ok(())
    }

    /// Handle relative path navigation (no leading `/`).
    fn apply_relative(&mut self, path: &str) -> Result<(), String> {
        let path = path.trim_end_matches('/');

        if path == ".." {
            // Go up one level
            if self.dataset.is_some() {
                self.dataset = None;
            } else if self.org.is_some() {
                self.org = None;
            }
            // Already at root — no-op
            return Ok(());
        }

        if path.is_empty() {
            return Err("empty path".to_string());
        }

        if self.dataset.is_some() {
            return Err(format!(
                "already in a dataset; use '..' to go up first, or use an absolute path: /org/{path}"
            ));
        }

        if self.org.is_some() {
            // At org level — relative path is a dataset
            self.dataset = Some(path.to_string());
        } else {
            // At root — relative path is an org
            self.org = Some(path.to_string());
        }

        Ok(())
    }

    /// Format the context as a prompt-friendly string.
    pub fn prompt_label(&self) -> String {
        match (&self.org, &self.dataset) {
            (Some(org), Some(ds)) => format!("/{org}/{ds}"),
            (Some(org), None) => format!("/{org}"),
            (None, Some(ds)) => format!("//{ds}"),
            (None, None) => String::new(),
        }
    }
}

/// Parse a command string respecting quoted arguments
/// Example: `foo bar "baz qux"` -> ["foo", "bar", "baz qux"]
pub fn parse_command_args(s: &str) -> Vec<String> {
    let mut args = Vec::new();
    let mut current_arg = String::new();
    let mut in_quotes = false;
    let chars = s.trim().chars().peekable();

    for ch in chars {
        match ch {
            '"' => {
                in_quotes = !in_quotes;
            }
            ' ' | '\t' if !in_quotes => {
                if !current_arg.is_empty() {
                    args.push(current_arg.clone());
                    current_arg.clear();
                }
            }
            _ => {
                current_arg.push(ch);
            }
        }
    }

    if !current_arg.is_empty() {
        args.push(current_arg);
    }

    args
}

impl ReplCommand {
    pub fn from_parts(parts: &[String]) -> Result<Self, String> {
        if parts.is_empty() {
            return Err("Empty command".to_string());
        }

        let command = parts[0].to_lowercase();

        match command.as_str() {
            "search" | "s" => {
                if parts.len() < 2 {
                    return Err("Usage: search <query> [limit]".to_string());
                }
                let query = parts[1..].join(" ");
                let limit = if parts.len() > 2 {
                    parts.last().and_then(|s| s.parse().ok())
                } else {
                    None
                };
                Ok(ReplCommand::Search { query, limit })
            }
            "show" | "describe" | "d" => {
                if parts.len() > 2 {
                    return Err("Usage: show [dataset_id]".to_string());
                }
                Ok(ReplCommand::Show {
                    dataset_id: parts.get(1).cloned(),
                })
            }
            "download" | "dl" => Ok(ReplCommand::Download {
                args: parts[1..].to_vec(),
            }),
            "select" | "sel" | "cd" => {
                if parts.len() != 2 {
                    return Err(
                        "Usage: cd <path>  (e.g. cd nasa-gov, cd air-quality, cd .., cd /org/dataset, cd /)"
                            .to_string(),
                    );
                }
                Ok(ReplCommand::Select {
                    path: parts[1].clone(),
                })
            }
            "list" | "ls" => {
                let what = match parts.len() {
                    1 => None,
                    2 => Some(parts[1].clone()),
                    _ => {
                        return Err("Usage: ls [organizations|orgs]".to_string());
                    }
                };
                Ok(ReplCommand::List { what })
            }
            "lcd" | "setdir" => {
                if parts.len() != 2 {
                    return Err("Usage: lcd <path>".to_string());
                }
                Ok(ReplCommand::SetDir {
                    path: PathBuf::from(&parts[1]),
                })
            }
            "info" | "status" => Ok(ReplCommand::Info),
            "next" | "n" | "more" => {
                if parts.len() != 1 {
                    return Err("Usage: next  (no arguments)".to_string());
                }
                Ok(ReplCommand::Next)
            }
            "help" | "h" | "?" => Ok(ReplCommand::Help),
            "quit" | "exit" | "q" => Ok(ReplCommand::Quit),
            _ => Err(format!("Unknown command: {}", parts[0])),
        }
    }
}

impl FromStr for ReplCommand {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let parts = parse_command_args(s);
        ReplCommand::from_parts(&parts)
    }
}

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

    #[test]
    fn test_parse_download_with_dataset_and_index() {
        let result = ReplCommand::from_str("download my-dataset 0");
        let Ok(ReplCommand::Download { args }) = result else {
            panic!("Expected Download command");
        };
        assert_eq!(args, vec!["my-dataset", "0"]);
    }

    #[test]
    fn test_parse_download_with_dataset_and_name() {
        let result = ReplCommand::from_str("download my-dataset csv");
        let Ok(ReplCommand::Download { args }) = result else {
            panic!("Expected Download command");
        };
        assert_eq!(args, vec!["my-dataset", "csv"]);
    }

    #[test]
    fn test_parse_download_dataset_only() {
        let result = ReplCommand::from_str("download my-dataset");
        let Ok(ReplCommand::Download { args }) = result else {
            panic!("Expected Download command");
        };
        assert_eq!(args, vec!["my-dataset"]);
    }

    #[test]
    fn test_parse_download_dl_alias() {
        let result = ReplCommand::from_str("dl my-dataset 0");
        let Ok(ReplCommand::Download { args }) = result else {
            panic!("Expected Download command");
        };
        assert_eq!(args, vec!["my-dataset", "0"]);
    }

    #[test]
    fn test_parse_download_no_args() {
        let result = ReplCommand::from_str("download");
        let Ok(ReplCommand::Download { args }) = result else {
            panic!("Expected Download command");
        };
        assert!(args.is_empty());
    }

    #[test]
    fn test_parse_download_multiple_selectors() {
        // "download dataset-id "RDF File" "XML File"" — multiple resource selectors
        let result = ReplCommand::from_str("download dataset-id \"RDF File\" \"XML File\"");
        let Ok(ReplCommand::Download { args }) = result else {
            panic!("Expected Download command");
        };
        assert_eq!(args, vec!["dataset-id", "RDF File", "XML File"]);
    }

    #[test]
    fn test_parse_download_multiple_indices() {
        let result = ReplCommand::from_str("download 0 1 2");
        let Ok(ReplCommand::Download { args }) = result else {
            panic!("Expected Download command");
        };
        assert_eq!(args, vec!["0", "1", "2"]);
    }

    #[test]
    fn test_parse_command_args_simple() {
        let args = parse_command_args("download dataset 0");
        assert_eq!(args, vec!["download", "dataset", "0"]);
    }

    #[test]
    fn test_parse_command_args_with_quotes() {
        let args = parse_command_args("download dataset \"Comma Separated Values File\"");
        assert_eq!(
            args,
            vec!["download", "dataset", "Comma Separated Values File"]
        );
    }

    #[test]
    fn test_parse_command_args_multiple_spaces() {
        let args = parse_command_args("search   climate    data");
        assert_eq!(args, vec!["search", "climate", "data"]);
    }

    #[test]
    fn test_parse_command_args_quotes_with_extra_spaces() {
        let args = parse_command_args("download   dataset   \"Multi Word Name\"  ");
        assert_eq!(args, vec!["download", "dataset", "Multi Word Name"]);
    }

    #[test]
    fn test_parse_download_with_quoted_name() {
        let result = ReplCommand::from_str("download my-dataset \"CSV File\"");
        let Ok(ReplCommand::Download { args }) = result else {
            panic!("Expected Download command");
        };
        assert_eq!(args, vec!["my-dataset", "CSV File"]);
    }

    #[test]
    fn test_parse_download_with_long_quoted_name() {
        let result = ReplCommand::from_str("download dataset \"Comma Separated Values File\"");
        let Ok(ReplCommand::Download { args }) = result else {
            panic!("Expected Download command");
        };
        assert_eq!(args, vec!["dataset", "Comma Separated Values File"]);
    }

    // --- SessionContext: absolute path tests ---

    #[test]
    fn test_absolute_org_and_dataset() {
        let mut ctx = SessionContext::default();
        ctx.apply_navigate("/epa-gov/air-quality").unwrap();
        assert_eq!(ctx.org, Some("epa-gov".to_string()));
        assert_eq!(ctx.dataset, Some("air-quality".to_string()));
        assert_eq!(ctx.prompt_label(), "/epa-gov/air-quality");
    }

    #[test]
    fn test_absolute_org_only() {
        let mut ctx = SessionContext::default();
        ctx.apply_navigate("/nasa-gov").unwrap();
        assert_eq!(ctx.org, Some("nasa-gov".to_string()));
        assert!(ctx.dataset.is_none());
        assert_eq!(ctx.prompt_label(), "/nasa-gov");
    }

    #[test]
    fn test_absolute_org_with_trailing_slash() {
        let mut ctx = SessionContext::default();
        ctx.apply_navigate("/epa-gov/").unwrap();
        assert_eq!(ctx.org, Some("epa-gov".to_string()));
        assert!(ctx.dataset.is_none());
    }

    #[test]
    fn test_absolute_root_clears_all() {
        let mut ctx = SessionContext {
            org: Some("epa-gov".to_string()),
            dataset: Some("air-quality".to_string()),
            last_listing: None,
        };
        ctx.apply_navigate("/").unwrap();
        assert!(ctx.org.is_none());
        assert!(ctx.dataset.is_none());
        assert_eq!(ctx.prompt_label(), "");
    }

    #[test]
    fn test_absolute_replaces_previous_context() {
        let mut ctx = SessionContext {
            org: Some("old-org".to_string()),
            dataset: Some("old-dataset".to_string()),
            last_listing: None,
        };
        ctx.apply_navigate("/new-org/new-dataset").unwrap();
        assert_eq!(ctx.org, Some("new-org".to_string()));
        assert_eq!(ctx.dataset, Some("new-dataset".to_string()));
    }

    #[test]
    fn test_absolute_org_clears_dataset() {
        let mut ctx = SessionContext {
            org: Some("old-org".to_string()),
            dataset: Some("old-dataset".to_string()),
            last_listing: None,
        };
        ctx.apply_navigate("/new-org").unwrap();
        assert_eq!(ctx.org, Some("new-org".to_string()));
        assert!(ctx.dataset.is_none());
    }

    // --- SessionContext: relative path tests ---

    #[test]
    fn test_relative_org_from_root() {
        let mut ctx = SessionContext::default();
        ctx.apply_navigate("nasa-gov").unwrap();
        assert_eq!(ctx.org, Some("nasa-gov".to_string()));
        assert!(ctx.dataset.is_none());
    }

    #[test]
    fn test_relative_dataset_from_org() {
        let mut ctx = SessionContext {
            org: Some("epa-gov".to_string()),
            dataset: None,
            last_listing: None,
        };
        ctx.apply_navigate("water-data").unwrap();
        assert_eq!(ctx.org, Some("epa-gov".to_string()));
        assert_eq!(ctx.dataset, Some("water-data".to_string()));
    }

    #[test]
    fn test_relative_from_dataset_errors() {
        let mut ctx = SessionContext {
            org: Some("epa-gov".to_string()),
            dataset: Some("air-quality".to_string()),
            last_listing: None,
        };
        let result = ctx.apply_navigate("something");
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("already in a dataset"));
    }

    #[test]
    fn test_dotdot_from_dataset_to_org() {
        let mut ctx = SessionContext {
            org: Some("epa-gov".to_string()),
            dataset: Some("air-quality".to_string()),
            last_listing: None,
        };
        ctx.apply_navigate("..").unwrap();
        assert_eq!(ctx.org, Some("epa-gov".to_string()));
        assert!(ctx.dataset.is_none());
    }

    #[test]
    fn test_dotdot_from_org_to_root() {
        let mut ctx = SessionContext {
            org: Some("epa-gov".to_string()),
            dataset: None,
            last_listing: None,
        };
        ctx.apply_navigate("..").unwrap();
        assert!(ctx.org.is_none());
        assert!(ctx.dataset.is_none());
    }

    #[test]
    fn test_dotdot_from_root_is_noop() {
        let mut ctx = SessionContext::default();
        ctx.apply_navigate("..").unwrap();
        assert!(ctx.org.is_none());
        assert!(ctx.dataset.is_none());
    }

    #[test]
    fn test_relative_with_trailing_slash() {
        let mut ctx = SessionContext::default();
        ctx.apply_navigate("nasa-gov/").unwrap();
        assert_eq!(ctx.org, Some("nasa-gov".to_string()));
        assert!(ctx.dataset.is_none());
    }

    // --- SessionContext: prompt_label ---

    #[test]
    fn test_prompt_label_org_and_dataset() {
        let ctx = SessionContext {
            org: Some("epa-gov".to_string()),
            dataset: Some("air-quality".to_string()),
            last_listing: None,
        };
        assert_eq!(ctx.prompt_label(), "/epa-gov/air-quality");
    }

    #[test]
    fn test_prompt_label_dataset_only() {
        let ctx = SessionContext {
            org: None,
            dataset: Some("orphan-ds".to_string()),
            last_listing: None,
        };
        assert_eq!(ctx.prompt_label(), "//orphan-ds");
    }

    #[test]
    fn test_prompt_label_empty() {
        let ctx = SessionContext::default();
        assert_eq!(ctx.prompt_label(), "");
    }

    // --- Command parsing: select/cd/lcd ---

    #[test]
    fn test_parse_select_command() {
        let result = ReplCommand::from_str("select /epa-gov/air-quality");
        let Ok(ReplCommand::Select { path }) = result else {
            panic!("Expected Select command");
        };
        assert_eq!(path, "/epa-gov/air-quality");
    }

    #[test]
    fn test_parse_sel_alias() {
        let result = ReplCommand::from_str("sel /epa-gov");
        let Ok(ReplCommand::Select { path }) = result else {
            panic!("Expected Select command via 'sel' alias");
        };
        assert_eq!(path, "/epa-gov");
    }

    #[test]
    fn test_parse_cd_alias() {
        let result = ReplCommand::from_str("cd nasa-gov");
        let Ok(ReplCommand::Select { path }) = result else {
            panic!("Expected Select command via 'cd' alias");
        };
        assert_eq!(path, "nasa-gov");
    }

    #[test]
    fn test_parse_cd_dotdot() {
        let result = ReplCommand::from_str("cd ..");
        let Ok(ReplCommand::Select { path }) = result else {
            panic!("Expected Select command");
        };
        assert_eq!(path, "..");
    }

    #[test]
    fn test_parse_lcd_command() {
        let result = ReplCommand::from_str("lcd ./downloads");
        let Ok(ReplCommand::SetDir { path }) = result else {
            panic!("Expected SetDir command");
        };
        assert_eq!(path, PathBuf::from("./downloads"));
    }

    #[test]
    fn test_parse_setdir_alias() {
        let result = ReplCommand::from_str("setdir /tmp");
        let Ok(ReplCommand::SetDir { path }) = result else {
            panic!("Expected SetDir command via 'setdir' alias");
        };
        assert_eq!(path, PathBuf::from("/tmp"));
    }

    #[test]
    fn test_parse_show_without_dataset() {
        let result = ReplCommand::from_str("show");
        let Ok(ReplCommand::Show { dataset_id }) = result else {
            panic!("Expected Show command");
        };
        assert!(dataset_id.is_none());
    }
}