yerba 0.3.0

YAML Editing and Refactoring with Better Accuracy
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
use std::process;
use std::sync::LazyLock;

use indoc::indoc;

use super::colorize_examples;
use super::{output, parse_file, resolve_files};

static EXAMPLES: LazyLock<String> = LazyLock::new(|| {
  colorize_examples(indoc! {r#"
    yerba sort config.yml "tags"
    yerba sort videos.yml --by ".title"
    yerba sort videos.yml --by ".date" --order desc --by ".title"
    yerba sort videos.yml "[].speakers" --by ".name"
    yerba sort videos.yml "[]" --by ".id" --order "talk-3,talk-1,talk-2"
    yerba sort speakers.yml "[]" --by ".name" --order "Charlie,Alice,Bob"
  "#})
});

#[derive(clap::Args)]
#[command(
  about = "Sort or reorder items in a sequence",
  arg_required_else_help = true,
  after_help = EXAMPLES.as_str()
)]
pub struct Args {
  file: String,
  /// Selector (optional — omit for root-level sequence)
  selector: Option<String>,
  /// Field to sort or match by (can be repeated for tie-breakers)
  #[arg(long, action = clap::ArgAction::Append)]
  by: Vec<String>,
  /// Sort direction (asc/desc) or explicit order (comma-separated values, must list all items)
  #[arg(long, action = clap::ArgAction::Append)]
  order: Vec<String>,
  /// Additional fields to display alongside values (e.g. --context ".title")
  #[arg(long, action = clap::ArgAction::Append)]
  context: Vec<String>,
  /// Case-sensitive sort (default: case-insensitive)
  #[arg(long)]
  case_sensitive: bool,
  #[arg(long)]
  dry_run: bool,
}

fn is_direction(value: &str) -> bool {
  matches!(value, "asc" | "desc" | "ascending" | "descending")
}

fn is_explicit_reorder(orders: &[String]) -> bool {
  orders.iter().any(|o| !is_direction(o))
}

impl Args {
  pub fn run(self) {
    use super::color::*;

    if self.by.is_empty() && self.order.is_empty() && self.selector.is_none() {
      let document = parse_file(&self.file);

      eprintln!("{RED}Error:{RESET} specify a selector, --by, or --order");
      eprintln!();
      eprintln!("  {BOLD}Examples:{RESET}");
      eprintln!("    yerba sort \"{}\" \"tags\"", self.file);
      eprintln!("    yerba sort \"{}\" --by \".title\" --order asc", self.file);
      eprintln!();

      super::show_similar_selectors(&self.file, &document, "[]");

      process::exit(1);
    }

    if !self.by.is_empty() && self.order.is_empty() {
      self.show_values();
    } else if is_explicit_reorder(&self.order) {
      self.run_reorder();
    } else {
      self.run_sort();
    }
  }

  fn show_values(self) {
    use super::color::*;

    if self.by.len() != 1 {
      eprintln!("{RED}Error:{RESET} --order is required when using --by");
      process::exit(1);
    }

    let by = &self.by[0];
    let selector = self.selector.as_deref().unwrap_or("");

    let items_selector = if selector.is_empty() {
      "[]".to_string()
    } else {
      format!("{}[]", selector)
    };

    let document = parse_file(&self.file);
    let (labels, context_values, selector_display) = self.resolve_labels(&document, by, selector, &items_selector);

    let context_hint = if self.context.is_empty() {
      format!("\n\n  {DIM}Add --context \".field\" to show additional fields alongside values{RESET}")
    } else {
      String::new()
    };

    eprintln!("{RED}Error:{RESET} --order is required when using --by");
    eprintln!();
    eprintln!("  {BOLD}Current values (by {by}):{RESET}");

    for (index, label) in labels.iter().enumerate() {
      let context = context_values.get(index).map(|c| c.as_slice()).unwrap_or(&[]);
      eprintln!("    {}", self.format_label_line(index, label, context));
    }

    let csv: String = labels.join(",");

    eprintln!("{context_hint}");
    eprintln!();
    eprintln!("  {BOLD}To sort alphabetically:{RESET}");
    eprintln!(
      "    yerba sort \"{}\"{selector_display} --by \"{by}\" --order asc",
      self.file
    );
    eprintln!(
      "    yerba sort \"{}\"{selector_display} --by \"{by}\" --order desc",
      self.file
    );
    eprintln!();
    eprintln!("  {BOLD}To reorder explicitly:{RESET}");
    eprintln!(
      "    yerba sort \"{}\"{selector_display} --by \"{by}\" --order \"{csv}\"",
      self.file
    );
    eprintln!();
    eprintln!("  {BOLD}To move individual items:{RESET}");
    eprintln!(
      "    yerba move \"{}\"{selector_display} <item> --before/--after <target>",
      self.file
    );

    process::exit(1);
  }

