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

Draft: TxtProperties: new method to get a HashMap #303

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

keepsimple1
Copy link
Owner

This is to follow up on issue #300 .

Copy link

@Luni-4 Luni-4 left a comment

Choose a reason for hiding this comment

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

Thanks a lot for your PR, very appreciated! :)

Some changes I would make, let me know if they are feasible in your opinion

@@ -499,6 +499,15 @@ impl TxtProperties {
pub fn get_property_val_str(&self, key: &str) -> Option<&str> {
self.get(key).map(|x| x.val_str())
}

/// Returns a map of properties, where the key is the property key.
pub fn get_property_map_str(&self) -> HashMap<String, String> {
Copy link

Choose a reason for hiding this comment

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

Can we consume the TxtProperties? Something like:

/// Returns a map of properties, where the key is the property key.
///
/// **This method consumes [`TxtProperties`].**
pub fn into_hashmap(self) -> HashMap<String, String>

One can always obtain an HasMap<&str, &str> using the current methods provided by TxtProperties

Copy link
Owner Author

Choose a reason for hiding this comment

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

There are 2 commits. The 2nd commit already consumes the TxtProperties.

/// Returns a map of properties, where the key is the property key.
pub fn get_property_map_str(&self) -> HashMap<String, String> {
let mut map = HashMap::new();
for prop in self.properties.iter() {
Copy link

@Luni-4 Luni-4 Feb 6, 2025

Choose a reason for hiding this comment

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

If we consider the comment above, I would use into_iter() here

Copy link
Owner Author

Choose a reason for hiding this comment

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

same here, it's in the 2nd commit ;-)

pub fn get_property_map_str(&self) -> HashMap<String, String> {
let mut map = HashMap::new();
for prop in self.properties.iter() {
map.insert(prop.key.clone(), prop.val_str().to_string());
Copy link

Choose a reason for hiding this comment

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

If we consume, we can use prop directly

Copy link
Owner Author

Choose a reason for hiding this comment

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

same here, it's in the 2nd commit.

(
p.key,
p.val
.map_or_else(String::new, |v| String::from_utf8(v).unwrap_or_default()),
Copy link

@Luni-4 Luni-4 Feb 6, 2025

Choose a reason for hiding this comment

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

I would filter out empty String or wrong ones. What do you think? In this way the HashMap will have only the correct ones.

Copy link
Owner Author

Choose a reason for hiding this comment

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

Sounds good! Will update.

Copy link

@Luni-4 Luni-4 left a comment

Choose a reason for hiding this comment

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

Last changes and it is fine for me

self.properties
.into_iter()
.filter_map(|p| {
// Skip the property if the value is non-empty and not valid UTF-8.
Copy link

Choose a reason for hiding this comment

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

The comment seems wrong. We should skip if the value is empty and not valid UTF-8

// Skip the property if the value is non-empty and not valid UTF-8.
let value = p
.val
.map_or(Some(String::new()), |v| String::from_utf8(v).ok())?;
Copy link

Choose a reason for hiding this comment

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

I would use is_some_and instead of map_or, in this way we do not consider empty Strings

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