deq-cli 1.0.0-rc.1

Command-line tool (`deq`) for Deq shapes, traditional Kurdish tattoo motifs.
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
631
632
633
634
635
636
637
638
639
640
//! The `deq` command-line tool: the interface of `spec/cli.json` (ADR 0011).
//!
//! Help, messages and exit codes match the Node and Python CLIs. Formats outside the core subset
//! exit with code 4 and point to the Node CLI.

// Building text line by line with `format!` reads best here.
#![allow(clippy::format_collect)]

mod output;
mod parse;

use deq_shapes::color::{BLACK, parse_color, to_hex};
use deq_shapes::export::export_shapes;
use deq_shapes::format::{js_round, js_str};
use deq_shapes::raster::{RasterFormat, encode_svg};
use deq_shapes::validate::{MAX_SPEC_BYTES, validate_spec};
use deq_shapes::{
    DeqError, ErrorCode, decode_leet, get_shape, leet, list_categories, list_shapes, pattern,
    render_flag,
};
use output::{File, emit, errno, join_path, write_all};
use parse::{Flags, Parsed, arr, param_value, parse_args, s};
use serde_json::{Map, Value, json};
use std::io::{Read as _, Write as _};
use std::process::ExitCode;

/// Exit code for formats only the Node CLI writes.
const EXIT_NODE_ONLY: u8 = 4;
/// Largest raster image side, in px.
const MAX_PX: f64 = 8192.0;
/// Largest SVG `deq leet --decode` reads, in bytes.
const MAX_DECODE_BYTES: usize = 4 * 1024 * 1024;
/// Flags that `deq leet --decode` refuses, in the order they are checked.
const NOT_WITH_DECODE: [&str; 13] = [
    "format",
    "out",
    "force",
    "level",
    "script",
    "seed",
    "letter-spacing",
    "line-spacing",
    "align",
    "colors",
    "current-color",
    "matte",
    "quality",
];

/// Everything that stops a command, by exit code.
#[derive(Debug)]
pub enum CliError {
    /// The command line is wrong (2); the command it belongs to, if any.
    Usage(String, Option<String>),
    /// A file could not be read or written (3).
    Io(String),
    /// The format is only in the Node CLI (4).
    NodeOnly(String),
    /// A shape, option or spec file is invalid (1).
    Deq(DeqError),
}

impl From<DeqError> for CliError {
    fn from(e: DeqError) -> Self {
        Self::Deq(e)
    }
}

impl std::fmt::Display for CliError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Usage(m, _) | Self::Io(m) => f.write_str(m),
            Self::NodeOnly(fmt) => write!(
                f,
                "format \"{fmt}\" is only available in the Node CLI (npx @0xdolan/deq-cli)"
            ),
            Self::Deq(e) => write!(f, "{e}"),
        }
    }
}

type Res<T> = Result<T, CliError>;

fn stdout(data: &[u8]) {
    let mut out = std::io::stdout().lock();
    // A closed pipe (`deq list | head`) is not an error worth reporting.
    let _ = out.write_all(data).and_then(|()| out.flush());
}

fn stderr(text: &str) {
    let _ = std::io::stderr().lock().write_all(text.as_bytes());
}

fn flag_str<'a>(flags: &'a Flags, key: &str) -> Option<&'a str> {
    flags.get(key).and_then(Value::as_str)
}

fn flag_on(flags: &Flags, key: &str) -> bool {
    flags.get(key) == Some(&Value::Bool(true))
}

fn invalid_options(message: String, issue: String) -> CliError {
    CliError::Deq(DeqError {
        code: ErrorCode::InvalidOptions,
        message,
        issues: vec![issue],
    })
}

// ---------------------------------------------------------------- options

fn render_options(command: &Value, flags: &Flags) -> Map<String, Value> {
    let mut o = Map::new();
    for f in arr(command, "flags") {
        let (Some(option), Some(value)) = (
            f.get("option").and_then(Value::as_str),
            flags.get(s(f, "name")),
        ) else {
            continue;
        };
        if s(f, "type") == "pair" {
            let pairs = value.as_array().map_or(&[][..], Vec::as_slice);
            let obj: Map<String, Value> = pairs
                .iter()
                .map(|p| {
                    let (k, v) = (
                        p.get(0).and_then(Value::as_str).unwrap_or(""),
                        p.get(1).and_then(Value::as_str).unwrap_or(""),
                    );
                    (
                        k.to_owned(),
                        if option == "params" {
                            param_value(v)
                        } else {
                            Value::from(v)
                        },
                    )
                })
                .collect();
            o.insert(option.to_owned(), Value::Object(obj));
        } else {
            o.insert(option.to_owned(), value.clone());
        }
    }
    o
}

