use crate::{Description, FieldName, FormatStyle, LogicalValue};
#[derive(Debug, Clone, Default)]
pub struct DescriptionBuilder {
style: FormatStyle,
fields: Vec<(FieldName, LogicalValue)>,
}
impl DescriptionBuilder {
pub const fn new(style: FormatStyle) -> Self {
Self {
style,
fields: Vec::new(),
}
}
pub fn field(mut self, name: FieldName, value: LogicalValue) -> Self {
self.fields.push((name, value));
self
}
pub fn package(self, value: LogicalValue) -> Self {
self.common("Package", value)
}
pub fn version(self, value: LogicalValue) -> Self {
self.common("Version", value)
}
pub fn title(self, value: LogicalValue) -> Self {
self.common("Title", value)
}
pub fn description(self, value: LogicalValue) -> Self {
self.common("Description", value)
}
pub fn license(self, value: LogicalValue) -> Self {
self.common("License", value)
}
pub fn authors_at_r(self, value: LogicalValue) -> Self {
self.common("Authors@R", value)
}
pub fn author(self, value: LogicalValue) -> Self {
self.common("Author", value)
}
pub fn maintainer(self, value: LogicalValue) -> Self {
self.common("Maintainer", value)
}
pub fn depends(self, value: LogicalValue) -> Self {
self.common("Depends", value)
}
pub fn imports(self, value: LogicalValue) -> Self {
self.common("Imports", value)
}
pub fn suggests(self, value: LogicalValue) -> Self {
self.common("Suggests", value)
}
pub fn url(self, value: LogicalValue) -> Self {
self.common("URL", value)
}
pub fn remotes(self, value: LogicalValue) -> Self {
self.common("Remotes", value)
}
fn common(self, name: &str, value: LogicalValue) -> Self {
self.field(
FieldName::new(name).expect("constant field name is valid"),
value,
)
}
pub fn build(self) -> Description {
let fields = self
.fields
.iter()
.map(|(name, value)| r_dcf_syntax::make::field(name, value, &self.style))
.collect::<Vec<_>>();
let record = r_dcf_syntax::make::record(&fields, &self.style);
Description::parse(&record)
}
}