use std::borrow::Cow;
use serde::{Deserialize, Serialize};
#[cfg(feature = "validation")]
use validator::Validate;
#[cfg(feature = "validation")]
use crate::val_helpr::ValidationResult;
#[derive(Clone, Debug, Deserialize, Hash, PartialEq, Serialize)]
#[cfg_attr(feature = "validation", derive(Validate))]
pub struct File<'a> {
external_id: Cow<'a, str>,
source: Cow<'a, str>,
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "validation",
validate(custom = "super::validate_block_id"))]
block_id: Option<Cow<'a, str>>,
}
impl<'a> File<'a> {
pub fn builder() -> build::FileBuilderInit<'a> {
build::FileBuilderInit::new()
}
#[cfg(feature = "validation")]
#[cfg_attr(docsrs, doc(cfg(feature = "validation")))]
pub fn validate(&self) -> ValidationResult {
Validate::validate(self)
}
}
pub mod build {
use std::marker::PhantomData;
use super::*;
use crate::build::*;
#[allow(non_camel_case_types)]
pub mod method {
#[derive(Clone, Copy, Debug)]
pub struct external_id;
}
pub type FileBuilderInit<'a> =
FileBuilder<'a, RequiredMethodNotCalled<method::external_id>>;
#[derive(Debug)]
pub struct FileBuilder<'a, ExternalId> {
external_id: Option<Cow<'a, str>>,
source: Option<Cow<'a, str>>,
block_id: Option<Cow<'a, str>>,
state: PhantomData<ExternalId>,
}
impl<'a, Ext> FileBuilder<'a, Ext> {
pub fn new() -> Self {
Self { external_id: None,
source: None,
block_id: None,
state: PhantomData::<_> }
}
pub fn external_id<S>(self,
external_id: S)
-> FileBuilder<'a, Set<method::external_id>>
where S: Into<Cow<'a, str>>
{
FileBuilder { external_id: Some(external_id.into()),
source: self.source,
block_id: self.block_id,
state: PhantomData::<_> }
}
pub fn block_id<S>(mut self, block_id: S) -> Self
where S: Into<Cow<'a, str>>
{
self.block_id = Some(block_id.into());
self
}
}
impl<'a> FileBuilder<'a, Set<method::external_id>> {
pub fn build(self) -> File<'a> {
File { external_id: self.external_id.unwrap(),
source: "remote".into(),
block_id: self.block_id }
}
}
}