rubric/dropbox/
fingerprint.rs

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
//! A set of data to verify a Submission's validity

// std uses
use std::env;

// external uses
use serde::{Serialize, Deserialize};

// internal uses
use crate::dropbox::AsCsv;


/// A set of data to verify a Submission. This struct contains some system data, and
/// some data provided by the instructor.
/// 
/// You shouldn't create one of these directly, instead add a fingerprint to a submission
/// with [`Submission::set_fingerprint`](crate::dropbox::submission::Submission::set_fingerprint).
#[derive(Debug, Serialize, Deserialize, PartialEq, PartialOrd)]
pub struct Fingerprint {
    /// Any random string
    pub secret: String,
    pub platform: String
}

impl Fingerprint {

    /// Creates a new fingerprint from a secret string
    pub fn from_secret(secret: &str) -> Self {
        Fingerprint {
            secret: String::from(secret),
            platform: String::from(env::consts::OS)
        }
    }
}

impl AsCsv for Fingerprint {
    fn as_csv(&self) -> String {
        format!("{},{}", self.secret, self.platform)
    }

    fn filename(&self) -> String {
        format!("fingerprint.csv")
    }

    fn header(&self) -> String {
        format!("secret,platform")
    }

}



#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_new_fingerprint() {
        let fp = Fingerprint::from_secret("my secret key");
        assert_eq!(fp.secret, "my secret key");
        assert!(fp.platform.len() > 0);
    }

    #[test]
    fn test_as_csv() {
        let fp = Fingerprint::from_secret("my_secret");
        assert!(fp.header().contains(","));
        assert!(fp.header().len() > 0);
        assert!(fp.as_csv().contains("my_secret"));
        if cfg!(target_os = "windows") {
            assert!(fp.as_csv().contains("windows"));
        }
    }
}