automatons_github/resource/pull_request/
mod.rs1use std::fmt::{Display, Formatter};
2
3use serde::{Deserialize, Serialize};
4use url::Url;
5
6use crate::id;
7
8pub use self::branch::PullRequestBranch;
9
10mod branch;
11
12id!(
13 PullRequestId
18);
19
20id!(
21 PullRequestNumber
26);
27
28#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Deserialize, Serialize)]
34pub struct PullRequest {
35 id: PullRequestId,
36 number: PullRequestNumber,
37 url: Url,
38 head: PullRequestBranch,
39 base: PullRequestBranch,
40}
41
42impl PullRequest {
43 #[cfg_attr(feature = "tracing", tracing::instrument)]
45 pub fn id(&self) -> PullRequestId {
46 self.id
47 }
48
49 #[cfg_attr(feature = "tracing", tracing::instrument)]
51 pub fn number(&self) -> PullRequestNumber {
52 self.number
53 }
54
55 #[cfg_attr(feature = "tracing", tracing::instrument)]
57 pub fn url(&self) -> &Url {
58 &self.url
59 }
60
61 #[cfg_attr(feature = "tracing", tracing::instrument)]
63 pub fn head(&self) -> &PullRequestBranch {
64 &self.head
65 }
66
67 #[cfg_attr(feature = "tracing", tracing::instrument)]
69 pub fn base(&self) -> &PullRequestBranch {
70 &self.base
71 }
72}
73
74impl Display for PullRequest {
75 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
76 write!(f, "#{}", self.number)
77 }
78}
79
80#[cfg(test)]
81mod tests {
82 use super::PullRequest;
83
84 #[test]
85 fn trait_deserialize() {
86 let pr: PullRequest = serde_json::from_str(include_str!(
87 "../../../tests/fixtures/resource/pull_request.json"
88 ))
89 .unwrap();
90
91 assert_eq!(27, pr.number().get());
92 }
93
94 #[test]
95 fn trait_display() {
96 let pr: PullRequest = serde_json::from_str(include_str!(
97 "../../../tests/fixtures/resource/pull_request.json"
98 ))
99 .unwrap();
100
101 assert_eq!("#27", pr.to_string());
102 }
103
104 #[test]
105 fn trait_send() {
106 fn assert_send<T: Send>() {}
107 assert_send::<PullRequest>();
108 }
109
110 #[test]
111 fn trait_sync() {
112 fn assert_sync<T: Sync>() {}
113 assert_sync::<PullRequest>();
114 }
115}