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
//! **The offer's replies** (yog's `docs/REMOTE.md` §15.1; DESIGN §4.51):
//! what `offer` answers, and one row of what `offers` lists. `approve` and
//! `decline` answer a bare receipt each and carry nothing to read.
//!
//! None of it is a secret, which is the point of the offer: the URL is a
//! one-time code and the word is a check, so every field here is painted,
//! printed and logged like any other reply.
use serde_json::{Map, Value};
use super::fields;
/// `offer`'s reply kind.
pub(crate) const OFFERED: &str = "offered";
/// `offers`' reply kind.
pub(crate) const KIND: &str = "offers";
/// `approve`'s and `decline`'s.
pub(crate) const APPROVED: &str = "approved";
pub(crate) const DECLINED: &str = "declined";
/// The state only which an offer can be approved in: its device has dialled.
pub const PROVED: &str = "proved";
/// A minted offer: the URL the new device opens, and when its code expires.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Offered {
pub url: String,
/// Unix seconds.
pub expires: i64,
}
/// One pending offer.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OfferRow {
pub name: String,
pub grade: String,
/// `open`, `claimed` or `proved` — or a word this build has not met,
/// which rides verbatim and can only make a row unapprovable.
pub state: String,
/// Unix seconds: when the code expires. It bounds an open offer only.
pub expires: i64,
/// The confirmation word, absent until a device has claimed.
pub word: Option<String>,
}
impl OfferRow {
/// Whether `approve` would be accepted: the device has dialled.
pub fn proved(&self) -> bool {
self.state == PROVED
}
/// The row as one line: who, what grade, where it stands, and the word.
pub fn line(&self) -> String {
let word = self
.word
.as_ref()
.map_or_else(String::new, |word| format!(" word {word}"));
format!("{} {} {}{word}", self.name, self.grade, self.state)
}
}
pub(crate) fn offered(obj: &Map<String, Value>) -> Result<Offered, String> {
Ok(Offered {
url: fields::text(obj, "url")?,
expires: fields::secs(obj, "expires")?,
})
}
pub(crate) fn row(value: &Value) -> Result<OfferRow, String> {
let obj = value.as_object().ok_or("offer: not a JSON object")?;
Ok(OfferRow {
name: fields::text(obj, "name")?,
grade: fields::text(obj, "grade")?,
state: fields::text(obj, "state")?,
expires: fields::secs(obj, "expires")?,
word: fields::opt_text(obj, "word")?,
})
}
/// **A moment as a clock reads it**: `HH:MM UTC`, for an expiry ten minutes
/// out. The date is left off on purpose — a code's whole life is shorter
/// than the minute hand's lap.
pub fn clock(unix: i64) -> String {
let minutes = unix.div_euclid(60);
format!(
"{:02}:{:02} UTC",
minutes.div_euclid(60).rem_euclid(24),
minutes.rem_euclid(60)
)
}
#[cfg(test)]
mod tests;