use std::fmt::{Display, Formatter};
use serde::{Deserialize, Serialize};
use crate::resource::{GitRef, GitSha, MinimalRepository};
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Deserialize, Serialize)]
pub struct PullRequestBranch {
#[serde(rename = "ref")]
git_ref: GitRef,
#[serde(rename = "sha")]
git_sha: GitSha,
repo: MinimalRepository,
}
impl PullRequestBranch {
#[cfg_attr(feature = "tracing", tracing::instrument)]
pub fn git_ref(&self) -> &GitRef {
&self.git_ref
}
#[cfg_attr(feature = "tracing", tracing::instrument)]
pub fn git_sha(&self) -> &GitSha {
&self.git_sha
}
#[cfg_attr(feature = "tracing", tracing::instrument)]
pub fn repository(&self) -> &MinimalRepository {
&self.repo
}
}
impl Display for PullRequestBranch {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.git_ref)
}
}
#[cfg(test)]
mod test {
use super::PullRequestBranch;
const JSON: &str = r#"
{
"ref": "main",
"sha": "3de05046636de664eff97823e24c92d382fa6607",
"repo": {
"id": 518377950,
"url": "https://api.github.com/repos/devxbots/automatons",
"name": "automatons"
}
}
"#;
#[test]
fn trait_deserialize() {
let branch: PullRequestBranch = serde_json::from_str(JSON).unwrap();
assert_eq!("main", branch.git_ref().get());
}
#[test]
fn trait_display() {
let branch: PullRequestBranch = serde_json::from_str(JSON).unwrap();
assert_eq!("main", branch.to_string());
}
#[test]
fn trait_send() {
fn assert_send<T: Send>() {}
assert_send::<PullRequestBranch>();
}
#[test]
fn trait_sync() {
fn assert_sync<T: Sync>() {}
assert_sync::<PullRequestBranch>();
}
}