automatons_github/resource/
organization.rs

1use std::fmt::{Display, Formatter};
2
3use serde::{Deserialize, Serialize};
4use url::Url;
5
6use crate::id;
7use crate::resource::{Login, NodeId};
8
9id!(
10    /// Organization id
11    ///
12    /// The [`OrganizationId`] is a unique, numerical id that is used to interact with an
13    /// organization through [GitHub's REST API](https://docs.github.com/en/rest).
14    OrganizationId
15);
16
17/// Organization
18///
19/// Organizations enable users to collaborate and share resources with each other in a structured
20/// way. Organizations can have members, teams, repositories, and other resources.
21#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Deserialize, Serialize)]
22pub struct Organization {
23    login: Login,
24    id: OrganizationId,
25    node_id: NodeId,
26    url: Url,
27    repos_url: Url,
28    events_url: Url,
29    hooks_url: Url,
30    issues_url: Url,
31    members_url: Url,
32    public_members_url: Url,
33    avatar_url: Url,
34    description: String,
35}
36
37impl Organization {
38    /// Returns the organization's [`Login`].
39    #[cfg_attr(feature = "tracing", tracing::instrument)]
40    pub fn login(&self) -> &Login {
41        &self.login
42    }
43
44    /// Returns the organization's [`OrganizationId`].
45    #[cfg_attr(feature = "tracing", tracing::instrument)]
46    pub fn id(&self) -> OrganizationId {
47        self.id
48    }
49
50    /// Returns the organization's [`NodeId`].
51    #[cfg_attr(feature = "tracing", tracing::instrument)]
52    pub fn node_id(&self) -> &NodeId {
53        &self.node_id
54    }
55
56    /// Returns the API endpoint to query the organization.
57    #[cfg_attr(feature = "tracing", tracing::instrument)]
58    pub fn url(&self) -> &Url {
59        &self.url
60    }
61
62    /// Returns the API endpoint to query the organization's repositories.
63    #[cfg_attr(feature = "tracing", tracing::instrument)]
64    pub fn repos_url(&self) -> &Url {
65        &self.repos_url
66    }
67
68    /// Returns the API endpoint to query the organization's events.
69    #[cfg_attr(feature = "tracing", tracing::instrument)]
70    pub fn events_url(&self) -> &Url {
71        &self.events_url
72    }
73
74    /// Returns the API endpoint to query the organization's hooks.
75    #[cfg_attr(feature = "tracing", tracing::instrument)]
76    pub fn hooks_url(&self) -> &Url {
77        &self.hooks_url
78    }
79
80    /// Returns the API endpoint to query the organization's issues.
81    #[cfg_attr(feature = "tracing", tracing::instrument)]
82    pub fn issues_url(&self) -> &Url {
83        &self.issues_url
84    }
85
86    /// Returns the API endpoint to query the organization's members.
87    #[cfg_attr(feature = "tracing", tracing::instrument)]
88    pub fn members_url(&self) -> &Url {
89        &self.members_url
90    }
91
92    /// Returns the API endpoint to query the organization's public members.
93    #[cfg_attr(feature = "tracing", tracing::instrument)]
94    pub fn public_members_url(&self) -> &Url {
95        &self.public_members_url
96    }
97
98    /// Returns the URL to the organization's avatar.
99    #[cfg_attr(feature = "tracing", tracing::instrument)]
100    pub fn avatar_url(&self) -> &Url {
101        &self.avatar_url
102    }
103
104    /// Returns the organization's description.
105    #[cfg_attr(feature = "tracing", tracing::instrument)]
106    pub fn description(&self) -> &str {
107        &self.description
108    }
109}
110
111impl Display for Organization {
112    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
113        write!(f, "{}", self.login)
114    }
115}
116
117#[cfg(test)]
118mod tests {
119    use super::Organization;
120
121    #[test]
122    fn trait_deserialize() {
123        let organization: Organization = serde_json::from_str(include_str!(
124            "../../tests/fixtures/resource/organization.json"
125        ))
126        .unwrap();
127
128        assert_eq!("devxbots", organization.login().get());
129    }
130
131    #[test]
132    fn trait_display() {
133        let organization: Organization = serde_json::from_str(include_str!(
134            "../../tests/fixtures/resource/organization.json"
135        ))
136        .unwrap();
137
138        assert_eq!("devxbots", organization.to_string());
139    }
140
141    #[test]
142    fn trait_send() {
143        fn assert_send<T: Send>() {}
144        assert_send::<Organization>();
145    }
146
147    #[test]
148    fn trait_sync() {
149        fn assert_sync<T: Sync>() {}
150        assert_sync::<Organization>();
151    }
152}