Skip to content

feat: Add TryFromIterator trait and implement it for Labels and Annotations #715

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

Merged
merged 6 commits into from
Jan 15, 2024

Conversation

Techassi
Copy link
Member

@Techassi Techassi commented Jan 12, 2024

Fixes #713

It does not directly solve the issue, but .collect() only works on types which implement FromIterator which must never fail. Implementing FromIterator for Result<Self, Error> is also not an option, because we cannot implement a foreign trait on a foreign type. There are now two possible solutions.

Old

let map = BTreeMap::from([
    ("stackable.tech/managed-by", "stackablectl"),
    ("stackable.tech/vendor", "Stackable"),
]);

let labels = map
    .iter()
    .map(|p| Label::try_from(p))
    .collect::<Result<Vec<Label>, LabelError>>()
    .unwrap();

let labels = Labels::from_iter(labels);

New

let map = BTreeMap::from([
    ("stackable.tech/managed-by", "stackablectl"),
    ("stackable.tech/vendor", "Stackable"),
]);

// Works on any iterator, like Vec<(String, String)> or &[(&str, &str)]
let labels = Labels::try_from_iter(map).unwrap();

@Techassi Techassi self-assigned this Jan 12, 2024
@Techassi Techassi marked this pull request as ready for review January 12, 2024 15:37
@NickLarsenNZ NickLarsenNZ self-requested a review January 15, 2024 08:17
Copy link
Member

@NickLarsenNZ NickLarsenNZ left a comment

Choose a reason for hiding this comment

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

LGTM

@Techassi Techassi added this pull request to the merge queue Jan 15, 2024
Merged via the queue into main with commit 4f6953b Jan 15, 2024
@Techassi Techassi deleted the feat/kvps-iterator-collect branch January 15, 2024 10:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Implement Labels::try_from<&BTreeMap<_,_>>
2 participants