Skip to content

Commit

Permalink
test(bib): add dates
Browse files Browse the repository at this point in the history
Also, fix a little date bug that crept in somewhere.

Signed-off-by: Bruce D'Arcus <bdarcus@gmail.com>
  • Loading branch information
bdarcus committed Jun 12, 2023
1 parent a22dae8 commit 66776c2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
2 changes: 2 additions & 0 deletions bibliography/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ url = { version = "2", features = ["serde"] }
edtf = { version = "0.2", features = ["chrono"] }
chrono = { version = "0.4", features = ["unstable-locales"] }
style = { path = "../style", package = "csln-style" }
icu = { version = "1.2.0", features = ["icu_datetime_experimental"] }
icu_testdata = { version = "1.2.0", features = ["icu_datetime_experimental"] }

24 changes: 23 additions & 1 deletion bibliography/src/reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ use edtf::level_1::Edtf;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::fmt;
use style::options::{StyleOptions, StyleContributors};
use style::options::{StyleOptions};
use url::Url;
//use icu::calendar::DateTime;

#[derive(Debug, Default, Deserialize, Serialize, Clone, JsonSchema, PartialEq)]
pub struct InputReference {
Expand Down Expand Up @@ -36,9 +37,30 @@ impl EdtfString {
pub fn as_date(&self) -> Option<Edtf> {
Edtf::parse(&self.0).ok()
}

// TODO do want this or string?
pub fn year(&self) -> Option<i32> {
self.as_date().and_then(|d| match d {
Edtf::Date(date) => Some(date.year()),
_ => None,
})
}
}

#[test]
fn test_edtf_dates () {
let date = EdtfString("2020-01-01".to_string());
assert_eq!(date.year(), Some(2020));
let date = EdtfString("2021-10".to_string());
assert_eq!(date.year(), Some(2021));
let date = EdtfString("2022".to_string());
assert_eq!(date.year(), Some(2022));
}

impl fmt::Display for EdtfString {



fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// TODO: finish this
let parsed_date: Edtf = match Edtf::parse(&self.0) {
Expand Down
2 changes: 1 addition & 1 deletion processor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ impl Processor {
let as_sorted = false;
match key {
"author" => reference.author.as_ref().unwrap().names(options, as_sorted),
"year" => reference.issued.as_ref().unwrap().year().unwrap_or_default(),
"year" => reference.issued.as_ref().unwrap().year().unwrap_or_default().to_string(),
"title" => reference.title.as_ref().unwrap().to_string(),
_ => panic!("Invalid key"),
}
Expand Down

0 comments on commit 66776c2

Please sign in to comment.