automatons_github/resource/
installation.rs

1use std::fmt::{Display, Formatter};
2
3use serde::{Deserialize, Serialize};
4
5use crate::id;
6use crate::resource::NodeId;
7
8id!(
9    /// Installation id
10    ///
11    /// The [`InstallationId`] is a unique, numerical id that is used to interact with an
12    /// installation through [GitHub's REST API](https://docs.github.com/en/rest).
13    InstallationId
14);
15
16/// App installation
17///
18/// When a user adds a GitHub App to an account, a new app installation is created. The installation
19/// id can be used by the app to request a scoped access token that allows it to interact with the
20/// resources of the account.
21#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Deserialize, Serialize)]
22pub struct Installation {
23    id: InstallationId,
24    node_id: NodeId,
25}
26
27impl Installation {
28    /// Returns the installation's id.
29    #[cfg_attr(feature = "tracing", tracing::instrument)]
30    pub fn id(&self) -> InstallationId {
31        self.id
32    }
33
34    /// Returns the installation's node id.
35    #[cfg_attr(feature = "tracing", tracing::instrument)]
36    pub fn node_id(&self) -> &NodeId {
37        &self.node_id
38    }
39}
40
41impl Display for Installation {
42    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
43        write!(f, "{}", self.id)
44    }
45}
46
47#[cfg(test)]
48mod tests {
49    use crate::resource::NodeId;
50
51    use super::{Installation, InstallationId};
52
53    #[test]
54    fn trait_deserialize() {
55        let installation: Installation = serde_json::from_str(include_str!(
56            "../../tests/fixtures/resource/installation.json"
57        ))
58        .unwrap();
59
60        assert_eq!(25802826, installation.id().get());
61    }
62
63    #[test]
64    fn trait_display() {
65        let installation = Installation {
66            id: InstallationId::new(42),
67            node_id: NodeId::new("node_id"),
68        };
69
70        assert_eq!("42", installation.to_string());
71    }
72
73    #[test]
74    fn trait_send() {
75        fn assert_send<T: Send>() {}
76        assert_send::<Installation>();
77    }
78
79    #[test]
80    fn trait_sync() {
81        fn assert_sync<T: Sync>() {}
82        assert_sync::<Installation>();
83    }
84}