Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unify deser impl #338

Closed
wants to merge 2 commits into from
Closed

Unify deser impl #338

wants to merge 2 commits into from

Conversation

ijackson
Copy link
Contributor

As part of my efforts to try to make #[deser(flatten)] work correctly (#336) I did some refactoring. Although the deser work is unfinished, I think these two commits are worth submitting separately.

This tests, in particular, that the error messdage is as expected
including the right key and filename.
This had open-coded copies of the impl for Value.  Replace them with
calls to the impl for Value.  This reduces duplication.  It would
allow us to change the impl for Value.

Use a macro for the many very-similar functions.
Copy link
Member

@matthiasbeyer matthiasbeyer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi. Sorry for not getting back to this for such a long time... I had other things on my mind and then I was on vacation. 🏖️

I hope you don't mind. 😸

If you are not interested anymore in continuing work on this PR, I am happy to make the adaptions requested in my review myself!

Comment on lines -364 to -411
fn deserialize_bool<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
visitor.visit_bool(self.cache.into_bool()?)
}

#[inline]
fn deserialize_i8<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
// FIXME: This should *fail* if the value does not fit in the requets integer type
visitor.visit_i8(self.cache.into_int()? as i8)
}

#[inline]
fn deserialize_i16<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
// FIXME: This should *fail* if the value does not fit in the requets integer type
visitor.visit_i16(self.cache.into_int()? as i16)
}

#[inline]
fn deserialize_i32<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
// FIXME: This should *fail* if the value does not fit in the requets integer type
visitor.visit_i32(self.cache.into_int()? as i32)
}

#[inline]
fn deserialize_i64<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
visitor.visit_i64(self.cache.into_int()?)
}

#[inline]
fn deserialize_u8<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
// FIXME: This should *fail* if the value does not fit in the requets integer type
visitor.visit_u8(self.cache.into_int()? as u8)
}

#[inline]
fn deserialize_u16<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
// FIXME: This should *fail* if the value does not fit in the requets integer type
visitor.visit_u16(self.cache.into_int()? as u16)
}

#[inline]
fn deserialize_u32<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
// FIXME: This should *fail* if the value does not fit in the requets integer type
visitor.visit_u32(self.cache.into_int()? as u32)
}

#[inline]
fn deserialize_u64<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
// FIXME: This should *fail* if the value does not fit in the requets integer type
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are FIXME nontations here that get lost in your patch. Can you please port them to your improvements?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the meantime those FIXMEs have gone and much of the code has been reorganised. I'm going to fix #464 first and then maybe revisit this.

Comment on lines +48 to +60
let expect_err = |res: Result<(), ConfigError>| {
assert!(res.is_err());
assert_eq!(
res.unwrap_err().to_string(),
format!(
"invalid type: string \"Torre di Pisa\", expected an integer for key `place.name` in tests/Settings.toml",
)
);
};

let c = make();
let res = c.try_deserialize().map(|_: Output| ());
expect_err(res);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this have to be so complicated?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was expecting there might be more tests with errors, so wanted to provide a helper to check an error was expected. I can inline this again if you prefer, which would probably make it a bit simpler looking.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes do that please.

@ijackson
Copy link
Contributor Author

Closing in favour of #488

@ijackson ijackson closed this Oct 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants