use super::collection::Collection;
use super::component::Component;
use super::enums::*;
use super::{
AppId, Artifact, ContentRating, Image, Language, License, MarkupTranslatableString, Release,
Screenshot, TranslatableList, TranslatableString, Video,
};
use chrono::{DateTime, Utc};
use std::collections::HashMap;
use url::Url;
#[derive(Default, Debug)]
pub struct ArtifactBuilder {
pub platform: Option<String>,
pub kind: Option<ArtifactKind>,
pub sizes: Vec<Size>,
pub url: Option<Url>,
pub checksums: Vec<Checksum>,
pub bundles: Vec<Bundle>,
}
#[allow(dead_code)]
impl ArtifactBuilder {
#[must_use]
pub fn kind(mut self, kind: ArtifactKind) -> Self {
self.kind = Some(kind);
self
}
#[must_use]
pub fn url(mut self, url: Url) -> Self {
self.url = Some(url);
self
}
#[must_use]
pub fn bundle(mut self, bundle: Bundle) -> Self {
self.bundles.push(bundle);
self
}
#[must_use]
pub fn size(mut self, size: Size) -> Self {
self.sizes.push(size);
self
}
#[must_use]
pub fn checksum(mut self, checksum: Checksum) -> Self {
self.checksums.push(checksum);
self
}
#[must_use]
pub fn platform(mut self, platform: &str) -> Self {
self.platform = Some(platform.to_string());
self
}
#[must_use]
pub fn build(self) -> Artifact {
Artifact {
url: self.url.expect("an artifact location is required"),
kind: self.kind.expect("artifact type is required"),
sizes: self.sizes,
checksums: self.checksums,
platform: self.platform,
bundles: self.bundles,
}
}
}
#[derive(Debug)]
pub struct CollectionBuilder {
pub version: String,
pub origin: Option<String>,
pub components: Vec<Component>,
pub architecture: Option<String>,
}
#[allow(dead_code)]
impl CollectionBuilder {
pub fn new(version: &str) -> Self {
Self {
version: version.to_string(),
origin: None,
components: vec![],
architecture: None,
}
}
#[must_use]
pub fn architecture(mut self, architecture: &str) -> Self {
self.architecture = Some(architecture.to_string());
self
}
#[must_use]
pub fn origin(mut self, origin: &str) -> Self {
self.origin = Some(origin.to_string());
self
}
#[must_use]
pub fn component(mut self, component: Component) -> Self {
self.components.push(component);
self
}
#[must_use]
pub fn build(self) -> Collection {
Collection {
version: self.version,
origin: self.origin,
components: self.components,
architecture: self.architecture,
}
}
}
#[derive(Default, Debug)]
pub struct ComponentBuilder {
pub kind: ComponentKind,
pub id: Option<AppId>,
pub name: Option<TranslatableString>,
pub summary: Option<TranslatableString>,
pub description: Option<MarkupTranslatableString>,
pub project_license: Option<License>,
pub metadata_license: Option<License>,
pub project_group: Option<String>,
pub compulsory_for_desktop: Option<String>,
pub extends: Vec<AppId>,
pub icons: Vec<Icon>,
pub screenshots: Vec<Screenshot>,
pub urls: Vec<ProjectUrl>,
pub developer_name: Option<TranslatableString>,
pub update_contact: Option<String>,
pub categories: Vec<Category>,
pub launchables: Vec<Launchable>,
pub pkgname: Option<String>,
pub bundles: Vec<Bundle>,
pub releases: Vec<Release>,
pub languages: Vec<Language>,
pub mimetypes: Vec<String>,
pub kudos: Vec<Kudo>,
pub keywords: Option<TranslatableList>,
pub content_rating: Option<ContentRating>,
pub provides: Vec<Provide>,
pub translations: Vec<Translation>,
pub source_pkgname: Option<String>,
pub suggestions: Vec<AppId>,
pub requirements: Vec<AppId>,
pub metadata: HashMap<String, Option<String>>,
}
#[allow(dead_code)]
impl ComponentBuilder {
#[must_use]
pub fn id(mut self, id: AppId) -> Self {
self.id = Some(id);
self
}
#[must_use]
pub fn name(mut self, name: TranslatableString) -> Self {
self.name = Some(name);
self
}
#[must_use]
pub fn content_rating(mut self, content_rating: ContentRating) -> Self {
self.content_rating = Some(content_rating);
self
}
#[must_use]
pub fn kind(mut self, kind: ComponentKind) -> Self {
self.kind = kind;
self
}
#[must_use]
pub fn developer_name(mut self, developer_name: TranslatableString) -> Self {
if !developer_name.is_empty() {
self.developer_name = Some(developer_name);
}
self
}
#[must_use]
pub fn summary(mut self, summary: TranslatableString) -> Self {
if !summary.is_empty() {
self.summary = Some(summary);
}
self
}
#[must_use]
pub fn description(mut self, description: MarkupTranslatableString) -> Self {
if !description.is_empty() {
self.description = Some(description);
}
self
}
#[must_use]
pub fn metadata_license(mut self, license: License) -> Self {
self.metadata_license = Some(license);
self
}
#[must_use]
pub fn project_license(mut self, license: License) -> Self {
self.project_license = Some(license);
self
}
#[must_use]
pub fn keywords(mut self, keywords: TranslatableList) -> Self {
if !keywords.is_empty() {
self.keywords = Some(keywords);
}
self
}
#[must_use]
pub fn compulsory_for_desktop(mut self, compulsory_for_desktop: &str) -> Self {
self.compulsory_for_desktop = Some(compulsory_for_desktop.to_string());
self
}
#[must_use]
pub fn project_group(mut self, group: &str) -> Self {
self.project_group = Some(group.to_string());
self
}
#[must_use]
pub fn suggest(mut self, id: AppId) -> Self {
self.suggestions.push(id);
self
}
#[must_use]
pub fn url(mut self, url: ProjectUrl) -> Self {
self.urls.push(url);
self
}
#[must_use]
pub fn screenshot(mut self, screenshot: Screenshot) -> Self {
self.screenshots.push(screenshot);
self
}
#[must_use]
pub fn icon(mut self, icon: Icon) -> Self {
self.icons.push(icon);
self
}
#[must_use]
pub fn kudo(mut self, kudo: Kudo) -> Self {
self.kudos.push(kudo);
self
}
#[must_use]
pub fn translation(mut self, translation: Translation) -> Self {
self.translations.push(translation);
self
}
#[must_use]
pub fn bundle(mut self, bundle: Bundle) -> Self {
self.bundles.push(bundle);
self
}
#[must_use]
pub fn language(mut self, language: Language) -> Self {
self.languages.push(language);
self
}
#[must_use]
pub fn category(mut self, category: Category) -> Self {
self.categories.push(category);
self
}
#[must_use]
pub fn mimetype(mut self, mimetype: &str) -> Self {
self.mimetypes.push(mimetype.to_string());
self
}
#[must_use]
pub fn extend(mut self, extend: AppId) -> Self {
self.extends.push(extend);
self
}
#[must_use]
pub fn release(mut self, release: Release) -> Self {
self.releases.push(release);
self
}
#[must_use]
pub fn launchable(mut self, launchable: Launchable) -> Self {
self.launchables.push(launchable);
self
}
#[must_use]
pub fn provide(mut self, provide: Provide) -> Self {
self.provides.push(provide);
self
}
#[must_use]
pub fn pkgname(mut self, pkgname: &str) -> Self {
self.pkgname = Some(pkgname.to_string());
self
}
#[must_use]
pub fn source_pkgname(mut self, source_pkgname: &str) -> Self {
self.source_pkgname = Some(source_pkgname.to_string());
self
}
#[must_use]
pub fn update_contact(mut self, update_contact: &str) -> Self {
self.update_contact = Some(update_contact.to_string());
self
}
#[must_use]
pub fn require(mut self, id: AppId) -> Self {
self.requirements.push(id);
self
}
#[must_use]
pub fn metadata(mut self, key: String, val: Option<String>) -> Self {
self.metadata.insert(key, val);
self
}
#[must_use]
pub fn build(self) -> Component {
Component {
kind: self.kind,
id: self.id.expect("An 'id' is required"),
name: self.name.expect("A 'name' is required"),
summary: self.summary,
description: self.description,
project_license: self.project_license,
metadata_license: self.metadata_license,
project_group: self.project_group,
compulsory_for_desktop: self.compulsory_for_desktop,
extends: self.extends,
icons: self.icons,
screenshots: self.screenshots,
urls: self.urls,
developer_name: self.developer_name,
update_contact: self.update_contact,
categories: self.categories,
launchables: self.launchables,
pkgname: self.pkgname,
bundles: self.bundles,
releases: self.releases,
languages: self.languages,
mimetypes: self.mimetypes,
kudos: self.kudos,
keywords: self.keywords,
content_rating: self.content_rating,
provides: self.provides,
translations: self.translations,
source_pkgname: self.source_pkgname,
suggestions: self.suggestions,
requirements: self.requirements,
metadata: self.metadata,
}
}
}
#[derive(Debug)]
pub struct ImageBuilder {
pub width: Option<u32>,
pub height: Option<u32>,
pub url: Url,
pub kind: ImageKind,
}
#[allow(dead_code)]
impl ImageBuilder {
pub fn new(url: Url) -> Self {
Self {
width: None,
height: None,
url,
kind: ImageKind::Source,
}
}
#[must_use]
pub fn kind(mut self, kind: ImageKind) -> Self {
self.kind = kind;
self
}
#[must_use]
pub fn width(mut self, width: u32) -> Self {
self.width = Some(width);
self
}
#[must_use]
pub fn height(mut self, height: u32) -> Self {
self.height = Some(height);
self
}
#[must_use]
pub fn build(self) -> Image {
Image {
width: self.width,
height: self.height,
url: self.url,
kind: self.kind,
}
}
}
#[derive(Debug)]
pub struct LanguageBuilder {
pub percentage: Option<u32>,
pub locale: String,
}
#[allow(dead_code)]
impl LanguageBuilder {
pub fn new(locale: &str) -> Self {
Self {
percentage: None,
locale: locale.to_string(),
}
}
#[must_use]
pub fn percentage(mut self, percentage: u32) -> Self {
self.percentage = Some(percentage);
self
}
#[must_use]
pub fn build(self) -> Language {
Language {
locale: self.locale,
percentage: self.percentage,
}
}
}
#[derive(Debug)]
pub struct ReleaseBuilder {
pub date: Option<DateTime<Utc>>,
pub date_eol: Option<DateTime<Utc>>,
pub description: Option<MarkupTranslatableString>,
pub version: String,
pub kind: Option<ReleaseKind>,
pub sizes: Vec<Size>,
pub urgency: ReleaseUrgency,
pub artifacts: Vec<Artifact>,
pub url: Option<Url>,
}
#[allow(dead_code)]
impl ReleaseBuilder {
pub fn new(version: &str) -> Self {
Self {
date: None,
date_eol: None,
description: None,
kind: Some(ReleaseKind::Stable),
sizes: vec![],
version: version.to_string(),
urgency: ReleaseUrgency::Medium,
artifacts: vec![],
url: None,
}
}
#[must_use]
pub fn description(mut self, description: MarkupTranslatableString) -> Self {
if !description.is_empty() {
self.description = Some(description);
}
self
}
#[must_use]
pub fn url(mut self, url: Url) -> Self {
self.url = Some(url);
self
}
#[must_use]
pub fn urgency(mut self, urgency: ReleaseUrgency) -> Self {
self.urgency = urgency;
self
}
#[must_use]
pub fn date(mut self, date: DateTime<Utc>) -> Self {
self.date = Some(date);
self
}
#[must_use]
pub fn date_eol(mut self, date_eol: DateTime<Utc>) -> Self {
self.date_eol = Some(date_eol);
self
}
#[must_use]
pub fn kind(mut self, kind: ReleaseKind) -> Self {
self.kind = Some(kind);
self
}
#[must_use]
pub fn size(mut self, size: Size) -> Self {
self.sizes.push(size);
self
}
#[must_use]
pub fn sizes(mut self, sizes: Vec<Size>) -> Self {
self.sizes = sizes;
self
}
#[must_use]
pub fn artifact(mut self, artifact: Artifact) -> Self {
self.artifacts.push(artifact);
self
}
#[must_use]
pub fn build(self) -> Release {
let kind = self.kind.unwrap_or_default();
Release {
version: self.version,
date: self.date,
date_eol: self.date_eol,
kind,
description: self.description,
sizes: self.sizes,
urgency: self.urgency,
artifacts: self.artifacts,
url: self.url,
}
}
}
#[derive(Default, Debug)]
pub struct ScreenshotBuilder {
pub is_default: Option<bool>,
pub caption: Option<TranslatableString>,
pub images: Vec<Image>,
pub videos: Vec<Video>,
}
#[allow(dead_code)]
impl ScreenshotBuilder {
#[must_use]
pub fn caption(mut self, caption: TranslatableString) -> Self {
if !caption.is_empty() {
self.caption = Some(caption);
}
self
}
#[must_use]
pub fn set_default(mut self, is_default: bool) -> Self {
self.is_default = Some(is_default);
self
}
#[must_use]
pub fn image(mut self, image: Image) -> Self {
self.images.push(image);
self
}
#[must_use]
pub fn images(mut self, images: Vec<Image>) -> Self {
self.images = images;
self
}
#[must_use]
pub fn video(mut self, video: Video) -> Self {
self.videos.push(video);
self
}
#[must_use]
pub fn videos(mut self, videos: Vec<Video>) -> Self {
self.videos = videos;
self
}
#[must_use]
pub fn build(self) -> Screenshot {
Screenshot {
caption: self.caption,
images: self.images,
videos: self.videos,
is_default: self.is_default.unwrap_or(true),
}
}
}
#[derive(Debug)]
pub struct VideoBuilder {
pub width: Option<u32>,
pub height: Option<u32>,
pub codec: Option<String>,
pub container: Option<String>,
pub url: Url,
}
#[allow(dead_code)]
impl VideoBuilder {
pub fn new(url: Url) -> Self {
Self {
width: None,
height: None,
container: None,
codec: None,
url,
}
}
#[must_use]
pub fn width(mut self, width: u32) -> Self {
self.width = Some(width);
self
}
#[must_use]
pub fn height(mut self, height: u32) -> Self {
self.height = Some(height);
self
}
#[must_use]
pub fn container(mut self, container: &str) -> Self {
self.container = Some(container.to_string());
self
}
#[must_use]
pub fn codec(mut self, codec: &str) -> Self {
self.codec = Some(codec.to_string());
self
}
#[must_use]
pub fn build(self) -> Video {
Video {
width: self.width,
height: self.height,
codec: self.codec,
container: self.container,
url: self.url,
}
}
}