Skip to content

Commit

Permalink
Remove expect, use collect, add must_use
Browse files Browse the repository at this point in the history
  • Loading branch information
voidentente committed Aug 7, 2024
1 parent 191dda6 commit e586fee
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
7 changes: 6 additions & 1 deletion src/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub struct Field {

impl Field {
/// Create a new empty field metadata
#[must_use]
pub const fn empty() -> Self {
Self {
meta: String::new(),
Expand Down Expand Up @@ -47,16 +48,19 @@ impl Field {
}

/// Get the metadata of this field
#[must_use]
pub fn get_meta(&self) -> &str {
self.meta.as_str()
}

/// Return whether this field has inner fields
#[must_use]
pub fn has_fields(&self) -> bool {
self.fields.is_some()
}

/// Get a reference to the inner fields of this field, if it has any
#[must_use]
pub fn fields(&self) -> Option<&Fields> {
self.fields.as_ref()
}
Expand All @@ -75,6 +79,7 @@ pub struct Fields {

impl Fields {
/// Return a new, empty metadata field map
#[must_use]
pub fn new() -> Self {
Self::default()
}
Expand All @@ -99,7 +104,7 @@ impl Fields {
impl<K: Into<String>> FromIterator<(K, Field)> for Fields {
fn from_iter<T: IntoIterator<Item = (K, Field)>>(iter: T) -> Self {
Self {
fields: HashMap::from_iter(iter.into_iter().map(|(k, v)| (k.into(), v))),
fields: iter.into_iter().map(|(k, v)| (k.into(), v)).collect(),
}
}
}
11 changes: 4 additions & 7 deletions src/ser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use unicode_ident::is_xid_continue;
use crate::{
error::{Error, Result},
extensions::Extensions,
meta::Fields,
meta::{Field, Fields},
options::Options,
parse::{is_ident_first_char, is_ident_raw_char, is_whitespace_char, LargeSInt, LargeUInt},
};
Expand Down Expand Up @@ -1341,14 +1341,11 @@ impl<'a, W: fmt::Write> ser::SerializeStruct for Compound<'a, W> {
if let Some((ref config, _)) = self.ser.pretty {
let mut iter = self.ser.field_memory.iter();

let name = iter.next().expect(
"is always at least one, because we push one at the beginning of this function",
);

let init = iter.next().and_then(|name| config.meta.get_field(name));
let field = iter
.try_fold(config.meta.get_field(name), |field, name| {
.try_fold(init, |field, name| {
field
.and_then(|field| field.fields())
.and_then(Field::fields)
.map(|fields| fields.get_field(name))
})
.flatten();
Expand Down

0 comments on commit e586fee

Please sign in to comment.