fn export_options(command: &Value, flags: &Flags) -> Map<String, Value> {
    let mut o = Map::new();
    o.insert(
        "render".to_owned(),
        Value::Object(render_options(command, flags)),
    );
    for (flag, key) in [
        ("current-color", "currentColor"),
        ("matte", "matte"),
        ("quality", "quality"),
    ] {
        if let Some(v) = flags.get(flag) {
            o.insert(key.to_owned(), v.clone());
        }
    }
    o
}

fn port_format<'a>(data: &Value, fmt: &'a str) -> Res<&'a str> {
    let ports = data.get("formats").map_or(&[][..], |f| arr(f, "ports"));
    if ports.iter().any(|p| p.as_str() == Some(fmt)) {
        Ok(fmt)
    } else {
        Err(CliError::NodeOnly(fmt.to_owned()))
    }
}

// ---------------------------------------------------------------- commands

fn json_bytes(value: &Value) -> Vec<u8> {
    let mut text = serde_json::to_string_pretty(value).unwrap_or_default();
    text.push('\n');
    text.into_bytes()
}

fn list(flags: &Flags) -> Res<()> {
    let category = flag_str(flags, "category");
    if let Some(c) = category {
        let known = list_categories();
        if !known.contains(&c) {
            let issue = format!("unknown category \"{c}\" (known: {})", known.join(", "));
            return Err(invalid_options(issue.clone(), issue));
        }
    }
    let shapes = list_shapes(category);
    let en = |sh: &Value| sh.get("names").map_or("", |n| s(n, "en")).to_owned();
    if flag_on(flags, "json") {
        let rows: Vec<Value> = shapes
            .iter()
            .map(|sh| json!({"id": s(sh, "id"), "category": s(sh, "category"), "name": en(sh)}))
            .collect();
        stdout(&json_bytes(&Value::Array(rows)));
        return Ok(());
    }
    let w1 = shapes
        .iter()
        .map(|sh| s(sh, "id").chars().count())
        .max()
        .unwrap_or(0);
    let w2 = shapes
        .iter()
        .map(|sh| s(sh, "category").chars().count())
        .max()
        .unwrap_or(0);
    let text: String = shapes
        .iter()
        .map(|sh| {
            format!(
                "{:<w1$}  {:<w2$}  {}\n",
                s(sh, "id"),
                s(sh, "category"),
                en(sh)
            )
        })
        .collect();
    stdout(text.as_bytes());
    Ok(())
}

const INFO_FIELDS: [&str; 9] = [
    "id",
    "version",
    "category",
    "names",
    "aliases",
    "tags",
    "layers",
    "symmetry",
    "parameters",
];

fn info(shape_id: &str, flags: &Flags) -> Res<()> {
    let sh = get_shape(shape_id)?;
    if flag_on(flags, "json") {
        let obj: Map<String, Value> = INFO_FIELDS
            .iter()
            .map(|k| ((*k).to_owned(), sh.get(*k).cloned().unwrap_or(Value::Null)))
            .collect();
        stdout(&json_bytes(&Value::Object(obj)));
        return Ok(());
    }
    let words = |k: &str| {
        let a: Vec<&str> = arr(sh, k).iter().filter_map(Value::as_str).collect();
        if a.is_empty() {
            "-".to_owned()
        } else {
            a.join(", ")
        }
    };
    let names = sh.get("names").unwrap_or(&Value::Null);
    let kurdish = |k: &str| {
        let n = names.get(k).unwrap_or(&Value::Null);
        format!(
            "{} ({})",
            n.get("text").and_then(Value::as_str).unwrap_or("-"),
            s(n, "status")
        )
    };
    let mut lines = vec![
        format!("{}  {}", s(sh, "id"), s(names, "en")),
        format!("Category:    {}", s(sh, "category")),
        format!("Sorani:      {}", kurdish("ckb")),
        format!("Kurmanji:    {}", kurdish("kmr")),
        format!("Aliases:     {}", words("aliases")),
        format!("Tags:        {}", words("tags")),
        format!("Layers:      {}", words("layers")),
        format!(
            "Symmetry:    {}",
            js_str(sh.get("symmetry").unwrap_or(&Value::Null))
        ),
    ];
    let params: Vec<(&String, &Value)> = sh
        .get("parameters")
        .and_then(Value::as_object)
        .map(|m| m.iter().collect())
        .unwrap_or_default();
    if params.is_empty() {
        lines.push("Parameters:  -".to_owned());
    } else {
        lines.push("Parameters:".to_owned());
        let w = params
            .iter()
            .map(|(n, _)| n.chars().count())
            .max()
            .unwrap_or(0);
        let num = |p: &Value, k: &str| js_str(p.get(k).unwrap_or(&Value::Null));
        for (n, p) in params {
            let kind = match s(p, "type") {
                "integer" => format!("integer {} to {}", num(p, "min"), num(p, "max")),
                "enum" => format!(
                    "enum {}",
                    arr(p, "values")
                        .iter()
                        .filter_map(Value::as_str)
                        .collect::<Vec<_>>()
                        .join("|")
                ),
                _ => "boolean".to_owned(),
            };
            lines.push(format!(
                "  {n:<w$}  {kind}, default {}: {}",
                num(p, "default"),
                s(p, "description")
            ));
        }
    }
    stdout(format!("{}\n", lines.join("\n")).as_bytes());
    Ok(())
}

