Skip to main content

crev_lib/
activity.rs

1//! Activities track user actions to help verified
2//! multi-step flows, and confirm user intention.
3//!
4//! Eg. when user reviews a package we record details
5//! and  we  can warn them if they attempt to create
6//! a proof review which they haven't previously reviewed.
7use crev_common::{
8    self,
9    serde::{as_rfc3339_fixed, from_rfc3339_fixed},
10};
11use crev_data::Version;
12use serde::{Deserialize, Serialize};
13
14pub type Date = chrono::DateTime<chrono::FixedOffset>;
15
16#[derive(Copy, Clone, Debug, PartialEq, Eq)]
17pub enum ReviewMode {
18    Full,
19    Differential,
20}
21
22impl ReviewMode {
23    #[must_use]
24    pub fn is_diff(self) -> bool {
25        self == ReviewMode::Differential
26    }
27
28    #[must_use]
29    pub fn is_full(self) -> bool {
30        self == ReviewMode::Full
31    }
32
33    #[must_use]
34    pub fn from_diff_flag(diff: bool) -> Self {
35        if diff {
36            ReviewMode::Differential
37        } else {
38            ReviewMode::Full
39        }
40    }
41}
42
43/// Which review is the most recent
44#[derive(Debug, Clone, Serialize, Deserialize)]
45pub struct LatestReviewActivity {
46    pub source: String,
47    pub name: String,
48    pub version: Version,
49    pub diff_base: Option<Version>,
50}
51
52/// Record of an in-progress review
53#[derive(Debug, Clone, Serialize, Deserialize)]
54pub struct ReviewActivity {
55    #[serde(
56        serialize_with = "as_rfc3339_fixed",
57        deserialize_with = "from_rfc3339_fixed"
58    )]
59    pub timestamp: Date,
60    pub diff_base: Option<Version>,
61}
62
63impl ReviewActivity {
64    #[must_use]
65    pub fn new(diff_base: Option<Version>) -> Self {
66        Self {
67            timestamp: crev_common::now(),
68            diff_base,
69        }
70    }
71
72    #[must_use]
73    pub fn to_review_mode(&self) -> ReviewMode {
74        if self.diff_base.is_some() {
75            ReviewMode::Differential
76        } else {
77            ReviewMode::Full
78        }
79    }
80}