biscuit-cli 0.6.0

a CLI to manipulate biscuit tokens
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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
/*
 * SPDX-FileCopyrightText: 2021 Clément Delafargue <clement@delafargue.name>
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */
use anyhow::{bail, Result};
use atty::Stream;
use biscuit_auth::{
    builder::{BiscuitBuilder, BlockBuilder, Rule, Term},
    Authorizer, AuthorizerBuilder, PrivateKey, PublicKey, ThirdPartyRequest, UnverifiedBiscuit,
};
use chrono::{DateTime, Duration, Utc};
use clap::{PossibleValue, ValueEnum};
use parse_duration as duration_parser;
use std::fs;
use std::io::{self, Read};
use std::path::PathBuf;
use std::process::Command;
use std::{collections::HashMap, convert::TryInto};
use std::{env, fmt::Display};

use crate::errors::CliError::*;

pub enum BiscuitFormat {
    RawBiscuit,
    Base64Biscuit,
}

#[derive(PartialEq, Clone, Copy, Debug, ValueEnum)]
pub enum KeyFormat {
    Raw,
    Hex,
    Pem,
}

impl Default for KeyFormat {
    fn default() -> Self {
        Self::Hex
    }
}

#[derive(PartialEq, Clone, Copy, Debug, Default)]
pub struct Algorithm(pub biscuit_auth::Algorithm);

impl ValueEnum for Algorithm {
    fn value_variants<'a>() -> &'a [Self] {
        &[
            Self(biscuit_auth::Algorithm::Ed25519),
            Self(biscuit_auth::Algorithm::Secp256r1),
        ]
    }

    fn to_possible_value<'a>(&self) -> Option<clap::PossibleValue<'a>> {
        Some(PossibleValue::new(match self.0 {
            biscuit_auth::Algorithm::Ed25519 => "ed25519",
            biscuit_auth::Algorithm::Secp256r1 => "secp256r1",
        }))
    }
}

impl Display for Algorithm {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.0.fmt(f)
    }
}

// `Base64String` is never constructed, but is still handled in
// `cli.rs`
#[allow(dead_code)]
pub enum BiscuitBytes {
    FromStdin(BiscuitFormat),
    FromFile(BiscuitFormat, PathBuf),
    Base64String(String),
}

// `FromStdin` is never constructed, but is still handled in
// `cli.rs`
#[allow(dead_code)]
pub enum KeyBytes {
    FromStdin(KeyFormat),
    FromFile(KeyFormat, PathBuf),
    HexString(String),
    PemString(String),
}

pub enum DatalogInput {
    FromEditor,
    FromStdin,
    FromFile(PathBuf),
    DatalogString(String),
}

pub enum AuthorizerInput {
    FromDatalog(DatalogInput, Vec<Param>),
    FromSnapshot(SnapshotInput),
}

pub enum SnapshotInput {
    FromFile(PathBuf, BiscuitFormat),
    FromString(String),
}

pub fn ensure_no_input_conflict(datalog: &DatalogInput, biscuit: &BiscuitBytes) -> Result<()> {
    match (datalog, biscuit) {
        // running $EDITOR as a child process requires a working stdin. When contents from stdin has already been read, this is
        // not the case. This could be handled by reopening stdin on /dev/tty, but it's not portable (and as such, more complicated
        // to do in rust than just disallowing a fringe use-case)
        (DatalogInput::FromEditor, BiscuitBytes::FromStdin(_)) => Err(StdinEditorConflict)?,
        // this combination should be prevented by the clap configuration
        (DatalogInput::FromStdin, BiscuitBytes::FromStdin(_)) => Err(MultipleStdinsConflict)?,
        _ => Ok(()),
    }
}

pub fn ensure_no_input_conflict_third_party(
    block: &BiscuitBytes,
    biscuit: &BiscuitBytes,
) -> Result<()> {
    match (block, biscuit) {
        (BiscuitBytes::FromStdin(_), BiscuitBytes::FromStdin(_)) => Err(MultipleStdinsConflict)?,
        _ => Ok(()),
    }
}

pub fn read_stdin_string(desc: &str) -> Result<String> {
    if atty::is(Stream::Stdin) && atty::is(Stream::Stderr) {
        eprintln!("Please input a {}, followed by <enter> and ^D", &desc);
    }
    let mut buffer = String::new();
    io::stdin().read_to_string(&mut buffer)?;
    Ok(buffer.trim().to_owned())
}

