#![forbid(unsafe_code)]
mod builder;
mod collections;
mod normalization;
mod schema;
mod typed;
mod validation;
use std::{fmt, str::Utf8Error};
pub use builder::DescriptionBuilder;
pub use collections::CollectionEditError;
pub use normalization::{NormalizationDiagnostic, NormalizationError};
pub use r_dcf_syntax::{
Diagnostic as SyntaxDiagnostic, DiagnosticKind as SyntaxDiagnosticKind, EditError, Field,
FieldName, FormatStyle, InvalidFieldName, InvalidLogicalValue, LineEnding, LogicalValue, Parse,
Record, SourceSpan, ValueText,
};
pub use r_metadata::{Relation, Remote, Url};
pub use typed::{CollectionEntry, CollectionIssue, CollectionResult};
pub use validation::{Severity, Validation, ValidationIssue};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Description {
parse: Parse,
}
impl Description {
pub fn builder() -> DescriptionBuilder {
DescriptionBuilder::default()
}
pub fn parse(source: &str) -> Self {
Self {
parse: r_dcf_syntax::parse(source),
}
}
pub fn parse_utf8(source: &[u8]) -> Result<Self, Utf8Error> {
std::str::from_utf8(source).map(Self::parse)
}
pub fn from_parse(parse: Parse) -> Self {
Self { parse }
}
pub const fn as_parse(&self) -> &Parse {
&self.parse
}
pub fn into_parse(self) -> Parse {
self.parse
}
pub fn diagnostics(&self) -> &[SyntaxDiagnostic] {
self.parse.diagnostics()
}
pub fn records(&self) -> impl Iterator<Item = Record> {
self.parse.records()
}
pub fn primary_record(&self) -> Option<Record> {
self.records().next()
}
pub fn field(&self, name: &str) -> Option<Field> {
self.primary_record()?.last_field(name)
}
pub fn fields<'a>(&'a self, name: &'a str) -> impl Iterator<Item = Field> + 'a {
self.primary_record()
.into_iter()
.flat_map(move |record| record.fields().collect::<Vec<_>>())
.filter(move |field| field.name().as_deref() == Some(name))
}
pub fn fields_all(&self) -> impl Iterator<Item = Field> {
self.records()
.flat_map(|record| record.fields().collect::<Vec<_>>())
}
fn raw(&self, name: &str) -> Option<ValueText> {
self.field(name).map(|field| field.value())
}
pub fn replace_last(&self, name: &str, value: &LogicalValue) -> Result<Self, EditError> {
self.parse
.replace_last(0, name, value)
.map(Self::from_parse)
}
pub fn replace_all(&self, name: &str, value: &LogicalValue) -> Result<Self, EditError> {
self.parse.replace_all(0, name, value).map(Self::from_parse)
}
pub fn insert_after(
&self,
after: &str,
name: &FieldName,
value: &LogicalValue,
) -> Result<Self, EditError> {
self.parse
.insert_after(0, after, name, value)
.map(Self::from_parse)
}
pub fn remove_last(&self, name: &str) -> Result<Self, EditError> {
self.parse.remove_last(0, name).map(Self::from_parse)
}
pub fn remove_all(&self, name: &str) -> Result<Self, EditError> {
self.parse.remove_all(0, name).map(Self::from_parse)
}
pub fn set_field(&self, name: &FieldName, value: &LogicalValue) -> Result<Self, EditError> {
if self.field(name.as_str()).is_some() {
return self.replace_last(name.as_str(), value);
}
let record = self
.primary_record()
.ok_or(EditError::RecordOutOfBounds { index: 0 })?;
let anchor = record
.fields()
.last()
.and_then(|field| field.name())
.ok_or_else(|| EditError::FieldNotFound {
name: "<insertion anchor>".to_owned(),
})?;
self.insert_after(&anchor, name, value)
}
pub fn upsert(&self, name: &FieldName, value: &LogicalValue) -> Result<Self, EditError> {
self.set_field(name, value)
}
}
impl fmt::Display for Description {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.parse, formatter)
}
}
macro_rules! raw_accessors {
($($method:ident => $field:literal),+ $(,)?) => {$(
#[doc = concat!("Returns raw `", $field, "` from the last declaration.")]
pub fn $method(&self) -> Option<ValueText> { self.raw($field) }
)+};
}
impl Description {
raw_accessors! {
package => "Package", type_ => "Type", package_type => "Type", title => "Title", version => "Version",
date => "Date", date_publication => "Date/Publication", authors_at_r => "Authors@R",
author => "Author", maintainer => "Maintainer", copyright => "Copyright",
description => "Description",
license => "License", depends => "Depends", imports => "Imports", suggests => "Suggests",
enhances => "Enhances", linking_to => "LinkingTo", system_requirements => "SystemRequirements",
url => "URL", bug_reports => "BugReports", additional_repositories => "Additional_repositories",
remotes => "Remotes", encoding => "Encoding", repository => "Repository",
contact => "Contact", mailing_list => "MailingList", note => "Note",
needs_compilation => "NeedsCompilation",
os_type => "OS_type", priority => "Priority", archs => "Archs", biarch => "Biarch",
classification_acm => "Classification/ACM", classification_acm_2012 => "Classification/ACM-2012",
classification_jel => "Classification/JEL", classification_msc => "Classification/MSC",
classification_msc_2010 => "Classification/MSC-2010", collate => "Collate",
lazy_data => "LazyData", lazy_load => "LazyLoad", byte_compile => "ByteCompile",
keep_source => "KeepSource", use_lto => "UseLTO", staged_install => "StagedInstall",
zip_data => "ZipData", build_vignettes => "BuildVignettes",
license_is_foss => "License_is_FOSS", license_restricts_use => "License_restricts_use",
vignette_builder => "VignetteBuilder", roxygen_note => "RoxygenNote", rd_macros => "RdMacros",
packaged => "Packaged", built => "Built",
}
}
#[cfg(test)]
mod tests;