Skip to content
This repository was archived by the owner on Feb 17, 2026. It is now read-only.

Commit f0cff31

Browse files
committed
refactor: option definitions
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>
1 parent d4a577a commit f0cff31

6 files changed

Lines changed: 305 additions & 502 deletions

File tree

bibliography/src/reference.rs

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,30 @@ SPDX-License-Identifier: MPL-2.0
33
SPDX-FileCopyrightText: © 2023 Bruce D'Arcus
44
*/
55

6-
//! A reference is a bibliographic item, such as a book, article, or web page.
6+
//! A reference is a bibliographic item, such as a book, article, or web page.
77
//! It is the basic unit of bibliographic data.
8-
//!
9-
//! The model includes the following core data types.
8+
//!
9+
//! The model includes the following core data types.
1010
//! Each is designed to be as simple as possible, while also allowing more complex data structures.
11-
//!
11+
//!
1212
//! ## Title
13-
//!
13+
//!
1414
//! A title can be a single string, a structured title, or a multilingual title.
15-
//!
15+
//!
1616
//! ## Contributor
17-
//!
17+
//!
1818
//! A contributor can be a single string, a structured name, or a list of contributors.
19-
//!
19+
//!
2020
//! ## Date
21-
//!
22-
//! Dates can either be EDTF strings, for flexible dates and date-times, or literal strings.
21+
//!
22+
//! Dates can either be EDTF strings, for flexible dates and date-times, or literal strings.
2323
//! Literal strings can be used for examples like "Han Dynasty".
2424
2525
use edtf::level_1::Edtf;
2626
use schemars::JsonSchema;
2727
use serde::{Deserialize, Serialize};
2828
use std::fmt;
29-
use style::{locale::MonthList, options::StyleOptions};
29+
use style::{locale::MonthList, options::Config};
3030
use url::Url;
3131
//use icu::calendar::DateTime;
3232

@@ -347,24 +347,25 @@ impl fmt::Display for ContributorList {
347347

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

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

360360
impl Name for Contributor {
361361
// if as_sorted is true, the name will be displayed as sorted.
362-
fn names(&self, options: StyleOptions, as_sorted: bool) -> String {
363-
let as_sorted_config = match options.contributors.display_as_sort {
364-
style::options::DisplayAsSort::All => true,
365-
style::options::DisplayAsSort::First => true,
366-
style::options::DisplayAsSort::None => false,
367-
};
362+
fn names(&self, options: Config, as_sorted: bool) -> String {
363+
let as_sorted_config =
364+
match options.contributors.clone().unwrap_or_default().display_as_sort {
365+
style::options::DisplayAsSort::All => true,
366+
style::options::DisplayAsSort::First => true,
367+
style::options::DisplayAsSort::None => false,
368+
};
368369
match self {
369370
Contributor::SimpleName(name) => name.to_string(),
370371
Contributor::StructuredName(contributor) => {
@@ -397,28 +398,33 @@ fn display_and_sort_names() {
397398
given_name: "John".to_string(),
398399
family_name: "Doe".to_string(),
399400
});
400-
let options = StyleOptions::default();
401+
let options = Config::default();
401402
assert_eq!(simple.names(options, false), "John Doe");
402-
let options = StyleOptions::default();
403+
let options = Config::default();
403404
assert_eq!(
404405
simple.names(options, true),
405406
"John Doe",
406407
"as_sorted=true should not affect a simple name"
407408
);
408-
let options = StyleOptions::default();
409+
let options = Config::default();
409410
assert_eq!(structured.names(options, false), "John Doe");
410-
let options = StyleOptions::default();
411+
let options = Config::default();
411412
assert_eq!(structured.names(options, true), "Doe, John");
412413
}
413414

414415
impl NameList for ContributorList {
415-
fn names_list(&self, options: StyleOptions, as_sorted: bool) -> String {
416+
fn names_list(&self, options: Config, as_sorted: bool) -> String {
416417
let names: Vec<String> = self
417418
.0
418419
.iter()
419420
.enumerate()
420421
.map(|(i, c)| {
421-
let as_sorted_config = match options.contributors.display_as_sort {
422+
let as_sorted_config = match options
423+
.contributors
424+
.clone()
425+
.unwrap_or_default()
426+
.display_as_sort
427+
{
422428
style::options::DisplayAsSort::All => true,
423429
style::options::DisplayAsSort::First => i == 0,
424430
style::options::DisplayAsSort::None => false,
@@ -440,9 +446,9 @@ fn contributor_list() {
440446
Contributor::SimpleName("John Doe".to_string()),
441447
Contributor::SimpleName("Jane Doe".to_string()),
442448
]);
443-
let options = StyleOptions::default();
449+
let options = Config::default();
444450
assert_eq!(contributor_list.names_list(options, false), "John Doe, Jane Doe");
445-
let options = StyleOptions::default();
451+
let options = Config::default();
446452
assert_eq!(
447453
contributor_list.names_list(options, true),
448454
"John Doe, Jane Doe",
@@ -458,16 +464,16 @@ fn contributor_list() {
458464
family_name: "Doe".to_string(),
459465
}),
460466
]);
461-
let options = StyleOptions::default();
467+
let options = Config::default();
462468
assert_eq!(structured_name_list.names_list(options, false), "John Doe, Jane Doe");
463-
let options = StyleOptions::default();
469+
let options = Config::default();
464470
assert_eq!(structured_name_list.names_list(options, true), "Doe, John, Doe, Jane");
465-
let options = StyleOptions {
466-
contributors: style::options::StyleContributors {
471+
let options = Config {
472+
contributors: Some(style::options::Contributors {
467473
display_as_sort: style::options::DisplayAsSort::First,
468-
..style::options::StyleContributors::default()
469-
},
470-
..style::options::StyleOptions::default()
474+
..style::options::Contributors::default()
475+
}),
476+
..style::options::Config::default()
471477
};
472478
assert_eq!(structured_name_list.names_list(options, false), "Doe, John, Jane Doe");
473479
}

processor/examples/style.csl.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
---
22
info:
33
title: APA
4+
options:
5+
sort:
6+
template:
7+
- key: author
8+
- key: year
9+
group:
10+
template:
11+
- author
12+
- year
413
templates:
514
title-apa:
615
- title: title

0 commit comments

Comments
 (0)