use serde::Serialize;
#[derive(Debug, PartialEq, Eq, Clone, Hash)]
pub struct Options {
name: Option<String>,
company: Option<String>,
}
#[derive(Debug, PartialEq, Clone)]
pub struct OptionsBuilder(Options);
#[derive(Serialize)]
pub(crate) struct SerializableOptions<'a> {
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) name: &'a Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) company: &'a Option<String>,
}
impl Options {
pub fn builder() -> OptionsBuilder {
OptionsBuilder::new()
}
pub fn json(&self) -> Result<String, serde_json::Error> {
serde_json::to_string(&SerializableOptions::from(self))
}
}
impl OptionsBuilder {
pub fn new() -> Self {
Self(Options {
name: None,
company: None,
})
}
pub fn name(mut self, name: impl Into<String>) -> Self {
self.0.name = Some(name.into());
self
}
pub fn company(mut self, company: impl Into<String>) -> Self {
self.0.company = Some(company.into());
self
}
pub fn build(self) -> Options {
self.0
}
}
impl Default for OptionsBuilder {
fn default() -> Self {
Self::new()
}
}
impl<'a> From<&'a Options> for SerializableOptions<'a> {
fn from(options: &'a Options) -> Self {
let Options { name, company } = options;
Self { name, company }
}
}