use serde::{Deserialize, Serialize};
use crate::{field_access, BotService, BotType};
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Bot {
#[serde(skip_serializing_if = "Option::is_none")]
user_id: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
full_name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
api_key: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
default_sending_stream: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
default_events_register_stream: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
default_all_public_streams: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
avatar_url: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
owner_id: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
services: Option<Vec<BotService>>,
#[serde(skip_serializing_if = "Option::is_none")]
email: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
bot_type: Option<BotType>,
#[serde(skip_serializing_if = "Option::is_none")]
is_active: Option<bool>,
}
impl Bot {
pub const fn new() -> Bot {
Bot {
user_id: None,
full_name: None,
api_key: None,
default_sending_stream: None,
default_events_register_stream: None,
default_all_public_streams: None,
avatar_url: None,
owner_id: None,
services: None,
email: None,
bot_type: None,
is_active: None,
}
}
}
field_access! {
Bot {
user_id: option { u64 },
default_all_public_streams: option { bool },
owner_id: option { u64 },
bot_type: option { BotType },
is_active: option { bool },
}
}
field_access! {
Bot {
full_name: option_cow_deref { str, String },
api_key: option_cow_deref { str, String },
default_sending_stream: option_cow_deref { str, String },
default_events_register_stream: option_cow_deref { str, String },
avatar_url: option_cow_deref { str, String },
services: option_cow_deref { [BotService], Vec<BotService> },
email: option_cow_deref { str, String },
}
}
impl Default for Bot {
fn default() -> Self {
Self::new()
}
}
impl std::fmt::Display for Bot {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
serde_json::to_string(self)
.map_err(|_| std::fmt::Error)
.and_then(|s| write!(f, "{s}"))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_bot() {
let user_id = 1;
let full_name = "test_full_name";
let api_key = "test_api_key";
let default_sending_stream = "test_default_sending_stream";
let default_events_register_stream = "test_default_events_register_stream";
let default_all_public_streams = true;
let avatar_url = "https://test.avatar.url";
let owner_id = 2;
let services = [BotService::new()];
let email = "test@example.com";
let bot_type = BotType::new();
let is_active = true;
let bot = Bot::new()
.with_user_id(user_id)
.with_full_name(full_name)
.with_api_key(api_key)
.with_default_sending_stream(default_sending_stream)
.with_default_events_register_stream(default_events_register_stream)
.with_default_all_public_streams(default_all_public_streams)
.with_avatar_url(avatar_url)
.with_owner_id(owner_id)
.with_services(services.as_ref())
.with_email(email)
.with_bot_type(bot_type)
.with_is_active(is_active);
let bot_json = bot.to_string();
assert_eq!(serde_json::to_string(&bot).unwrap(), bot_json);
assert_eq!(serde_json::from_str::<Bot>(&bot_json).unwrap(), bot);
assert_eq!(bot.user_id(), Some(user_id));
assert_eq!(bot.full_name(), Some(full_name));
assert_eq!(bot.api_key(), Some(api_key));
assert_eq!(bot.default_sending_stream(), Some(default_sending_stream));
assert_eq!(
bot.default_events_register_stream(),
Some(default_events_register_stream)
);
assert_eq!(
bot.default_all_public_streams(),
Some(default_all_public_streams)
);
assert_eq!(bot.avatar_url(), Some(avatar_url));
assert_eq!(bot.owner_id(), Some(owner_id));
assert_eq!(bot.services(), Some(services.as_ref()));
assert_eq!(bot.email(), Some(email));
assert_eq!(bot.bot_type(), Some(bot_type));
assert_eq!(bot.is_active(), Some(is_active));
}
}