firefly_types/
meta.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
use crate::Encode;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq)]
pub struct Meta<'a> {
    pub app_id: &'a str,
    /// App name is shown in the launcher in the list of apps.
    pub app_name: &'a str,
    pub author_id: &'a str,
    pub author_name: &'a str,
    /// Launcher is the app that starts first when runtime is launched.
    pub launcher: bool,
    /// Let the app to use privileged and dangerous runtime API.
    pub sudo: bool,
    /// The ever-incrementing version number of the app build.
    /// Used by netplay to ensure both devices running the same version.
    pub version: u32,
}

impl<'a> Encode<'a> for Meta<'a> {}

/// A struct with only a few safe fields from Meta.
///
/// It is used to serialize information about an app outside of the app dir.
/// Since it is outside, it escapes the hash and signature checks
/// and so it must not store any information that must be verified before use.
#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq)]
#[allow(clippy::module_name_repetitions)]
pub struct ShortMeta<'a> {
    pub app_id: &'a str,
    pub author_id: &'a str,
}

impl<'a> Encode<'a> for ShortMeta<'a> {}

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

    #[test]
    fn test_meta_roundtrip() {
        let given = Meta {
            app_id: "some-app-id",
            app_name: "Some App Name",
            author_id: "some-author-id",
            author_name: "Some Author Name",
            launcher: false,
            sudo: false,
            version: 12,
        };
        let mut buf = vec![0; given.size()];
        let raw = given.encode(&mut buf).unwrap();
        let actual = Meta::decode(raw).unwrap();
        assert_eq!(given, actual);
    }

    #[test]
    fn test_short_meta_roundtrip() {
        let given = ShortMeta {
            app_id: "some-app-id",
            author_id: "some-author-id",
        };
        let mut buf = vec![0; given.size()];
        let raw = given.encode(&mut buf).unwrap();
        let actual = ShortMeta::decode(raw).unwrap();
        assert_eq!(given, actual);
    }
}