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

Make attribute creation more uniform #413

Closed
wants to merge 1 commit into from

Conversation

dralley
Copy link
Collaborator

@dralley dralley commented Jul 9, 2022

closes #410

@dralley dralley force-pushed the attr-conversions branch 2 times, most recently from 3566ac9 to ce6493d Compare July 9, 2022 05:05
@dralley
Copy link
Collaborator Author

dralley commented Jul 9, 2022

See question: #410 (comment)

/// # use pretty_assertions::assert_eq;
/// use quick_xml::events::attributes::Attribute;
///
/// let features = Attribute::new("features".as_bytes(), "Bells & whistles".as_bytes());
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think, this looks better:

Suggested change
/// let features = Attribute::new("features".as_bytes(), "Bells & whistles".as_bytes());
/// let features = Attribute::new(b"features", b"Bells & whistles");

Copy link
Collaborator Author

@dralley dralley Jul 9, 2022

Choose a reason for hiding this comment

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

The type of this is [u8; <len>] rather than &[u8], so you have to do a bit extra (e.g. as_ref(), and at that point it's not really cleaner anymore.

Copy link
Collaborator

Choose a reason for hiding this comment

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

This is only when you apply that suggestion to the tuple, because (b"...", b"...") will have a type ([u8; <len1>], [u8; <len2>]). Using byte literals with from_* methods is compiled, I checked

@dralley dralley force-pushed the attr-conversions branch 3 times, most recently from 2f61970 to eba1900 Compare July 9, 2022 15:27
@@ -40,6 +42,9 @@
- [#363]: Do not generate empty `Event::Text` events
- [#412]: Fix using incorrect encoding if `read_to_end` family of methods or `read_text`
method not found a corresponding end tag and reader has non-UTF-8 encoding
- [#413]: `Attribute::from((&[u8], &[u8]))` was made consistent with `Attribute::from((&str, &str))`
in that both will now escape the value. WARNING: if you were passing escaped values via this
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm not sure how to draw more attention to this but it is a somewhat consequential change and users won't get a compile error.

@dralley dralley requested a review from Mingun July 9, 2022 15:31
@dralley dralley marked this pull request as ready for review July 9, 2022 15:31
@dralley
Copy link
Collaborator Author

dralley commented Jul 9, 2022

@Mingun I think it makes sense, now, to make the members of Attribute private and use accessors e.g. raw_value(), normalized_value(), unescaped_value(). Do you agree?

Also, do you forsee any reason for the key parameter type to be Into<QName> as opposed to &[u8]?

@codecov-commenter
Copy link

codecov-commenter commented Jul 9, 2022

Codecov Report

Merging #413 (6b34514) into master (a6588c2) will decrease coverage by 0.02%.
The diff coverage is 53.84%.

@@            Coverage Diff             @@
##           master     #413      +/-   ##
==========================================
- Coverage   49.58%   49.55%   -0.03%     
==========================================
  Files          22       22              
  Lines       13935    13953      +18     
==========================================
+ Hits         6909     6915       +6     
- Misses       7026     7038      +12     
Flag Coverage Δ
unittests 49.55% <53.84%> (-0.03%) ⬇️

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

Impacted Files Coverage Δ
src/events/attributes.rs 92.85% <53.84%> (-0.74%) ⬇️

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 a6588c2...6b34514. Read the comment docs.

@Mingun
Copy link
Collaborator

Mingun commented Jul 9, 2022

With private fields it will be impossible to use Attribute in patterns which could break some usages. From the other hand, such usage is potentially unsafe, because Attribute could contain data in unknown encoding. I think it is better to introduce such change together with fixing accessors in #379.

Also, do you forsee any reason for the key parameter type to be Into<QName> as opposed to &[u8]?

Oh, yees... I totally forget, that I introduced QName for type safety which means, that user should always explicitly use it instead of plain &[u8], otherwise it just have no point. So yes, it should be QName and could be Into<QName>

@dralley
Copy link
Collaborator Author

dralley commented Jul 9, 2022

user should always explicitly use it instead of plain &[u8]

Well if this is the goal, Into<QName> doesn't make sense and neither does the current From<(.., ..)> syntax. Both of those would allow implicit conversion from bytes (or str).

@dralley
Copy link
Collaborator Author

dralley commented Jul 16, 2022

This PR isn't relevant anymore.

@dralley dralley closed this Jul 16, 2022
@dralley dralley deleted the attr-conversions branch July 16, 2022 22:05
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.

Using attribute-from-tuple conversion can be error-prone
3 participants