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

Rewrite several prompts to take IntoIterator<Item: ToString> instead of &[T] #223

Closed
wants to merge 3 commits into from

Conversation

waterlubber
Copy link

This allows for much more flexibility when generating prompts. The user can pass in, for example, the output of a .map without having to collect it into a vector and provide a slice.
Since all these functions do is run a simple for loop over the input, it's an easy change to make. Existing code should continue to work, as &[T] implements IntoIterator<Item: T> by default.

…d of `&[T]`

This allows for much more flexibility when generating prompts. The user can pass in, for example, the output of a `.map` without having to collect it into a vector and provide a slice.
Since all these functions do is run a simple for loop over the input, it's an easy change to make. Existing code should continue to work, as &[T] implements IntoIterator<Item: T> by default.
@waterlubber
Copy link
Author

54181b0 should fix these problems. By mistake, I reordered all the other terms; so 2e280e6 fixes that. Sorry about that.

@pksunkara
Copy link
Collaborator

Existing code should continue to work, as &[T] implements IntoIterator<Item: T> by default.

Can you try it locally? I remember we had issues about that in Clap.

// Try giving it directly like this
.items(&["one", "two"])

clap-rs/clap#2857
clap-rs/clap#2870
clap-rs/clap#4081

I think this is a breaking change. (I don't mind it but I wanted to confirm)

@waterlubber
Copy link
Author

waterlubber commented Oct 22, 2022

I just ran some basic tests to see if things still worked in an intuitive manner:

use crate::{MultiSelect, Select, Sort};

    #[cfg(feature = "fuzzy-select")]
    #[test]
    fn fuzzy_test() {
        use crate::FuzzySelect;
        let s1 = FuzzySelect::new()
            .items(&["ham", "muenster", "bread"])
            .with_prompt("Select Something?")
            .interact();
        let items = vec![("foo", true), ("bar", false), ("baz", true)];
        let s2 = MultiSelect::new().items_checked(&items).interact();
        let s22 = MultiSelect::new()
            .items_checked(&[("test", true), ("test2", false)])
            .interact();
        let s3 = Select::new().items(["a"]).interact();
        let items_2 = items.iter().map(|(a, b)| a);
        let s4 = Sort::new().items(items_2).interact();
    }

They do, but there is a small problem with MultiSelect. IntoIterator on Vec gives references to tuples (&(T, bool)) instead of tuples ((T, bool)).

I can slightly change the function definition to fix this:

 pub fn items_checked<'a, T, I>(&mut self, items: T) -> &mut Self
    where
        T: IntoIterator<Item = &'a (I, bool)>,
        I: ToString + 'a,
    {
        for (item, checked) in items {
            self.items.push(item.to_string());
            self.defaults.push(*checked);
        }
        self
    }

Which makes it work very well with the tests above. This might be a breaking change, but I'm not entirely sure. Rust might automatically coerce these types. If this is an acceptable solution, I can go ahead and commit this change to the PR.

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