use std::collections::HashMap;
use std::fmt::{Display, Formatter};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use url::Url;
use crate::resource::{Account, NodeId};
use crate::{id, name};
id!(
AppId
);
name!(
AppName
);
name!(
AppSlug
);
#[derive(Clone, Eq, PartialEq, Debug, Deserialize, Serialize)]
pub struct App {
id: AppId,
node_id: NodeId,
name: AppName,
slug: AppSlug,
owner: Account,
description: String,
external_url: Url,
html_url: Url,
created_at: DateTime<Utc>,
updated_at: DateTime<Utc>,
permissions: HashMap<String, String>,
events: Vec<String>,
}
impl App {
#[cfg_attr(feature = "tracing", tracing::instrument)]
pub fn id(&self) -> AppId {
self.id
}
#[cfg_attr(feature = "tracing", tracing::instrument)]
pub fn node_id(&self) -> &NodeId {
&self.node_id
}
#[cfg_attr(feature = "tracing", tracing::instrument)]
pub fn name(&self) -> &AppName {
&self.name
}
#[cfg_attr(feature = "tracing", tracing::instrument)]
pub fn slug(&self) -> &AppSlug {
&self.slug
}
#[cfg_attr(feature = "tracing", tracing::instrument)]
pub fn owner(&self) -> &Account {
&self.owner
}
#[cfg_attr(feature = "tracing", tracing::instrument)]
pub fn description(&self) -> &String {
&self.description
}
#[cfg_attr(feature = "tracing", tracing::instrument)]
pub fn external_url(&self) -> &Url {
&self.external_url
}
#[cfg_attr(feature = "tracing", tracing::instrument)]
pub fn html_url(&self) -> &Url {
&self.html_url
}
#[cfg_attr(feature = "tracing", tracing::instrument)]
pub fn created_at(&self) -> &DateTime<Utc> {
&self.created_at
}
#[cfg_attr(feature = "tracing", tracing::instrument)]
pub fn updated_at(&self) -> &DateTime<Utc> {
&self.updated_at
}
#[cfg_attr(feature = "tracing", tracing::instrument)]
pub fn permissions(&self) -> &HashMap<String, String> {
&self.permissions
}
#[cfg_attr(feature = "tracing", tracing::instrument)]
pub fn events(&self) -> &Vec<String> {
&self.events
}
}
impl Display for App {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.name)
}
}
#[cfg(test)]
mod tests {
use super::App;
#[test]
fn trait_deserialize() {
let app: App =
serde_json::from_str(include_str!("../../tests/fixtures/resource/app.json")).unwrap();
assert_eq!("devxbots/checkbot", app.name().get());
}
#[test]
fn trait_display() {
let app: App =
serde_json::from_str(include_str!("../../tests/fixtures/resource/app.json")).unwrap();
assert_eq!("devxbots/checkbot", app.to_string());
}
#[test]
fn trait_send() {
fn assert_send<T: Send>() {}
assert_send::<App>();
}
#[test]
fn trait_sync() {
fn assert_sync<T: Sync>() {}
assert_sync::<App>();
}
}