Skip to content

Commit

Permalink
refactor: option definitions
Browse files Browse the repository at this point in the history
What seemed like a minor issue turned out to be fairly complex.

I completely redid the style::options code, and then had to update a lot
of the processor and reference code accordingly.

Close: #62

Signed-off-by: Bruce D'Arcus <bdarcus@gmail.com>
  • Loading branch information
bdarcus committed Jun 20, 2023
1 parent d4a577a commit f0cff31
Show file tree
Hide file tree
Showing 6 changed files with 305 additions and 502 deletions.
74 changes: 40 additions & 34 deletions bibliography/src/reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,30 @@ SPDX-License-Identifier: MPL-2.0
SPDX-FileCopyrightText: © 2023 Bruce D'Arcus
*/

//! A reference is a bibliographic item, such as a book, article, or web page.
//! A reference is a bibliographic item, such as a book, article, or web page.
//! It is the basic unit of bibliographic data.
//!
//! The model includes the following core data types.
//!
//! The model includes the following core data types.
//! Each is designed to be as simple as possible, while also allowing more complex data structures.
//!
//!
//! ## Title
//!
//!
//! A title can be a single string, a structured title, or a multilingual title.
//!
//!
//! ## Contributor
//!
//!
//! A contributor can be a single string, a structured name, or a list of contributors.
//!
//!
//! ## Date
//!
//! Dates can either be EDTF strings, for flexible dates and date-times, or literal strings.
//!
//! Dates can either be EDTF strings, for flexible dates and date-times, or literal strings.
//! Literal strings can be used for examples like "Han Dynasty".
use edtf::level_1::Edtf;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::fmt;
use style::{locale::MonthList, options::StyleOptions};
use style::{locale::MonthList, options::Config};
use url::Url;
//use icu::calendar::DateTime;

Expand Down Expand Up @@ -347,24 +347,25 @@ impl fmt::Display for ContributorList {

/// A Name is a string that can be formatted in different ways.
pub trait Name {
fn names(&self, options: StyleOptions, as_sorted: bool) -> String;
fn names(&self, options: Config, as_sorted: bool) -> String;
}

/// A NameList is a list of names that can be formatted in different ways, depending on configuration options, and context.
pub trait NameList {
/// Return a list of names, formatted according to the given options.
/// If `as_sorted` is true, the names will be displayed as sorted.
fn names_list(&self, options: StyleOptions, as_sorted: bool) -> String;
fn names_list(&self, options: Config, as_sorted: bool) -> String;
}

impl Name for Contributor {
// if as_sorted is true, the name will be displayed as sorted.
fn names(&self, options: StyleOptions, as_sorted: bool) -> String {
let as_sorted_config = match options.contributors.display_as_sort {
style::options::DisplayAsSort::All => true,
style::options::DisplayAsSort::First => true,
style::options::DisplayAsSort::None => false,
};
fn names(&self, options: Config, as_sorted: bool) -> String {
let as_sorted_config =
match options.contributors.clone().unwrap_or_default().display_as_sort {
style::options::DisplayAsSort::All => true,
style::options::DisplayAsSort::First => true,
style::options::DisplayAsSort::None => false,
};
match self {
Contributor::SimpleName(name) => name.to_string(),
Contributor::StructuredName(contributor) => {
Expand Down Expand Up @@ -397,28 +398,33 @@ fn display_and_sort_names() {
given_name: "John".to_string(),
family_name: "Doe".to_string(),
});
let options = StyleOptions::default();
let options = Config::default();
assert_eq!(simple.names(options, false), "John Doe");
let options = StyleOptions::default();
let options = Config::default();
assert_eq!(
simple.names(options, true),
"John Doe",
"as_sorted=true should not affect a simple name"
);
let options = StyleOptions::default();
let options = Config::default();
assert_eq!(structured.names(options, false), "John Doe");
let options = StyleOptions::default();
let options = Config::default();
assert_eq!(structured.names(options, true), "Doe, John");
}

impl NameList for ContributorList {
fn names_list(&self, options: StyleOptions, as_sorted: bool) -> String {
fn names_list(&self, options: Config, as_sorted: bool) -> String {
let names: Vec<String> = self
.0
.iter()
.enumerate()
.map(|(i, c)| {
let as_sorted_config = match options.contributors.display_as_sort {
let as_sorted_config = match options
.contributors
.clone()
.unwrap_or_default()
.display_as_sort
{
style::options::DisplayAsSort::All => true,
style::options::DisplayAsSort::First => i == 0,
style::options::DisplayAsSort::None => false,
Expand All @@ -440,9 +446,9 @@ fn contributor_list() {
Contributor::SimpleName("John Doe".to_string()),
Contributor::SimpleName("Jane Doe".to_string()),
]);
let options = StyleOptions::default();
let options = Config::default();
assert_eq!(contributor_list.names_list(options, false), "John Doe, Jane Doe");
let options = StyleOptions::default();
let options = Config::default();
assert_eq!(
contributor_list.names_list(options, true),
"John Doe, Jane Doe",
Expand All @@ -458,16 +464,16 @@ fn contributor_list() {
family_name: "Doe".to_string(),
}),
]);
let options = StyleOptions::default();
let options = Config::default();
assert_eq!(structured_name_list.names_list(options, false), "John Doe, Jane Doe");
let options = StyleOptions::default();
let options = Config::default();
assert_eq!(structured_name_list.names_list(options, true), "Doe, John, Doe, Jane");
let options = StyleOptions {
contributors: style::options::StyleContributors {
let options = Config {
contributors: Some(style::options::Contributors {
display_as_sort: style::options::DisplayAsSort::First,
..style::options::StyleContributors::default()
},
..style::options::StyleOptions::default()
..style::options::Contributors::default()
}),
..style::options::Config::default()
};
assert_eq!(structured_name_list.names_list(options, false), "Doe, John, Jane Doe");
}
9 changes: 9 additions & 0 deletions processor/examples/style.csl.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
---
info:
title: APA
options:
sort:
template:
- key: author
- key: year
group:
template:
- author
- year
templates:
title-apa:
- title: title
Expand Down
Loading

0 comments on commit f0cff31

Please sign in to comment.