use std::borrow::Cow;
use std::hash::{DefaultHasher, Hash, Hasher};
use std::sync::Arc;
use crate::id::format::{self, Format};
use crate::id::Result;
use super::Selector;
#[derive(Clone, Debug, Default)]
pub struct Builder<'a> {
format: format::Builder<'a, 7>,
}
impl Selector {
#[inline]
#[must_use]
pub fn builder<'a>() -> Builder<'a> {
Builder {
format: Format::builder().with(0, "zrs"),
}
}
#[inline]
#[must_use]
pub fn to_builder(&self) -> Builder<'_> {
Builder {
format: self.format.to_builder().with(0, "zrs"),
}
}
}
impl<'a> Builder<'a> {
#[inline]
#[must_use]
pub fn with_provider<S>(mut self, value: S) -> Self
where
S: Into<Cow<'a, str>>,
{
self.set_provider(value);
self
}
#[inline]
#[must_use]
pub fn with_resource<S>(mut self, value: S) -> Self
where
S: Into<Cow<'a, str>>,
{
self.set_resource(value);
self
}
#[inline]
#[must_use]
pub fn with_variant<S>(mut self, value: S) -> Self
where
S: Into<Cow<'a, str>>,
{
self.set_variant(value);
self
}
#[inline]
#[must_use]
pub fn with_context<S>(mut self, value: S) -> Self
where
S: Into<Cow<'a, str>>,
{
self.set_context(value);
self
}
#[inline]
#[must_use]
pub fn with_location<S>(mut self, value: S) -> Self
where
S: Into<Cow<'a, str>>,
{
self.set_location(value);
self
}
#[inline]
#[must_use]
pub fn with_fragment<S>(mut self, value: S) -> Self
where
S: Into<Cow<'a, str>>,
{
self.set_fragment(value);
self
}
#[inline]
pub fn set_provider<S>(&mut self, value: S) -> &mut Self
where
S: Into<Cow<'a, str>>,
{
self.format.set(1, value);
self
}
#[inline]
pub fn set_resource<S>(&mut self, value: S) -> &mut Self
where
S: Into<Cow<'a, str>>,
{
self.format.set(2, value);
self
}
#[inline]
pub fn set_variant<S>(&mut self, value: S) -> &mut Self
where
S: Into<Cow<'a, str>>,
{
self.format.set(3, value);
self
}
#[inline]
pub fn set_context<S>(&mut self, value: S) -> &mut Self
where
S: Into<Cow<'a, str>>,
{
self.format.set(4, value);
self
}
#[inline]
pub fn set_location<S>(&mut self, value: S) -> &mut Self
where
S: Into<Cow<'a, str>>,
{
self.format.set(5, value);
self
}
#[inline]
pub fn set_fragment<S>(&mut self, value: S) -> &mut Self
where
S: Into<Cow<'a, str>>,
{
self.format.set(6, value);
self
}
pub fn build(self) -> Result<Selector> {
let format = self.format.build()?;
let hash = {
let mut hasher = DefaultHasher::new();
format.hash(&mut hasher);
hasher.finish()
};
Ok(Selector { format: Arc::new(format), hash })
}
}