  fn run_sort(self) {
    use super::color::*;

    let selector = self.selector.as_deref().unwrap_or("");

    let sort_fields: Vec<yerba::SortField> = if self.by.is_empty() {
      Vec::new()
    } else {
      self
        .by
        .iter()
        .enumerate()
        .map(|(index, field)| {
          let direction = self.order.get(index).map(|s| s.as_str());

          match direction {
            Some("desc" | "descending") => yerba::SortField::desc(field),
            Some("asc" | "ascending") | None => yerba::SortField::asc(field),
            Some(other) => {
              eprintln!("{RED}Error:{RESET} invalid sort direction \"{other}\". Use \"asc\" or \"desc\"");
              process::exit(1);
            }
          }
        })
        .collect()
    };

    for resolved_file in resolve_files(&self.file) {
      let mut document = parse_file(&resolved_file);

      if sort_fields.is_empty() {
        let first_item_selector = if selector.is_empty() {
          "[0]".to_string()
        } else {
          format!("{}[0]", selector)
        };

        match document.get_value(&first_item_selector) {
          Some(first) if first.is_mapping() => {
            eprintln!("{RED}Error:{RESET} --by is required to sort a sequence of maps");
            eprintln!();

            let selectors = document.selectors();
            let prefix = if selector.is_empty() { "[]." } else { "" };
            let fields: Vec<&String> = selectors
              .iter()
              .filter(|s| {
                let check = if prefix.is_empty() {
                  format!("{}[].", selector)
                } else {
                  prefix.to_string()
                };
                s.starts_with(&check) && !s[check.len()..].contains('.') && !s[check.len()..].contains('[')
              })
              .collect();

            if !fields.is_empty() {
              eprintln!("  {BOLD}Available fields:{RESET}");

              for field in &fields {
                let short = field.rsplit_once('.').map(|(_, f)| f).unwrap_or(field);
                eprintln!("    .{short}");
              }
            }

            process::exit(1);
          }

          None if selector.is_empty() => {
            eprintln!(
              "{RED}Error:{RESET} no sequence found at root level in {}",
              resolved_file
            );
            eprintln!();
            eprintln!("  {DIM}Specify a selector for the sequence to sort:{RESET}");
            eprintln!("    yerba sort \"{}\" \"<selector>\"", self.file);
            eprintln!();

            super::show_similar_selectors(&resolved_file, &document, "[]");

            process::exit(1);
          }

          _ => {}
        }
      }

      if document.sort_items(selector, &sort_fields, self.case_sensitive).is_ok() {
        output(&resolved_file, &document, self.dry_run);
      }
    }
  }

  fn run_reorder(self) {
    use super::color::*;

    if self.by.len() != 1 {
      eprintln!("{RED}Error:{RESET} explicit --order requires exactly one --by field");

      process::exit(1);
    }

    if self.order.len() != 1 {
      eprintln!("{RED}Error:{RESET} explicit --order must be a single comma-separated list");

      process::exit(1);
    }

    let by = &self.by[0];
    let order = &self.order[0];
    let selector = self.selector.as_deref().unwrap_or("");

    let items_selector = if selector.is_empty() {
      "[]".to_string()
    } else {
      format!("{}[]", selector)
    };

    let mut document = parse_file(&self.file);
    let mut seen = std::collections::HashSet::new();

    let (labels, _, _) = self.resolve_labels(&document, by, selector, &items_selector);
    let duplicates: Vec<&String> = labels.iter().filter(|label| !seen.insert(label.as_str())).collect();

    if !duplicates.is_empty() {
      eprintln!("{RED}Error:{RESET} --order requires unique values for {by}, but found duplicates");
      eprintln!();
      eprintln!("  {BOLD}Duplicate values:{RESET}");

      for label in &duplicates {
        eprintln!("    {label}");
      }

      eprintln!();
      eprintln!("  {DIM}Use \"yerba sort\" with --by instead to sort by field, or");
      eprintln!("  choose a --by field with unique values (e.g. \".id\"){RESET}");

      process::exit(1);
    }

    let values_with_commas: Vec<&String> = labels.iter().filter(|l| l.contains(',')).collect();

    if !values_with_commas.is_empty() {
      let selector_display = if selector.is_empty() {
        String::new()
      } else {
        format!(" \"{selector}\"")
      };

      eprintln!("{RED}Error:{RESET} some values for {by} contain commas, which conflicts with --order parsing");
      eprintln!();
      eprintln!("  {BOLD}Values with commas:{RESET}");

      for label in &values_with_commas {
        eprintln!("    {label}");
      }

      eprintln!();
      eprintln!("  {BOLD}Use yerba move to reorder individual items instead:{RESET}");
      eprintln!(
        "    yerba move \"{}\"{selector_display} <item> --before/--after <target>",
        self.file
      );

      process::exit(1);
    }

    let desired_order: Vec<&str> = order.split(',').map(|s| s.trim()).collect();

    let mut used = vec![false; labels.len()];
    let mut moves: Vec<usize> = Vec::new();

    for desired in &desired_order {
      let found = labels
        .iter()
        .enumerate()
        .find(|(index, label)| label.as_str() == *desired && !used[*index]);

      if let Some((index, _)) = found {
        moves.push(index);
        used[index] = true;
      } else {
        eprintln!("{RED}Error:{RESET} no item found with {by} == \"{desired}\"");
        eprintln!();
        eprintln!("  {BOLD}Available values:{RESET}");

        for (index, label) in labels.iter().enumerate() {
          eprintln!("    {DIM}{index}:{RESET} {label}");
        }

        process::exit(1);
      }
    }

    let missing: Vec<&String> = labels
      .iter()
      .enumerate()
      .filter(|(index, _)| !used[*index])
      .map(|(_, label)| label)
      .collect();

    if !missing.is_empty() {
      eprintln!(
        "{RED}Error:{RESET} --order must specify all {} items, but {} are missing",
        labels.len(),
        missing.len()
      );
      eprintln!();
      eprintln!("  {BOLD}Missing values:{RESET}");

      for label in &missing {
        eprintln!("    {label}");
      }

      eprintln!();
      eprintln!("  {BOLD}All values (by {by}):{RESET}");

      for label in &labels {
        eprintln!("    {label}");
      }

      eprintln!();
      eprintln!("  {BOLD}To move individual items, use:{RESET}");
      eprintln!("    yerba move <file> <selector> <item> --before/--after <target>");

      process::exit(1);
    }

    let container = if selector.is_empty() { "" } else { selector };

    for target in 0..moves.len() {
      let source = moves[target];

      if source != target {
        let result = document.move_item(container, source, target);

        if let Err(error) = result {
          eprintln!("{RED}Error:{RESET} {}", error);
          process::exit(1);
        }

        for item in moves.iter_mut().skip(target + 1) {
          if *item >= target && *item < source {
            *item += 1;
          } else if *item == source {
            *item = target;
          }
        }
      }
    }

    output(&self.file, &document, self.dry_run);
  }