fn render_cmd(data: &Value, command: &Value, ids: &[String], flags: &Flags) -> Res<()> {
    let name = s(command, "name");
    let mut fmt = flag_str(flags, "format");
    if flag_on(flags, "layered") {
        if fmt.is_some_and(|f| f != "svg") {
            return Err(CliError::Usage(
                "--layered only works with --format svg".to_owned(),
                Some(name.to_owned()),
            ));
        }
        fmt = Some("svg-layered");
    }
    let fmt = port_format(data, fmt.unwrap_or("svg"))?;
    let items: Vec<Value> = ids.iter().map(|i| Value::from(i.as_str())).collect();
    let files = export_shapes(fmt, &items, &Value::Object(export_options(command, flags)))?;
    emit(
        name,
        files.into_iter().map(|f| (f.name, f.data)).collect(),
        flags,
    )
}

fn export_cmd(data: &Value, command: &Value, ids: &[String], flags: &Flags) -> Res<()> {
    let name = s(command, "name");
    let usage = |m: &str| CliError::Usage(m.to_owned(), Some(name.to_owned()));
    let every = flag_on(flags, "all");
    if every && !ids.is_empty() {
        return Err(usage("give shape ids or --all, not both"));
    }
    if !every && ids.is_empty() {
        return Err(usage("give shape ids or --all"));
    }
    let out = flag_str(flags, "out").unwrap_or(".");
    if out == "-" {
        return Err(usage("export writes files; --out - is not supported"));
    }
    let listed: Vec<&str> = flags.get("format").and_then(Value::as_array).map_or_else(
        || vec!["svg"],
        |a| a.iter().filter_map(Value::as_str).collect(),
    );
    let formats = listed
        .into_iter()
        .map(|f| port_format(data, f))
        .collect::<Res<Vec<_>>>()?;
    let items: Vec<Value> = if every {
        list_shapes(None)
            .iter()
            .map(|sh| Value::from(s(sh, "id")))
            .collect()
    } else {
        ids.iter().map(|i| Value::from(i.as_str())).collect()
    };
    let sizes: Vec<Value> = flags
        .get("sizes")
        .and_then(Value::as_array)
        .cloned()
        .unwrap_or_else(|| vec![Value::from(24)]);
    let base = export_options(command, flags);
    let mut targets = Vec::new();
    for fmt in formats {
        for size in &sizes {
            let mut o = base.clone();
            if let Some(Value::Object(r)) = o.get_mut("render") {
                r.insert("size".to_owned(), size.clone());
            }
            for f in export_shapes(fmt, &items, &Value::Object(o))? {
                targets.push(File::new(
                    join_path(out, &[fmt, &js_str(size), &f.name]),
                    f.data,
                ));
            }
        }
    }
    write_all(&targets, flag_on(flags, "force"))
}

