Skip to content

Commit

Permalink
Unwrap in less places
Browse files Browse the repository at this point in the history
  • Loading branch information
reknih committed Oct 3, 2024
1 parent 2bb2ab0 commit 7f1c867
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 19 deletions.
6 changes: 3 additions & 3 deletions src/csl/citation_label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ impl Alphanumerical {
year
});

year.map(|y| {
year.and_then(|y| {
let mut num = String::with_capacity(2);
write!(&mut num, "{:02}", y).unwrap();
num
write!(&mut num, "{:02}", y).ok()?;
Some(num)
})
}

Expand Down
13 changes: 6 additions & 7 deletions src/csl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1275,12 +1275,11 @@ impl<'a> StyleContext<'a> {
ctx.set_special_form(None);
};

match kind {
Some(CitePurpose::Author) => {
match (kind, self.csl.bibliography.as_ref()) {
(Some(CitePurpose::Author), _) => {
do_author(&mut ctx);
}
Some(CitePurpose::Full) if self.csl.bibliography.is_some() => {
let bib = self.csl.bibliography.as_ref().unwrap();
(Some(CitePurpose::Full), Some(bib)) => {
ctx.writing.push_name_options(&bib.name_options);
bib.layout.render(&mut ctx);
ctx.writing.pop_name_options();
Expand All @@ -1301,7 +1300,7 @@ impl<'a> StyleContext<'a> {
}
}
}
Some(CitePurpose::Prose) => {
(Some(CitePurpose::Prose), _) => {
do_author(&mut ctx);
if !self.csl.citation.layout.prefix.as_ref().map_or(false, |f| {
f.chars().next().map_or(false, char::is_whitespace)
Expand Down Expand Up @@ -1339,7 +1338,7 @@ impl<'a> StyleContext<'a> {
);
}
}
Some(CitePurpose::Year) | Some(CitePurpose::Full) | None => {
(Some(CitePurpose::Year) | Some(CitePurpose::Full) | None, _) => {
do_regular(&mut ctx);
}
}
Expand Down Expand Up @@ -1573,7 +1572,7 @@ impl<'a> StyleContext<'a> {
let fallback = if i == 0 {
locale.parse_base().and_then(|base| match base {
BaseLanguage::Iso639_1(lang) => {
Some(LocaleCode(String::from_utf8(lang.to_vec()).unwrap()))
Some(LocaleCode(String::from_utf8(lang.to_vec()).ok()?))
}
BaseLanguage::Iana(lang) => Some(LocaleCode(lang)),
_ => None,
Expand Down
8 changes: 4 additions & 4 deletions src/csl/taxonomy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -752,8 +752,8 @@ impl EntryLike for citationberg::json::Item {
match self.0.get(&variable.to_string()) {
Some(csl_json::Value::Names(names)) => names
.iter()
.map(|name| {
Cow::Owned(match name {
.filter_map(|name| {
Some(Cow::Owned(match name {
csl_json::NameValue::Literal(l) => Person {
name: l.literal.clone(),
prefix: None,
Expand All @@ -772,7 +772,7 @@ impl EntryLike for citationberg::json::Item {
if let Some(given) = given {
parts.push(given.as_str());
}
let mut p = Person::from_strings(parts).unwrap();
let mut p = Person::from_strings(parts).ok()?;
if let Some(suffix) = suffix {
p.suffix = Some(suffix.as_str().to_owned());
}
Expand All @@ -796,7 +796,7 @@ impl EntryLike for citationberg::json::Item {
given_name: given.clone(),
alias: None,
},
})
}))
})
.collect(),
_ => vec![],
Expand Down
6 changes: 3 additions & 3 deletions src/selectors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ impl Selector {
/// This can panic if there are resolving entries which do not bind the
/// argument.
pub fn bound<'s>(&self, entry: &'s Entry, bound: &str) -> Option<&'s Entry> {
self.apply(entry).map(|mut hm| hm.remove(bound).unwrap())
self.apply(entry).and_then(|mut hm| hm.remove(bound))
}

/// Applies the selector to an [`Entry`] and returns the bound variables
Expand Down Expand Up @@ -227,8 +227,8 @@ impl Selector {

Self::Binding(binding, expr) => {
expr.apply_any(entries).map(|(mut bound, es)| {
if !es.is_empty() {
bound.insert(binding.to_string(), es.first().unwrap());
if let Some(first) = es.first() {
bound.insert(binding.to_string(), first);
}
(bound, vec![])
})
Expand Down
5 changes: 3 additions & 2 deletions src/types/persons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,9 @@ impl Person {
}

let last_pre = parts[0];
let given_name =
if parts.len() > 1 { Some(parts.last().unwrap().to_string()) } else { None };
let given_name = (parts.len() > 1)
.then(|| parts.last().map(|last| last.to_string()))
.flatten();

let suffix = if parts.len() > 2 { Some(parts[1].to_string()) } else { None };

Expand Down

0 comments on commit 7f1c867

Please sign in to comment.