  fn resolve_labels(
    &self,
    document: &yerba::Document,
    by: &str,
    selector: &str,
    items_selector: &str,
  ) -> (Vec<String>, Vec<Vec<String>>, String) {
    use super::color::*;

    let items = document.get_values(items_selector);

    if items.is_empty() {
      if selector.is_empty() {
        eprintln!("{RED}Error:{RESET} no sequence found at root level");
        eprintln!();
        eprintln!("  {DIM}If the file is a map, specify which sequence to sort:{RESET}");
        eprintln!(
          "    yerba sort \"{}\" \"<selector>\" --by \"{by}\" --order asc",
          self.file
        );
        eprintln!();

        super::show_similar_selectors(&self.file, document, "[]");
      } else {
        eprintln!("{RED}Error:{RESET} no sequence found at selector: {selector}");

        super::show_similar_selectors(&self.file, document, selector);
      }

      process::exit(1);
    }

    let by_is_scalar = by == ".";

    if !by_is_scalar {
      let by_selector = if selector.is_empty() {
        format!("[].{}", by.strip_prefix('.').unwrap_or(by))
      } else {
        format!("{}[].{}", selector, by.strip_prefix('.').unwrap_or(by))
      };

      if !document.exists(&by_selector) {
        eprintln!("{RED}Error:{RESET} field \"{by}\" not found in items");

        super::show_similar_selectors(&self.file, document, &by_selector);

        process::exit(1);
      }
    }

    let labels: Vec<String> = items
      .iter()
      .map(|item| {
        if by_is_scalar {
          match item {
            serde_yaml::Value::String(string) => string.clone(),
            _ => serde_json::to_string(&yerba::json::yaml_to_json(item)).unwrap_or_default(),
          }
        } else {
          let field = by.strip_prefix('.').unwrap_or(by);

          yerba::json::resolve_select_field(item, field)
            .as_str()
            .unwrap_or("")
            .to_string()
        }
      })
      .collect();

    let context_values: Vec<Vec<String>> = items
      .iter()
      .map(|item| {
        self
          .context
          .iter()
          .map(|context| {
            let field = context.strip_prefix('.').unwrap_or(context);

            let value = yerba::json::resolve_select_field(item, field);

            match &value {
              serde_json::Value::String(string) => string.clone(),
              serde_json::Value::Null => String::new(),
              _ => serde_json::to_string(&value).unwrap_or_default(),
            }
          })
          .collect()
      })
      .collect();

    let selector_display = if selector.is_empty() {
      String::new()
    } else {
      format!(" \"{selector}\"")
    };

    (labels, context_values, selector_display)
  }

  fn format_label_line(&self, index: usize, label: &str, context: &[String]) -> String {
    use super::color::*;

    if context.is_empty() {
      format!("{DIM}[{index}]{RESET} {label}")
    } else {
      let context = context
        .iter()
        .filter(|c| !c.is_empty())
        .cloned()
        .collect::<Vec<_>>()
        .join(", ");

      if context.is_empty() {
        format!("{DIM}[{index}]{RESET} {label}")
      } else {
        format!("{DIM}[{index}]{RESET} {label}  {DIM}{context}{RESET}")
      }
    }
  }
}