/// The flag file for the `flag` command: its name and contents.
/// `svg` as a raster image `width` px wide, with currentColor made concrete.
fn raster(svg: &str, width: f64, fmt: &str, flags: &Flags, what: &str) -> Res<Vec<u8>> {
    let quality = flags.get("quality").and_then(Value::as_i64);
    let quality = match quality {
        Some(q) => match u8::try_from(q) {
            Ok(q) if q <= 100 => Some(q),
            _ => {
                let issue = format!("quality: must be 0 to 100 (got {q})");
                return Err(invalid_options(
                    format!("invalid {what} options: {issue}"),
                    issue,
                ));
            }
        },
        None => None,
    };
    let current = to_hex(parse_color(
        flag_str(flags, "current-color").unwrap_or("#000000"),
        BLACK,
    )?);
    let matte = parse_color(flag_str(flags, "matte").unwrap_or("#ffffff"), BLACK)?;
    let kind = match fmt {
        "png" => RasterFormat::Png,
        "jpg" => RasterFormat::Jpg,
        "webp" => RasterFormat::Webp,
        other => return Err(CliError::NodeOnly(other.to_owned())),
    };
    Ok(encode_svg(
        &svg.replace("currentColor", &current),
        width,
        kind,
        quality,
        matte,
    )?)
}

fn flag_file(flags: &Flags) -> Res<(String, Vec<u8>)> {
    let variant = flag_str(flags, "variant").unwrap_or("full-color");
    let width = flags.get("width").and_then(Value::as_i64).unwrap_or(900);
    let fmt = flag_str(flags, "format").unwrap_or("svg");
    let mut options = Map::new();
    options.insert("variant".to_owned(), Value::from(variant));
    options.insert("width".to_owned(), Value::from(width));
    if let Some(title) = flags.get("title") {
        options.insert("title".to_owned(), title.clone());
    }
    let svg = render_flag(&Value::Object(options))?;
    let name = format!("kurdish-flag-{variant}.{fmt}");
    if fmt == "svg" {
        return Ok((name, format!("{svg}\n").into_bytes()));
    }
    #[allow(clippy::cast_precision_loss)]
    let data = raster(&svg, width as f64, fmt, flags, "flag")?;
    Ok((name, data))
}

/// The `width` and `height` attributes of the root `<svg>` (24 when missing).
fn svg_size(svg: &str) -> (f64, f64) {
    let head = svg.split('>').next().unwrap_or("");
    let attr = |name: &str| {
        head.split_once(&format!(" {name}=\""))
            .and_then(|(_, rest)| rest.split('"').next())
            .and_then(|v| v.parse::<f64>().ok())
            .unwrap_or(24.0)
    };
    (attr("width"), attr("height"))
}

/// A scene (Leet text or pattern) as `<stem>.<format>`; images are as wide as the SVG in px.
fn scene_file(stem: &str, svg: &str, flags: &Flags) -> Res<(String, Vec<u8>)> {
    let fmt = flag_str(flags, "format").unwrap_or("svg");
    let name = format!("{stem}.{fmt}");
    if fmt == "svg" {
        return Ok((name, format!("{svg}\n").into_bytes()));
    }
    let (sw, sh) = svg_size(svg);
    let w = js_round(sw).max(1.0);
    let h = js_round(sh).max(1.0);
    if w > MAX_PX || h > MAX_PX {
        return Err(DeqError::new(
            ErrorCode::LimitExceeded,
            format!(
                "the image would be {w} x {h} px; the largest is {MAX_PX} x {MAX_PX} (lower --size)"
            ),
        )
        .into());
    }
    Ok((name, raster(svg, w, fmt, flags, "scene")?))
}

fn leet_cmd(command: &Value, text: Option<&String>, flags: &Flags) -> Res<()> {
    let usage = |m: String| CliError::Usage(m, Some("leet".to_owned()));
    if let Some(file) = flag_str(flags, "decode") {
        if text.is_some() {
            return Err(usage("give text or --decode, not both".to_owned()));
        }
        if let Some(bad) = NOT_WITH_DECODE.iter().find(|n| flags.contains_key(**n)) {
            return Err(usage(format!("--decode cannot be combined with --{bad}")));
        }
        let svg = read_text(file, MAX_DECODE_BYTES)?;
        let out = decode_leet(&svg, &Value::Object(render_options(command, flags)))?;
        if out.unmatched > 0 {
            stderr(&format!(
                "deq: warning: {} glyphs could not be read (shown as U+FFFD); pass the render flags used to write the file\n",
                out.unmatched
            ));
        }
        stdout(format!("{}\n", out.text).as_bytes());
        return Ok(());
    }
    let Some(text) = text else {
        return Err(usage("give text or --decode".to_owned()));
    };
    let r = leet(text, &Value::Object(render_options(command, flags)))?;
    if !r.unknown.is_empty() {
        let points: Vec<String> = r
            .unknown
            .iter()
            .filter_map(|c| c.chars().next())
            .map(|c| format!("U+{:04X}", u32::from(c)))
            .collect();
        stderr(&format!(
            "deq: warning: left out characters with no glyph: {}\n",
            points.join(", ")
        ));
    }
    emit("leet", vec![scene_file("leet", &r.svg, flags)?], flags)
}

