1use crate::{Description, FieldName, FormatStyle, LogicalValue};
2
3#[derive(Debug, Clone, Default)]
5pub struct DescriptionBuilder {
6 style: FormatStyle,
7 fields: Vec<(FieldName, LogicalValue)>,
8}
9
10impl DescriptionBuilder {
11 pub const fn new(style: FormatStyle) -> Self {
13 Self {
14 style,
15 fields: Vec::new(),
16 }
17 }
18
19 pub fn field(mut self, name: FieldName, value: LogicalValue) -> Self {
21 self.fields.push((name, value));
22 self
23 }
24
25 pub fn package(self, value: LogicalValue) -> Self {
27 self.common("Package", value)
28 }
29 pub fn version(self, value: LogicalValue) -> Self {
31 self.common("Version", value)
32 }
33 pub fn title(self, value: LogicalValue) -> Self {
35 self.common("Title", value)
36 }
37 pub fn description(self, value: LogicalValue) -> Self {
39 self.common("Description", value)
40 }
41 pub fn license(self, value: LogicalValue) -> Self {
43 self.common("License", value)
44 }
45 pub fn authors_at_r(self, value: LogicalValue) -> Self {
47 self.common("Authors@R", value)
48 }
49 pub fn author(self, value: LogicalValue) -> Self {
51 self.common("Author", value)
52 }
53 pub fn maintainer(self, value: LogicalValue) -> Self {
55 self.common("Maintainer", value)
56 }
57 pub fn depends(self, value: LogicalValue) -> Self {
59 self.common("Depends", value)
60 }
61 pub fn imports(self, value: LogicalValue) -> Self {
63 self.common("Imports", value)
64 }
65 pub fn suggests(self, value: LogicalValue) -> Self {
67 self.common("Suggests", value)
68 }
69 pub fn url(self, value: LogicalValue) -> Self {
71 self.common("URL", value)
72 }
73 pub fn remotes(self, value: LogicalValue) -> Self {
75 self.common("Remotes", value)
76 }
77
78 fn common(self, name: &str, value: LogicalValue) -> Self {
79 self.field(
80 FieldName::new(name).expect("constant field name is valid"),
81 value,
82 )
83 }
84
85 pub fn build(self) -> Description {
87 let fields = self
88 .fields
89 .iter()
90 .map(|(name, value)| r_dcf_syntax::make::field(name, value, &self.style))
91 .collect::<Vec<_>>();
92 let record = r_dcf_syntax::make::record(&fields, &self.style);
93 Description::parse(&record)
94 }
95}