-
Notifications
You must be signed in to change notification settings - Fork 124
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
Added a builder like struct for the search function (Related with issue ) #364
base: master
Are you sure you want to change the base?
Conversation
Cargo.toml
Outdated
@@ -41,6 +41,7 @@ log = "0.4.14" | |||
maybe-async = "0.2.6" | |||
serde = { version = "1.0.130", default-features = false } | |||
serde_json = "1.0.67" | |||
strum = { version = "0.24.0", features = ["derive"] } | |||
sha2 = "0.10.0" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it necessary to impose the strum
crate? We are cautious about including a new crate, it will increase compile time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will it really increase compile time ? As strum is built while building models which are built before the main crate. Anyway I think moving SearchFilter to the models is a good idea and will solve this problem
examples/search_query.rs
Outdated
.album("Another Album") | ||
.into(); | ||
|
||
println!("{}", query); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's not a full example, right? The SearchQuery
should combine with the search
endpoint?
I tried an other approach to ensure type safety. Please tell me what you think about this before I try to implement testing and more example around it |
After rereading the Spotify's document, I think its explanation about the restriction between Just adding a builder for the |
This reverts commit 7995a07.
- Moved SearchFilter to models crate - Fixed unused import - SearchQuery now hold only string references to not clone Strings - From<SearchQuery> now take an immutable references - Removed strum from main crate
I reverted my previous changes to go back to the builder pattern. |
In my opinion, there are two ways to test the
let query_string = SearchQuery::default().album("arrival").artist("abba").tag_new("test");
// the query_string could be:
// 1. `ablum:arrival artist:abba test`
// 2. `artist:abba ablum:arrival test`
// 3. `test artist:abba ablum:arrival`
// 4. ...
// we could split query string by whitespace and assert the splited strings contain `ablum:arrival` `artist:abba` `test`
let query_words: Vec<String> = query_string.split(' ').collect();
assert_eq!(3, query_words.len());
assert!(query_words.contains('ablum:arrival'));
assert!(query_words.contains('artist:abba'));
// ... |
- Replac HashMap by BTreeMap to ensure order - Add documentation - Add unit tests
I finally chose the BTreeMap because it's a bit tricky to split correctly the query as there can be spaces inside album, track.. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome idea, thanks for the contribution!!
src/search/mod.rs
Outdated
impl<'a> SearchQuery<'a> { | ||
/// Basic filter where the given string can be anything | ||
pub fn any(&mut self, str: &'a str) -> &mut Self { | ||
self.no_filter_query = str; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To me, it'd be more intuitive if calling any
multiple times appended its contents, instead of replace them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So any would comport differently from the other fields ? Or should I change this for all fields ?
I'm a bit confused about what did just happen in the pipeline. It seems it complained about some code which is inside the documentation. I never really worked with rust doc so what happened ? |
The pipeline complained about that it's unable to find the definition of /// Exemple
/// ```rust
/// use SearchQuery;
/// SearchQuery::default()
/// .any("foo")
/// .album("bar")
/// // Filter on album containing "bar" and anything containing "foo"
/// ```
|
Message to comment on stale PRs. If none provided, will not mark PRs stale |
Description
These changes add a struct to build well formatted query to pass to the search method (fixes #354).
These are not breaking changes as you can still pass a simple String to the method.
It can still be improve as it doesn't prevent users to pass wrong filter with wrong SearchType (give a track title to search an album for example, which is not supporte by Spotify API).
Motivation and Context
Make writing searches easier
Dependencies
Adding strum to the main project
Type of change
Please delete options that are not relevant.
How has this been tested?
I've modified one test case to use the struct instead of a basic &str. As the old test still passes, these changes are non breaking.
I also wrote a simple example which printed the String generated by the struct to verify that it was right with its inputs. This file is included in this PR but will probably be removed if it is accepted.
Please also list any relevant details for your test configuration
Is this change properly documented?
Please make sure you've properly documented the changes you're making.
Don't forget to add an entry to the CHANGELOG if necessary (new features, breaking changes, relevant internal improvements).