fn generate_cmd(command: &Value, flags: &Flags) -> Res<()> {
    let r = pattern(&Value::Object(render_options(command, flags)))?;
    let kind = flag_str(flags, "kind").unwrap_or("band");
    let seed = flags.get("seed").and_then(Value::as_i64).unwrap_or(1);
    let stem = format!("pattern-{kind}-{seed}");
    emit("generate", vec![scene_file(&stem, &r.svg, flags)?], flags)
}

/// A regular file of at most `max_bytes` as UTF-8, refusing symbolic links.
fn read_text(file: &str, max_bytes: usize) -> Res<String> {
    let Ok(meta) = std::fs::symlink_metadata(file) else {
        return Err(CliError::Io(format!("cannot read {file}: not found")));
    };
    if meta.file_type().is_symlink() {
        return Err(CliError::Io(format!(
            "{file} is a symbolic link; pass the real file"
        )));
    }
    if !meta.is_file() {
        return Err(CliError::Io(format!("{file} is not a regular file")));
    }
    if meta.len() > max_bytes as u64 {
        return Err(CliError::Io(format!(
            "{file} is larger than {} KB",
            max_bytes / 1024
        )));
    }
    let mut bytes = Vec::new();
    std::fs::File::open(file)
        .and_then(|fh| fh.take(max_bytes as u64 + 1).read_to_end(&mut bytes))
        .map_err(|e| CliError::Io(format!("cannot read {file}: {}", errno(&e))))?;
    Ok(String::from_utf8_lossy(&bytes).into_owned())
}

fn validate(file: &str) -> Res<()> {
    let text = read_text(file, MAX_SPEC_BYTES)?;
    let value: Value = serde_json::from_str(&text)
        .map_err(|_| DeqError::new(ErrorCode::InvalidShape, format!("{file}: not valid JSON")))?;
    let (shape_id, count) = validate_spec(&value).map_err(|e| DeqError {
        code: e.code,
        message: format!("{file}: {}", e.message),
        issues: e.issues,
    })?;
    stdout(format!("{file}: ok ({shape_id}, {count} variants rendered)\n").as_bytes());
    Ok(())
}

fn dispatch(data: &Value, line: &[String]) -> Res<()> {
    let (command, args, flags) = match parse_args(data, line)? {
        Parsed::Text(text) => {
            stdout(text.as_bytes());
            return Ok(());
        }
        Parsed::Run {
            command,
            args,
            flags,
        } => (command, args, flags),
    };
    let first = args.first().map_or("", String::as_str);
    match s(command, "name") {
        "list" => list(&flags),
        "info" => info(first, &flags),
        "render" => render_cmd(data, command, &args, &flags),
        "export" => export_cmd(data, command, &args, &flags),
        "flag" => emit("flag", vec![flag_file(&flags)?], &flags),
        "leet" => leet_cmd(command, args.first(), &flags),
        "generate" => generate_cmd(command, &flags),
        "validate" => validate(first),
        _ => {
            stdout(
                data.get("completions")
                    .map_or("", |c| s(c, first))
                    .as_bytes(),
            );
            Ok(())
        }
    }
}

/// Run the CLI and return the exit code.
fn run(argv: &[String]) -> u8 {
    let data: Value = match serde_json::from_str(include_str!("../data/cli.json")) {
        Ok(d) => d,
        Err(e) => {
            stderr(&format!("deq: error: broken CLI data: {e}\n"));
            return 1;
        }
    };
    let name = s(&data, "name");
    match dispatch(&data, argv) {
        Ok(()) => 0,
        Err(CliError::Usage(msg, command)) => {
            let target = command.map_or_else(|| name.to_owned(), |c| format!("{name} {c}"));
            stderr(&format!(
                "{name}: error: {msg}\nRun \"{target} --help\" for usage.\n"
            ));
            2
        }
        Err(e) => {
            stderr(&format!("{name}: error: {e}\n"));
            match e {
                CliError::Io(_) => 3,
                CliError::NodeOnly(_) => EXIT_NODE_ONLY,
                _ => 1,
            }
        }
    }
}

fn main() -> ExitCode {
    let argv: Vec<String> = std::env::args_os()
        .skip(1)
        .map(|a| a.to_string_lossy().into_owned())
        .collect();
    ExitCode::from(run(&argv))
}