pub fn read_stdin_bytes() -> Result<Vec<u8>> {
    if atty::is(Stream::Stdin) {
        Err(BinaryFromTTY)?
    }
    let mut buffer = Vec::new();
    io::stdin().read_to_end(&mut buffer).map(|_| ())?;
    Ok(buffer)
}

pub fn read_editor_string() -> Result<String> {
    let file = tempfile::Builder::new()
        .suffix(".biscuit-datalog")
        .tempfile()?;
    let path = &file.path();

    if atty::isnt(Stream::Stdin) || atty::isnt(Stream::Stdout) {
        Err(EditorOutsideTTY)?
    }

    let result = get_editor_command()?.arg(path).spawn()?.wait()?;

    if result.success() {
        Ok(fs::read_to_string(path).map_err(|_| FileNotFound(path.to_path_buf()))?)
    } else {
        Err(FailedReadingTempFile)?
    }
}

pub fn get_editor_command() -> Result<Command> {
    let editor_unparsed = match env::var("EDITOR") {
        Ok(e) => Ok(e),
        Err(env::VarError::NotPresent) => Ok("vim".to_owned()),
        e => e,
    }?;

    let editor_parts = shell_words::split(&editor_unparsed)?;
    match editor_parts.split_first() {
        Some((editor_binary, editor_args)) => {
            let mut editor_cmd = Command::new(editor_binary);
            editor_cmd.args(editor_args);
            Ok(editor_cmd)
        }
        None => Err(FailedParsingEditorEnvVar)?,
    }
}

pub fn read_authority_from(
    from: &DatalogInput,
    all_params: &[Param],
    context: &Option<String>,
    builder: BiscuitBuilder,
) -> Result<BiscuitBuilder> {
    let string = match from {
        DatalogInput::FromEditor => read_editor_string()?,
        DatalogInput::FromStdin => read_stdin_string("datalog program")?,
        DatalogInput::FromFile(f) => fs::read_to_string(f)?,
        DatalogInput::DatalogString(str) => str.to_owned(),
    };

    let mut params = HashMap::new();
    let mut scope_params = HashMap::new();
    for p in all_params {
        match p {
            Param::Term(name, t) => {
                params.insert(name.clone(), t.clone());
            }
            Param::PublicKey(name, pk) => {
                scope_params.insert(name.clone(), *pk);
            }
        }
    }

    let mut builder = builder
        .code_with_params(&string, params, scope_params)
        .map_err(|e| ParseError("datalog statements".to_string(), e.to_string()))?;
    if let Some(ctx) = context {
        builder = builder.context(ctx.to_owned());
    }

    Ok(builder)
}

pub fn read_block_from(
    from: &DatalogInput,
    all_params: &[Param],
    context: &Option<String>,
    builder: BlockBuilder,
) -> Result<BlockBuilder> {
    let string = match from {
        DatalogInput::FromEditor => read_editor_string()?,
        DatalogInput::FromStdin => read_stdin_string("datalog program")?,
        DatalogInput::FromFile(f) => fs::read_to_string(f)?,
        DatalogInput::DatalogString(str) => str.to_owned(),
    };

    let mut params = HashMap::new();
    let mut scope_params = HashMap::new();
    for p in all_params {
        match p {
            Param::Term(name, t) => {
                params.insert(name.clone(), t.clone());
            }
            Param::PublicKey(name, pk) => {
                scope_params.insert(name.clone(), *pk);
            }
        }
    }
    let mut builder = builder
        .code_with_params(&string, params, scope_params)
        .map_err(|e| ParseError("datalog statements".to_string(), e.to_string()))?;

    if let Some(ctx) = context {
        builder = builder.context(ctx.to_owned());
    }

    Ok(builder)
}

pub fn read_authorizer_from(from: &AuthorizerInput) -> Result<AuthorizerBuilder> {
    match from {
        AuthorizerInput::FromDatalog(datalog, all_params) => {
            read_authorizer_from_datalog(datalog, all_params)
        }
        AuthorizerInput::FromSnapshot(snapshot) => read_authorizer_from_snapshot(snapshot),
    }
}

