use std::fmt::Display;
use serde::{self, Serialize};
use crate::errors::Error;
use super::{
id::{ChannelId, VideoId},
ChannelFilter, ChannelSortingCriteria, ChannelType, CommentSearch, ExtraVideoInfo, Language,
Order, Organisation, SearchOrder, VideoFilter, VideoSearch, VideoSearchCondition,
VideoSortingCriteria, VideoStatus, VideoType,
};
#[derive(Serialize, Debug, Default, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct VideoFilterBuilder {
filter: VideoFilter,
}
impl VideoFilterBuilder {
#[inline]
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[inline]
#[must_use]
pub fn include(mut self, include: &[ExtraVideoInfo]) -> Self {
self.filter.include = include.to_vec();
self
}
#[inline]
#[must_use]
pub const fn paginated(mut self, paginated: bool) -> Self {
self.filter.paginated = paginated;
self
}
#[inline]
#[must_use]
pub const fn limit(mut self, limit: u32) -> Self {
self.filter.limit = limit;
self.filter.paginated = true;
self
}
#[inline]
#[must_use]
pub const fn offset(mut self, offset: i32) -> Self {
self.filter.offset = offset;
self.filter.paginated = true;
self
}
#[inline]
#[must_use]
pub const fn sort_by(mut self, sort_by: VideoSortingCriteria) -> Self {
self.filter.sort_by = sort_by;
self
}
#[inline]
#[must_use]
pub const fn order(mut self, order: Order) -> Self {
self.filter.order = order;
self
}
#[inline]
#[must_use]
#[allow(clippy::missing_const_for_fn)]
pub fn channel_id(mut self, channel_id: ChannelId) -> Self {
self.filter.channel_id = Some(channel_id);
self
}
#[inline]
#[must_use]
#[allow(clippy::missing_const_for_fn)]
pub fn id(mut self, ids: &[VideoId]) -> Self {
self.filter.id = ids.to_vec();
self
}
#[inline]
#[must_use]
#[allow(clippy::missing_const_for_fn)]
pub fn organisation(mut self, org: Organisation) -> Self {
self.filter.org = Some(org);
self
}
#[inline]
#[must_use]
pub fn language(mut self, lang: &[Language]) -> Self {
self.filter.lang = lang.to_vec();
self
}
#[inline]
#[must_use]
pub const fn max_upcoming_hours(mut self, hours: u32) -> Self {
self.filter.max_upcoming_hours = hours;
self
}
#[inline]
#[must_use]
#[allow(clippy::missing_const_for_fn)]
pub fn mentioned_channel_id(mut self, channel_id: ChannelId) -> Self {
self.filter.mentioned_channel_id = Some(channel_id);
self
}
#[inline]
#[must_use]
pub fn topic(mut self, topic: &str) -> Self {
self.filter.topic = Some(topic.to_owned());
self
}
#[inline]
#[must_use]
pub const fn video_type(mut self, video_type: VideoType) -> Self {
self.filter.video_type = video_type;
self
}
#[inline]
#[must_use]
pub fn status(mut self, status: &[VideoStatus]) -> Self {
self.filter.status = status.to_vec();
self
}
#[inline]
#[must_use]
#[allow(clippy::missing_const_for_fn)]
pub fn build(self) -> VideoFilter {
self.filter
}
}
impl Display for VideoFilterBuilder {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}({})", stringify!(VideoFilterBuilder), self.filter)
}
}
impl From<VideoFilterBuilder> for VideoFilter {
fn from(builder: VideoFilterBuilder) -> Self {
builder.filter
}
}
#[derive(Serialize, Debug, Default, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct ChannelFilterBuilder {
filter: ChannelFilter,
}
impl ChannelFilterBuilder {
#[inline]
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[inline]
#[must_use]
pub const fn sort_by(mut self, sort_by: ChannelSortingCriteria) -> Self {
self.filter.sort_by = sort_by;
self
}
#[inline]
#[must_use]
pub const fn order(mut self, order: Order) -> Self {
self.filter.order = order;
self
}
#[inline]
#[must_use]
pub fn language(mut self, lang: &[Language]) -> Self {
self.filter.languages = lang.to_vec();
self
}
#[inline]
#[must_use]
pub const fn channel_type(mut self, channel_type: ChannelType) -> Self {
self.filter.channel_type = Some(channel_type);
self
}
#[inline]
#[must_use]
#[allow(clippy::missing_const_for_fn)]
pub fn organisation(mut self, organisation: Organisation) -> Self {
self.filter.organisation = Some(organisation);
self
}
#[inline]
#[must_use]
pub const fn limit(mut self, limit: u32) -> Self {
self.filter.limit = limit;
self
}
#[inline]
#[must_use]
pub const fn offset(mut self, offset: i32) -> Self {
self.filter.offset = offset;
self
}
pub fn build(self) -> Result<ChannelFilter, Error> {
match &self.filter.limit {
0..=50 => (),
_ => {
return Err(Error::FilterCreationError(format!(
"Could not instantiate {} with a limit of {}. Valid range is 0 to 50, inclusive.",
stringify!(ChannelFilter),
self.filter.limit
)))
}
}
Ok(self.filter)
}
}
#[derive(Serialize, Debug, Default, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct VideoSearchBuilder {
search: VideoSearch,
}
impl VideoSearchBuilder {
#[inline]
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[inline]
#[must_use]
pub fn conditions(mut self, conditions: &[VideoSearchCondition]) -> Self {
self.search.conditions = conditions.to_vec();
self
}
#[inline]
#[must_use]
pub const fn paginated(mut self, paginated: bool) -> Self {
self.search.paginated = paginated;
self
}
#[inline]
#[must_use]
pub const fn limit(mut self, limit: u32) -> Self {
self.search.limit = limit;
self.search.paginated = true;
self
}
#[inline]
#[must_use]
pub const fn offset(mut self, offset: i32) -> Self {
self.search.offset = offset;
self.search.paginated = true;
self
}
#[inline]
#[must_use]
pub const fn order(mut self, order: SearchOrder) -> Self {
self.search.sort_order = order;
self
}
#[inline]
#[must_use]
#[allow(clippy::missing_const_for_fn)]
pub fn channels(mut self, channels: &[ChannelId]) -> Self {
self.search.channels = channels.to_vec();
self
}
#[inline]
#[must_use]
#[allow(clippy::missing_const_for_fn)]
pub fn organisations(mut self, organisations: &[Organisation]) -> Self {
self.search.organisations = organisations.to_vec();
self
}
#[inline]
#[must_use]
pub fn languages(mut self, languages: &[Language]) -> Self {
self.search.languages = languages.to_vec();
self
}
#[inline]
#[must_use]
pub fn topics(mut self, topics: &[String]) -> Self {
self.search.topics = topics.to_vec();
self
}
#[inline]
#[must_use]
pub fn types(mut self, types: &[VideoType]) -> Self {
self.search.types = types.to_vec();
self
}
#[inline]
#[must_use]
#[allow(clippy::missing_const_for_fn)]
pub fn build(self) -> VideoSearch {
self.search
}
}
impl From<VideoSearchBuilder> for VideoSearch {
fn from(builder: VideoSearchBuilder) -> Self {
builder.search
}
}
#[derive(Serialize, Debug, Default, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct CommentSearchBuilder {
search: CommentSearch,
}
impl CommentSearchBuilder {
#[inline]
#[must_use]
pub fn new(search: &str) -> Self {
Self {
search: CommentSearch {
search: search.to_owned(),
..CommentSearch::default()
},
}
}
#[inline]
#[must_use]
pub const fn paginated(mut self, paginated: bool) -> Self {
self.search.paginated = paginated;
self
}
#[inline]
#[must_use]
pub const fn limit(mut self, limit: u32) -> Self {
self.search.limit = limit;
self.search.paginated = true;
self
}
#[inline]
#[must_use]
pub const fn offset(mut self, offset: i32) -> Self {
self.search.offset = offset;
self.search.paginated = true;
self
}
#[inline]
#[must_use]
pub const fn order(mut self, order: SearchOrder) -> Self {
self.search.sort_order = order;
self
}
#[inline]
#[must_use]
#[allow(clippy::missing_const_for_fn)]
pub fn channels(mut self, channels: &[ChannelId]) -> Self {
self.search.channels = channels.to_vec();
self
}
#[inline]
#[must_use]
#[allow(clippy::missing_const_for_fn)]
pub fn organisations(mut self, organisations: &[Organisation]) -> Self {
self.search.organisations = organisations.to_vec();
self
}
#[inline]
#[must_use]
pub fn languages(mut self, languages: &[Language]) -> Self {
self.search.languages = languages.to_vec();
self
}
#[inline]
#[must_use]
pub fn topics(mut self, topics: &[String]) -> Self {
self.search.topics = topics.to_vec();
self
}
#[inline]
#[must_use]
pub fn types(mut self, types: &[VideoType]) -> Self {
self.search.types = types.to_vec();
self
}
#[inline]
#[must_use]
#[allow(clippy::missing_const_for_fn)]
pub fn build(self) -> CommentSearch {
self.search
}
}
impl From<CommentSearchBuilder> for CommentSearch {
fn from(builder: CommentSearchBuilder) -> Self {
builder.search
}
}