Skip to main content

r_description/
builder.rs

1use crate::{Description, FieldName, FormatStyle, LogicalValue};
2
3/// Canonical single-record `DESCRIPTION` builder.
4#[derive(Debug, Clone, Default)]
5pub struct DescriptionBuilder {
6    style: FormatStyle,
7    fields: Vec<(FieldName, LogicalValue)>,
8}
9
10impl DescriptionBuilder {
11    /// Creates a builder using `style`.
12    pub const fn new(style: FormatStyle) -> Self {
13        Self {
14            style,
15            fields: Vec::new(),
16        }
17    }
18
19    /// Appends a validated field. Duplicate declarations are retained.
20    pub fn field(mut self, name: FieldName, value: LogicalValue) -> Self {
21        self.fields.push((name, value));
22        self
23    }
24
25    /// Appends `Package`.
26    pub fn package(self, value: LogicalValue) -> Self {
27        self.common("Package", value)
28    }
29    /// Appends `Version`.
30    pub fn version(self, value: LogicalValue) -> Self {
31        self.common("Version", value)
32    }
33    /// Appends `Title`.
34    pub fn title(self, value: LogicalValue) -> Self {
35        self.common("Title", value)
36    }
37    /// Appends `Description`.
38    pub fn description(self, value: LogicalValue) -> Self {
39        self.common("Description", value)
40    }
41    /// Appends `License`.
42    pub fn license(self, value: LogicalValue) -> Self {
43        self.common("License", value)
44    }
45    /// Appends `Authors@R`.
46    pub fn authors_at_r(self, value: LogicalValue) -> Self {
47        self.common("Authors@R", value)
48    }
49    /// Appends `Author`.
50    pub fn author(self, value: LogicalValue) -> Self {
51        self.common("Author", value)
52    }
53    /// Appends `Maintainer`.
54    pub fn maintainer(self, value: LogicalValue) -> Self {
55        self.common("Maintainer", value)
56    }
57    /// Appends `Depends`.
58    pub fn depends(self, value: LogicalValue) -> Self {
59        self.common("Depends", value)
60    }
61    /// Appends `Imports`.
62    pub fn imports(self, value: LogicalValue) -> Self {
63        self.common("Imports", value)
64    }
65    /// Appends `Suggests`.
66    pub fn suggests(self, value: LogicalValue) -> Self {
67        self.common("Suggests", value)
68    }
69    /// Appends `URL`.
70    pub fn url(self, value: LogicalValue) -> Self {
71        self.common("URL", value)
72    }
73    /// Appends `Remotes`.
74    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    /// Builds a lossless parse with no structural syntax diagnostics.
86    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}