pub fn read_authorizer_from_datalog(
    from: &DatalogInput,
    all_params: &[Param],
) -> Result<AuthorizerBuilder> {
    let string = match from {
        DatalogInput::FromEditor => read_editor_string()?,
        DatalogInput::FromStdin => read_stdin_string("datalog program")?,
        DatalogInput::FromFile(f) => fs::read_to_string(f)?,
        DatalogInput::DatalogString(str) => str.to_owned(),
    };

    let mut params = HashMap::new();
    let mut scope_params = HashMap::new();
    for p in all_params {
        match p {
            Param::Term(name, t) => {
                params.insert(name.clone(), t.clone());
            }
            Param::PublicKey(name, pk) => {
                scope_params.insert(name.clone(), *pk);
            }
        }
    }
    let builder = AuthorizerBuilder::new()
        .code_with_params(&string, params, scope_params)
        .map_err(|e| ParseError("datalog statements".to_string(), e.to_string()))?;

    Ok(builder)
}

fn read_authorizer_from_snapshot(
    snapshot: &SnapshotInput,
) -> std::result::Result<AuthorizerBuilder, anyhow::Error> {
    let builder = match snapshot {
        SnapshotInput::FromFile(path, BiscuitFormat::RawBiscuit) => {
            AuthorizerBuilder::from_raw_snapshot(
                &fs::read(path).map_err(|_| FileNotFound(path.clone()))?,
            )?
        }
        SnapshotInput::FromFile(path, BiscuitFormat::Base64Biscuit) => {
            AuthorizerBuilder::from_base64_snapshot(
                fs::read_to_string(path)
                    .map_err(|_| FileNotFound(path.clone()))?
                    .trim(),
            )?
        }
        SnapshotInput::FromString(str) => AuthorizerBuilder::from_base64_snapshot(str)?,
    };
    Ok(builder)
}

fn read_pem_private_key(str: &str, alg: &Option<Algorithm>) -> Result<PrivateKey> {
    Ok(match alg {
        Some(alg) => PrivateKey::from_pem_with_algorithm(str, alg.0),
        None => PrivateKey::from_pem(str),
    }?)
}

pub fn read_private_key_from(from: &KeyBytes, alg: &Option<Algorithm>) -> Result<PrivateKey> {
    let key = match (from, alg) {
        (KeyBytes::FromStdin(KeyFormat::Raw), Some(alg)) => {
            let bytes = read_stdin_bytes()?;
            PrivateKey::from_bytes(&bytes, alg.0)
                .map_err(|e| ParseError("private key".to_string(), format!("{}", &e)))?
        }
        (KeyBytes::FromStdin(KeyFormat::Hex), None) => {
            let str = read_stdin_string("hex-encoded private key")?;
            str.parse()
                .map_err(|e| ParseError("private key".to_string(), format!("{}", &e)))?
        }
        (KeyBytes::FromStdin(KeyFormat::Pem), None) => {
            let str = read_stdin_string("PEM private key")?;
            read_pem_private_key(&str, alg)
                .map_err(|e| ParseError("private key".to_string(), format!("{}", &e)))?
        }
        (KeyBytes::FromFile(KeyFormat::Raw, path), Some(alg)) => {
            let bytes = fs::read(path).map_err(|_| FileNotFound(path.clone()))?;
            PrivateKey::from_bytes(&bytes, alg.0)
                .map_err(|e| ParseError("private key".to_string(), format!("{}", &e)))?
        }
        (KeyBytes::FromFile(KeyFormat::Hex, path), None) => {
            let str = fs::read_to_string(path).map_err(|e| match e.kind() {
                io::ErrorKind::NotFound => FileNotFound(path.clone()),
                _ => FileError(e),
            })?;
            str.trim()
                .parse()
                .map_err(|e| ParseError("private key".to_string(), format!("{}", &e)))?
        }
        (KeyBytes::FromFile(KeyFormat::Pem, path), None) => {
            let str = fs::read_to_string(path).map_err(|_| FileNotFound(path.clone()))?;
            read_pem_private_key(&str, alg)
                .map_err(|e| ParseError("private key".to_string(), format!("{}", &e)))?
        }
        (KeyBytes::HexString(str), None) => str
            .parse()
            .map_err(|e| ParseError("private key".to_string(), format!("{}", &e)))?,
        (KeyBytes::PemString(str), None) => read_pem_private_key(str, alg)
            .map_err(|e| ParseError("private key".to_string(), format!("{}", &e)))?,
        (KeyBytes::FromStdin(KeyFormat::Raw), None)
        | (KeyBytes::FromFile(KeyFormat::Raw, _), None) => {
            bail!("Raw private key binary input requires an explicit key algorithm")
        }
        (_, Some(_)) => {
            bail!("The private key algorithm must only be set when reading raw binary input")
        }
    };
    let key_alg = key.algorithm().into();

    if let Some(a) = alg {
        if a.0 != key_alg {
            Err(std::io::Error::other(format!(
                "Inconsistent algorithm: key algorithm is {}, expected algorithm is {}",
                key_alg, a
            )))?
        }
    }

    Ok(key)
}

