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

Use consistent naming for event constructors #431

Merged
merged 15 commits into from
Jul 24, 2022
Merged

Conversation

Mingun
Copy link
Collaborator

@Mingun Mingun commented Jul 19, 2022

Continuation of #428 (includes it changes) that unifies all event constructor names. I'm not sure about BytesText::new, however. While this name is very aligned and consistent with other names, I afraid, it can be error-prone. Would it be better to use new or leave from_escaped?

@Mingun Mingun requested a review from dralley July 19, 2022 18:38
@dralley
Copy link
Collaborator

dralley commented Jul 19, 2022

Would it be better to use new or leave from_escaped?

I think it would be OK to leave from_escaped() or new_from_escaped(), but we should make it more clear in which circumstances it is safe to use. Instead of saying

Creates a new BytesText from a string. The string is expected not to be escaped.

We should say something like

Creates a new BytesText without escaping the input.

Warning: This should be used only when the input is pre-escaped, or is known to not contain any characters requiring escape sequences (e.g. <, >, &).

},
decoder: Decoder::utf8(),
}
pub fn from_unescaped(content: &'a str) -> Self {
Copy link
Collaborator

@dralley dralley Jul 19, 2022

Choose a reason for hiding this comment

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

Oh, now I see the question.

Yes, this is backwards from what I think would be most intuitive. IMO, the default should be to escape the input in new(), with an alternative to provide escaped input if you already have it (or if you know that it doesn't need to be escaped). It should be easier to do what is most likely correct (perform escaping) than what is most likely incorrect (not doing so).

This is the model I had in mind when writing the comment above.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, I had the same doubts. I've changed names: from_escaped and new

@codecov-commenter
Copy link

Codecov Report

Merging #431 (a99f2f1) into master (b456b5a) will decrease coverage by 0.18%.
The diff coverage is 89.59%.

@@            Coverage Diff             @@
##           master     #431      +/-   ##
==========================================
- Coverage   49.51%   49.32%   -0.19%     
==========================================
  Files          22       22              
  Lines       13847    13805      -42     
==========================================
- Hits         6856     6810      -46     
- Misses       6991     6995       +4     
Flag Coverage Δ
unittests 49.32% <89.59%> (-0.19%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
benches/macrobenches.rs 0.00% <0.00%> (ø)
benches/microbenches.rs 0.00% <0.00%> (ø)
examples/custom_entities.rs 0.00% <0.00%> (ø)
src/events/mod.rs 68.60% <71.76%> (+0.40%) ⬆️
src/writer.rs 48.57% <90.00%> (-0.44%) ⬇️
src/reader.rs 60.97% <91.17%> (+0.61%) ⬆️
src/de/mod.rs 75.16% <95.65%> (-2.71%) ⬇️
src/de/seq.rs 91.83% <100.00%> (ø)
src/events/attributes.rs 94.10% <100.00%> (ø)
src/name.rs 88.54% <100.00%> (+0.16%) ⬆️
... and 5 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b456b5a...a99f2f1. Read the comment docs.

/// Creates a new `BytesStart` from the given content (name + attributes)
///
/// Owns its contents.
/// `&content[..name_len]` is not checked to be a valid name
Copy link
Collaborator

Choose a reason for hiding this comment

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

The attributes are also not checked to be valid.

Copy link
Collaborator

@dralley dralley Jul 21, 2022

Choose a reason for hiding this comment

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

To be honest I'm not sure I see the use case for this function for the majority of users, ironically the one thing which it does get used for a lot is creating events from invalid data for the tests.

I'm not saying that we should remove it, but perhaps it can be discouraged more actively?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Actually, we should mark that functions unsafe (#153) and provide a safe versions returning Result and which will check for a validity. But first we need to implement that validity checks (#371 and others) :). So I think it is a task for another PR, probably the one that will add these checks

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sure, sounds fine. Let's still do the documentation part though.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Could you write a suggestion?

Copy link
Collaborator

@dralley dralley Jul 22, 2022

Choose a reason for hiding this comment

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

How about

&content[..name_len] must be a valid name, and the remainder of content must be correctly-formed attributes. Neither are checked, it is possible to generate invalid XML if content or name_len are incorrect.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done + added similar warnings to all other constructors where there were none

@dralley
Copy link
Collaborator

dralley commented Jul 23, 2022

Approved: but please allow #425 to be merged first

@dralley
Copy link
Collaborator

dralley commented Jul 24, 2022

Alright, well, go ahead and fix up the conflicts here and on the other PR. Might as well go ahead and merge them.

Mingun and others added 15 commits July 24, 2022 19:40
…tead

As a result, it became clear that the `test_read_write_roundtrip_escape`
test did not check anything, so it was deleted
…caped`

Writer is anywhere works only in UTF-8, so this constructor is not needed to be public
Writer is anywhere works only in UTF-8, so this constructor is not needed to be public
…BytesStart::new`

Replace `BytesStart::owned` and `BytesStart::borrowed` by `BytesStart::from_content`
Replace `BytesText::from_plain_str` by `BytesText::new`
There is an agreement that constructors should be at the beginning
Co-authored-by: Daniel Alley <dalley@redhat.com>
@dralley dralley merged commit d8ae1c3 into tafia:master Jul 24, 2022
@Mingun Mingun deleted the renames branch July 24, 2022 15:29
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.

3 participants