-
Notifications
You must be signed in to change notification settings - Fork 129
Added a builder like struct for the search function (Related with issue ) #364
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
Open
GartoxFR
wants to merge
9
commits into
ramsayleung:master
Choose a base branch
from
GartoxFR:type-safe-search
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
82878ed
Added a builder like struct for the search function
GartoxFR 7995a07
New type safe system with traits
GartoxFR 320d0f5
Revert "New type safe system with traits"
GartoxFR 5b5e60d
Fixed comments :
GartoxFR 87c9c2d
SearchQuery :
GartoxFR 92ae1a4
Removed useless example
GartoxFR 6044d8f
Fix english mistakes
GartoxFR 04e4f8a
Changed "any" query to append when used multiple times
GartoxFR 3462c77
Fix missing import in doc
GartoxFR File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| use rspotify::search::SearchQuery; | ||
|
|
||
| #[tokio::main] | ||
| async fn main() { | ||
| let query: String = SearchQuery::default() | ||
| .any("any") | ||
| .artist("Lisa") | ||
| .track("Demon slayer") | ||
| .album("Another Album") | ||
| .into(); | ||
|
|
||
| println!("{}", query); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's not a full example, right? The |
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| use std::collections::HashMap; | ||
|
|
||
| use strum::Display; | ||
|
|
||
| #[derive(Debug, Display, PartialEq, Eq, Hash)] | ||
| #[strum(serialize_all = "snake_case")] | ||
|
GartoxFR marked this conversation as resolved.
Outdated
|
||
| pub enum SearchFilter { | ||
| Album, | ||
| Artist, | ||
| Track, | ||
| Year, | ||
| Upc, | ||
| #[strum(serialize = "tag:hipster")] | ||
| TagHipster, | ||
| #[strum(serialize = "tag:new")] | ||
| TagNew, | ||
| Isrc, | ||
| Genre, | ||
| } | ||
|
|
||
| #[derive(Debug, Default)] | ||
| pub struct SearchQuery { | ||
| no_filter_query: String, | ||
| query_map: HashMap<SearchFilter, String>, | ||
| } | ||
|
GartoxFR marked this conversation as resolved.
|
||
|
|
||
| impl SearchQuery { | ||
| pub fn any<T: Into<String>>(&mut self, str: T) -> &mut Self { | ||
| self.no_filter_query = str.into(); | ||
| self | ||
| } | ||
|
|
||
| pub fn album<T: Into<String>>(&mut self, str: T) -> &mut Self { | ||
| self.query_map.insert(SearchFilter::Album, str.into()); | ||
| self | ||
| } | ||
|
|
||
| pub fn artist<T: Into<String>>(&mut self, str: T) -> &mut Self { | ||
| self.query_map.insert(SearchFilter::Artist, str.into()); | ||
| self | ||
| } | ||
|
|
||
| pub fn track<T: Into<String>>(&mut self, str: T) -> &mut Self { | ||
| self.query_map.insert(SearchFilter::Track, str.into()); | ||
| self | ||
| } | ||
|
|
||
| pub fn year<T: Into<String>>(&mut self, str: T) -> &mut Self { | ||
| self.query_map.insert(SearchFilter::Year, str.into()); | ||
| self | ||
| } | ||
|
|
||
| pub fn upc<T: Into<String>>(&mut self, str: T) -> &mut Self { | ||
| self.query_map.insert(SearchFilter::Upc, str.into()); | ||
| self | ||
| } | ||
|
|
||
| pub fn tag_new(&mut self) -> &mut Self { | ||
| self.query_map.insert(SearchFilter::TagNew, "".into()); | ||
| self | ||
| } | ||
|
|
||
| pub fn tag_hipster(&mut self) -> &mut Self { | ||
| self.query_map.insert(SearchFilter::TagHipster, "".into()); | ||
| self | ||
| } | ||
|
|
||
| pub fn isrc<T: Into<String>>(&mut self, str: T) -> &mut Self { | ||
| self.query_map.insert(SearchFilter::Isrc, str.into()); | ||
| self | ||
| } | ||
|
|
||
| pub fn genre<T: Into<String>>(&mut self, str: T) -> &mut Self { | ||
| self.query_map.insert(SearchFilter::Genre, str.into()); | ||
| self | ||
| } | ||
| } | ||
|
|
||
| impl From<&mut SearchQuery> for String { | ||
| fn from(val: &mut SearchQuery) -> Self { | ||
|
GartoxFR marked this conversation as resolved.
Outdated
|
||
| let mut rep = val.no_filter_query.clone(); | ||
| rep.push(' '); | ||
| rep.push_str( | ||
| val.query_map | ||
| .iter() | ||
| .map(|entry| match entry.0 { | ||
| SearchFilter::TagNew | SearchFilter::TagHipster => format!("{} ", entry.0), | ||
| _ => format!("{}:{} ", entry.0, entry.1), | ||
| }) | ||
| .collect::<String>() | ||
| .as_str(), | ||
| ); | ||
|
|
||
| rep | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
strumcrate? 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