-
Notifications
You must be signed in to change notification settings - Fork 148
RUST-447 Standardize API with other serde libraries #179
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
Conversation
src/de/serde.rs
Outdated
Deserialize, | ||
DeserializeSeed, | ||
Deserializer, | ||
Deserializer as SerdeDeserializer, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this rename needs to occur because the original name conflicts with the newly-named Deserialize
struct, but the import needs to remain as-is for this line, but let me know if there's a better workaround.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since you don't need to use the name SerdeDeserializer
at all, I think you can actually use Deserializer as _
, which will include it for the purposes of values that implement that trait but won't put any name into scope.
src/de/mod.rs
Outdated
}; | ||
|
||
use ::serde::de::{Deserialize, Error}; | ||
use ::serde::de::{self, Error as SerdeError}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same conflict as other rename.
src/document.rs
Outdated
/// Attempts to encode the `Document` into a byte stream. | ||
pub fn encode<W: Write + ?Sized>(&self, writer: &mut W) -> EncoderResult<()> { | ||
/// Attempts to serialize the `Document` into a byte stream. | ||
pub fn serialize_doc<W: Write + ?Sized>(&self, writer: &mut W) -> crate::ser::Result<()> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, I hadn't thought of this conflict!. Technically you could have two different methods with the same name if one of them is from a trait, but it's not traditional overloading because there's no disambiguation at the call site based on the parameters; you'd only have access to one of methods for a given variable based on what type it is. Here's an example of that: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=996a1b2bab9c7a8602de0098de00012c
I think I might prefer the name serialize_to
here; when calling it, I think it would read well, e.g. doc.serialize_to(&mut writer)?
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, less redundant than having doc
in the name
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like serialize_to
, but we might also want to consider something like to_writer
and from_reader
like in serde_json
and serde-xml-rs
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm fine with from_reader
and to_writer
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm fine with both, I'll leave as-is with serialize_to
unless anyone feels super strongly either way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can change it to Patrick's suggestions; sorry if I was unclear
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oops sorry I didn't see that first comment! I had this window sitting open for a while and didn't realize you need to refresh to see new comments. will do
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No worries; github does require a lot of refreshes that it should probably trigger on its own
#[derive(Debug)] | ||
#[non_exhaustive] | ||
pub enum DecoderError { | ||
pub enum Error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we have a separate error type in the de
module than at the top level (as opposed to something like serde_json
, which has only one error type in the entire crate`, I think we still might want to have a prefix in the name of this type (and for the Result type below); it's a bit friendlier to users who need to import multiple Error types from our crate if we give them unique names.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@patrickfreed is there precedence in other serde libraries for this naming style when there's more than one error/result defined? I agree that it's a little less user friendly to use something like crate::de::Error
rather than DeserializerError
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's pretty standard to rely on the module path for these kinds of things. All the serialization libraries that I can find which have separate errors for serialization / deserialization simply name each Error
(toml
, xml
, serde_urlencoded
), and serde
itself also follows this pattern with its ser::Error
and de::Error
traits. I think we should keep to this pattern for consistency with other libraries.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense, I think consistency with serde
would be good. @saghm what do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess it makes sense to be consistent with our oid::Error
. I'm not sure what you're referring to with the xml-rs
crate, though; as it doesn't seem to have ser
or de
modules; do you mean serde_xml
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The xml
crate I was referring to appears to actually just be a parsing library, not a serialization one. It has reader
and writer
modules with their own Error
s which are somewhat analogous to de
and ser
. serde_xml-rs
appears to be the actual corresponding serialization crate for xml.
src/document.rs
Outdated
/// Attempts to encode the `Document` into a byte stream. | ||
pub fn encode<W: Write + ?Sized>(&self, writer: &mut W) -> EncoderResult<()> { | ||
/// Attempts to serialize the `Document` into a byte stream. | ||
pub fn serialize_doc<W: Write + ?Sized>(&self, writer: &mut W) -> crate::ser::Result<()> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, I hadn't thought of this conflict!. Technically you could have two different methods with the same name if one of them is from a trait, but it's not traditional overloading because there's no disambiguation at the call site based on the parameters; you'd only have access to one of methods for a given variable based on what type it is. Here's an example of that: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=996a1b2bab9c7a8602de0098de00012c
I think I might prefer the name serialize_to
here; when calling it, I think it would read well, e.g. doc.serialize_to(&mut writer)?
.
src/document.rs
Outdated
/// Attempts to decode a `Document` from a byte stream. | ||
pub fn decode<R: Read + ?Sized>(reader: &mut R) -> DecoderResult<Document> { | ||
/// Attempts to deserialize a `Document` from a byte stream. | ||
pub fn deserialize<R: Read + ?Sized>(reader: &mut R) -> crate::de::Result<Document> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you go with my serialize_to
suggestion above, I think this should be renamed to deserialize_from
for parity.
#[derive(Debug)] | ||
#[non_exhaustive] | ||
pub enum EncoderError { | ||
pub enum Error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto above suggestion to prefer the name of the Error/Result types in this module
src/de/serde.rs
Outdated
Deserialize, | ||
DeserializeSeed, | ||
Deserializer, | ||
Deserializer as SerdeDeserializer, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since you don't need to use the name SerdeDeserializer
at all, I think you can actually use Deserializer as _
, which will include it for the purposes of values that implement that trait but won't put any name into scope.
This PR updates the bson API language to use "serialize" and "deserialize" in place of "encode" and "decode" respectively.