Skip to content

Commit

Permalink
refactor(options/sort): vector -> object
Browse files Browse the repository at this point in the history
To make room for additional configuration options for sorting, move the
template to a named template field, and add a couple of example
parameters.

Refs: #64

Signed-off-by: Bruce D'Arcus <bdarcus@gmail.com>
  • Loading branch information
bdarcus committed Jun 13, 2023
1 parent 66776c2 commit 9e6f6aa
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
2 changes: 1 addition & 1 deletion bibliography/src/reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use edtf::level_1::Edtf;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::fmt;
use style::options::{StyleOptions};
use style::options::{StyleOptions, StyleContributors};
use url::Url;
//use icu::calendar::DateTime;

Expand Down
6 changes: 3 additions & 3 deletions processor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::collections::HashMap;
use std::fmt::Debug;
use std::option::Option;
use style::options::StyleOptions;
use style::options::{MonthOptions, Sort, SortGroupKey, SortOrder};
use style::options::{MonthOptions, SortOptions, SortGroupKey, SortOrder};
#[allow(unused_imports)] // for now
use style::template::{
Contributors, DateForm, Dates, StyleTemplateComponent, StyleTemplateContributor,
Expand Down Expand Up @@ -423,9 +423,9 @@ impl Processor {
) -> Vec<InputReference> {
let mut references: Vec<InputReference> = references;
let options: &StyleOptions = &self.style.options;
let sort_config: &[Sort] = self.style.options.get_sort_config();
let sort_config: &SortOptions = self.style.options.get_sort_config();
//println!("{:?}", sort_config);
sort_config.iter().rev().for_each(|sort| {
sort_config.template.iter().rev().for_each(|sort| {
match sort.key {
SortGroupKey::Author => {
references.par_sort_by(|a, b| {
Expand Down
49 changes: 43 additions & 6 deletions style/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub struct StyleOptions {
/// Localization configuration.
pub localization: Localization,
/// Sorting configuration.
pub sort: Vec<Sort>,
pub sort: SortOptions,
/// Substitution configuration.
pub substitute: SubstituteOptions,
}
Expand All @@ -64,17 +64,54 @@ impl Default for StyleOptions {
disambiguate: Disambiguation::default(),
group: GroupOptions::default().group,
localization: Localization { scope: LocalizationScope::PerItem },
sort: vec![
sort: SortOptions::default(),
substitute: SubstituteOptions::default(),
}
}
}

/// Sorting is configured by the [`SortOptions`] struct.
/// It distinguishes between the templates, which specify the sequence of keys used for sorting
/// and order they are sorted in, and options which cusutomize the sorting process.
#[derive(Debug, Deserialize, Serialize, Clone, JsonSchema)]
pub struct SortOptions {
pub options: SortConfig,
pub template: Vec<Sort>,
}

impl Default for SortOptions {
fn default() -> Self {
Self {
options: SortConfig::default(),
template: vec![
Sort {
key: SortGroupKey::Author,
order: SortOrder::Ascending,
},
Sort {
key: SortGroupKey::Year,
order: SortOrder::Ascending,
}
},
],
substitute: SubstituteOptions::default(),
}
}
}

#[derive(Debug, Deserialize, Serialize, Clone, JsonSchema)]
pub struct SortConfig {
/// Shorten name lists for sorting the same as for display.
// REVIEW: may need more options here.
pub shorten_names: bool,
/// Use same substitutions for sorting as for rendering.
pub render_substitutions: bool,
}


impl Default for SortConfig {
fn default() -> Self {
Self {
shorten_names: false,
render_substitutions: true,
}
}
}
Expand Down Expand Up @@ -123,8 +160,8 @@ impl StyleOptions {
pub fn get_group_key_config(&self) -> &[SortGroupKey] {
self.group.as_slice()
}
pub fn get_sort_config(&self) -> &[Sort] {
self.sort.as_slice()
pub fn get_sort_config(&self) -> &SortOptions {
&self.sort
}
pub fn get_contributors_config(&self) -> &StyleContributors {
&self.contributors
Expand Down

0 comments on commit 9e6f6aa

Please sign in to comment.