fn read_pem_public_key(str: &str, alg: &Option<Algorithm>) -> Result<PublicKey> {
    Ok(match alg {
        Some(alg) => PublicKey::from_pem_with_algorithm(str, alg.0),
        None => PublicKey::from_pem(str),
    }?)
}

pub fn read_public_key_from(from: &KeyBytes, alg: &Option<Algorithm>) -> Result<PublicKey> {
    let key = match (from, alg) {
        (KeyBytes::FromStdin(KeyFormat::Raw), Some(alg)) => {
            let bytes = read_stdin_bytes()?;
            PublicKey::from_bytes(&bytes, alg.0)
                .map_err(|e| ParseError("public key".to_string(), format!("{}", &e)))?
        }
        (KeyBytes::FromStdin(KeyFormat::Hex), None) => {
            let str = read_stdin_string("hex-encoded public key")?;
            str.parse()
                .map_err(|e| ParseError("public key".to_string(), format!("{}", &e)))?
        }
        (KeyBytes::FromStdin(KeyFormat::Pem), None) => {
            let str = read_stdin_string("PEM public key")?;
            read_pem_public_key(&str, alg)
                .map_err(|e| ParseError("public key".to_string(), format!("{}", &e)))?
        }
        (KeyBytes::FromFile(KeyFormat::Raw, path), Some(alg)) => {
            let bytes = fs::read(path).map_err(|_| FileNotFound(path.clone()))?;
            PublicKey::from_bytes(&bytes, alg.0)
                .map_err(|e| ParseError("public key".to_string(), format!("{}", &e)))?
        }
        (KeyBytes::FromFile(KeyFormat::Hex, path), None) => {
            let str = fs::read_to_string(path).map_err(|_| FileNotFound(path.clone()))?;
            str.trim()
                .parse()
                .map_err(|e| ParseError("public key".to_string(), format!("{}", &e)))?
        }
        (KeyBytes::FromFile(KeyFormat::Pem, path), None) => {
            let str = fs::read_to_string(path).map_err(|_| FileNotFound(path.clone()))?;
            read_pem_public_key(&str, alg)
                .map_err(|e| ParseError("public key".to_string(), format!("{}", &e)))?
        }
        (KeyBytes::HexString(str), None) => str
            .parse()
            .map_err(|e| ParseError("public key".to_string(), format!("{}", &e)))?,
        (KeyBytes::PemString(str), None) => read_pem_public_key(str, alg)
            .map_err(|e| ParseError("public key".to_string(), format!("{}", &e)))?,
        (KeyBytes::FromStdin(KeyFormat::Raw), None)
        | (KeyBytes::FromFile(KeyFormat::Raw, _), None) => {
            bail!("Raw public key binary input requires an explicit key algorithm")
        }
        (_, Some(_)) => {
            bail!("The public key algorithm must only be set when reading raw binary input")
        }
    };
    let key_alg = key.algorithm().into();

    if let Some(a) = alg {
        if a.0 != key_alg {
            Err(std::io::Error::other(format!(
                "Inconsistent algorithm: key algorithm is {}, expected algorithm is {}",
                key_alg, a
            )))?
        }
    }

    Ok(key)
}

pub fn read_biscuit_from(from: &BiscuitBytes) -> Result<UnverifiedBiscuit> {
    let b = match from {
        BiscuitBytes::FromStdin(BiscuitFormat::RawBiscuit) => {
            UnverifiedBiscuit::from(read_stdin_bytes()?)?
        }
        BiscuitBytes::FromStdin(BiscuitFormat::Base64Biscuit) => {
            UnverifiedBiscuit::from_base64(read_stdin_string("base64-encoded biscuit")?)?
        }
        BiscuitBytes::FromFile(BiscuitFormat::RawBiscuit, path) => {
            UnverifiedBiscuit::from(fs::read(path).map_err(|_| FileNotFound(path.clone()))?)?
        }
        BiscuitBytes::FromFile(BiscuitFormat::Base64Biscuit, path) => {
            UnverifiedBiscuit::from_base64(
                fs::read_to_string(path)
                    .map_err(|_| FileNotFound(path.clone()))?
                    .trim(),
            )?
        }
        BiscuitBytes::Base64String(str) => UnverifiedBiscuit::from_base64(str)?,
    };
    Ok(b)
}

pub fn read_request_from(from: &BiscuitBytes) -> Result<ThirdPartyRequest> {
    let req = match from {
        BiscuitBytes::FromStdin(BiscuitFormat::RawBiscuit) => {
            ThirdPartyRequest::deserialize(&read_stdin_bytes()?)?
        }
        BiscuitBytes::FromStdin(BiscuitFormat::Base64Biscuit) => {
            ThirdPartyRequest::deserialize_base64(read_stdin_string(
                "base64-encoded third-party block request",
            )?)?
        }
        BiscuitBytes::FromFile(BiscuitFormat::RawBiscuit, path) => ThirdPartyRequest::deserialize(
            &fs::read(path).map_err(|_| FileNotFound(path.clone()))?,
        )?,
        BiscuitBytes::FromFile(BiscuitFormat::Base64Biscuit, path) => {
            ThirdPartyRequest::deserialize_base64(
                fs::read_to_string(path)
                    .map_err(|_| FileNotFound(path.clone()))?
                    .trim(),
            )?
        }
        BiscuitBytes::Base64String(str) => ThirdPartyRequest::deserialize_base64(str)?,
    };
    Ok(req)
}

pub fn read_snapshot_from(from: &BiscuitBytes) -> Result<Authorizer> {
    let b = match from {
        BiscuitBytes::FromStdin(BiscuitFormat::RawBiscuit) => {
            Authorizer::from_raw_snapshot(&read_stdin_bytes()?)?
        }
        BiscuitBytes::FromStdin(BiscuitFormat::Base64Biscuit) => {
            Authorizer::from_base64_snapshot(&read_stdin_string("base64-encoded biscuit")?)?
        }
        BiscuitBytes::FromFile(BiscuitFormat::RawBiscuit, path) => {
            Authorizer::from_raw_snapshot(&fs::read(path).map_err(|_| FileNotFound(path.clone()))?)?
        }
        BiscuitBytes::FromFile(BiscuitFormat::Base64Biscuit, path) => {
            Authorizer::from_base64_snapshot(
                fs::read_to_string(path)
                    .map_err(|_| FileNotFound(path.clone()))?
                    .trim(),
            )?
        }
        BiscuitBytes::Base64String(str) => Authorizer::from_base64_snapshot(str)?,
    };
    Ok(b)
}

pub fn append_third_party_from(
    biscuit: &UnverifiedBiscuit,
    from: &BiscuitBytes,
) -> Result<UnverifiedBiscuit> {
    let b = match from {
        BiscuitBytes::FromStdin(BiscuitFormat::RawBiscuit) => {
            biscuit.append_third_party(&read_stdin_bytes()?)?
        }
        BiscuitBytes::FromStdin(BiscuitFormat::Base64Biscuit) => biscuit
            .append_third_party_base64(read_stdin_string("base64-encode third-party block")?)?,
        BiscuitBytes::FromFile(BiscuitFormat::RawBiscuit, path) => {
            biscuit.append_third_party(&fs::read(path).map_err(|_| FileNotFound(path.clone()))?)?
        }
        BiscuitBytes::FromFile(BiscuitFormat::Base64Biscuit, path) => biscuit
            .append_third_party_base64(
                fs::read_to_string(path)
                    .map_err(|_| FileNotFound(path.clone()))?
                    .trim(),
            )?,
        BiscuitBytes::Base64String(str) => biscuit.append_third_party_base64(str)?,
    };
    Ok(b)
}

#[derive(Debug)]
pub enum Ttl {
    Duration(Duration),
    DateTime(DateTime<Utc>),
}

impl Ttl {
    pub fn to_datetime(&self) -> DateTime<Utc> {
        match self {
            Self::Duration(d) => Utc::now() + *d,
            Self::DateTime(d) => *d,
        }
    }
}

pub fn parse_duration(str: &str) -> Result<Duration> {
    let std_duration = duration_parser::parse(str)
        .map_err(|e| ParseError("duration".to_string(), e.to_string()))?;
    let duration = Duration::from_std(std_duration).map_err(|_| InvalidDuration)?;
    Ok(duration)
}

pub fn parse_date(str: &str) -> Result<DateTime<Utc>> {
    let r = DateTime::parse_from_rfc3339(str)?;
    Ok(r.into())
}

pub fn parse_ttl(str: &str) -> Result<Ttl> {
    parse_date(str)
        .map(Ttl::DateTime)
        .or_else(|_| parse_duration(str).map(Ttl::Duration))
}

#[derive(Debug, Clone)]
pub enum Param {
    Term(String, Term),
    PublicKey(String, PublicKey),
}

pub fn parse_param(kv: &str) -> Result<Param, std::io::Error> {
    use std::io::{Error, ErrorKind};
    let (binding, value) = (kv.split_once('=').ok_or_else(|| Error::new(
        ErrorKind::Other,
        "Params must be `key=value` or `key:type=value` where type is pubkey, string, integer, date, bytes or bool.",
    )))?;

    let (name, annotation) = match binding.rsplit_once(':') {
        None => (binding, None),
        Some((name, annotation)) => (name, Some(annotation)),
    };

    match annotation {
      Some("pubkey") => {
        let pubkey = value.parse().map_err(Error::other)?;
        Ok(Param::PublicKey(name.to_string(), pubkey))
      },
      Some("integer") => {
        let int = value
            .parse()
            .map_err(|e| Error::new(ErrorKind::Other, format!("{}", &e)))?;
        Ok(Param::Term(name.to_string(), Term::Integer(int)))
      },
      Some("date") => {
        let date =
            time::OffsetDateTime::parse(value, &time::format_description::well_known::Rfc3339)
                .map_err(|e| Error::new(ErrorKind::Other, format!("{}", &e)))?;
        let timestamp = date
            .unix_timestamp()
            .try_into()
            .map_err(|e| Error::new(ErrorKind::Other, format!("{}", &e)))?;
        Ok(Param::Term(name.to_string(), Term::Date(timestamp)))
      },
      Some("bytes") => {
        let hex_bytes = value.strip_prefix("hex:").ok_or_else(|| {
            Error::new(
        ErrorKind::Other,
        "Unusupported byte array literal. Byte arrays must be hex-encoded and start with `hex:`."
        )
        })?;
        let bytes =
            hex::decode(hex_bytes).map_err(|e| Error::new(ErrorKind::Other, format!("{}", &e)))?;
        Ok(Param::Term(name.to_string(), Term::Bytes(bytes)))
      },
      Some("bool") => {
        if value.to_lowercase() == "true" {
            Ok(Param::Term(name.to_string(), Term::Bool(true)))
        } else if value.to_lowercase() == "false" {
            Ok(Param::Term(name.to_string(), Term::Bool(false)))
        } else {
            Err(Error::new(
                ErrorKind::Other,
                "Boolean params must be either \"true\" or \"false\".",
            ))
        }
      },
      Some("string") | None => {
        Ok(Param::Term(name.to_string(), Term::Str(value.to_string())))
      },
      _ => {
        Err(Error::new(
                ErrorKind::Other,
                "Unsupported parameter type. Supported types are `pubkey`, `string`, `integer`, `date`, `bytes`, or `bool`.",
            ))
      }
    }
}

pub fn parse_rule(rule: &str) -> Result<Rule, std::io::Error> {
    use std::io::{Error, ErrorKind};
    rule.try_into()
        .map_err(|e| Error::new(ErrorKind::Other, format!("Could not parse rule: {e}")))
}