|  | 
|  | 1 | +//! This module provides functionality for generating completion candidates for | 
|  | 2 | +//! things like the import/export commands in the CLI. | 
|  | 3 | +
 | 
|  | 4 | +use clap::builder::StyledStr; | 
|  | 5 | +use clap_complete::CompletionCandidate; | 
|  | 6 | +use mecomp_core::rpc::MusicPlayerClient; | 
|  | 7 | +use mecomp_storage::db::schemas::dynamic::query::Compile; | 
|  | 8 | + | 
|  | 9 | +#[derive(Debug, PartialEq, Eq)] | 
|  | 10 | +pub enum CompletableTable { | 
|  | 11 | +    Artist, | 
|  | 12 | +    Album, | 
|  | 13 | +    Song, | 
|  | 14 | +    Playlist, | 
|  | 15 | +    DynamicPlaylist, | 
|  | 16 | +    Collection, | 
|  | 17 | +} | 
|  | 18 | + | 
|  | 19 | +/// Generate completion candidates for items in the database, | 
|  | 20 | +/// Given the table name, returns a function that can be used to get completion candidates | 
|  | 21 | +/// from that table. | 
|  | 22 | +pub fn complete_things(table: CompletableTable) -> impl Fn() -> Vec<CompletionCandidate> { | 
|  | 23 | +    move || { | 
|  | 24 | +        let rt = tokio::runtime::Builder::new_multi_thread() | 
|  | 25 | +            .enable_all() | 
|  | 26 | +            .build() | 
|  | 27 | +            .expect("Failed to create Tokio runtime"); | 
|  | 28 | + | 
|  | 29 | +        let g = rt.enter(); | 
|  | 30 | + | 
|  | 31 | +        let handle = tokio::runtime::Handle::current(); | 
|  | 32 | + | 
|  | 33 | +        let client: MusicPlayerClient = match handle.block_on(mecomp_core::rpc::init_client(6600)) { | 
|  | 34 | +            Ok(client) => client, | 
|  | 35 | +            Err(e) => { | 
|  | 36 | +                eprintln!("Failed to connect to daemon: {e}"); | 
|  | 37 | +                return vec![]; | 
|  | 38 | +            } | 
|  | 39 | +        }; | 
|  | 40 | +        let ctx = tarpc::context::current(); | 
|  | 41 | + | 
|  | 42 | +        let candidates = match table { | 
|  | 43 | +            CompletableTable::Song => get_song_candidates(&handle, &client, ctx), | 
|  | 44 | +            CompletableTable::Album => get_album_candidates(&handle, &client, ctx), | 
|  | 45 | +            CompletableTable::Artist => get_artist_candidates(&handle, &client, ctx), | 
|  | 46 | +            CompletableTable::Playlist => get_playlist_candidates(&handle, &client, ctx), | 
|  | 47 | +            CompletableTable::DynamicPlaylist => { | 
|  | 48 | +                get_dynamic_playlist_candidates(&handle, &client, ctx) | 
|  | 49 | +            } | 
|  | 50 | +            CompletableTable::Collection => get_collection_candidates(&handle, &client, ctx), | 
|  | 51 | +        }; | 
|  | 52 | +        drop(g); // Exit the Tokio runtime context | 
|  | 53 | + | 
|  | 54 | +        candidates | 
|  | 55 | +            .iter() | 
|  | 56 | +            .cloned() | 
|  | 57 | +            .map(|(id, help)| CompletionCandidate::new(id).help(Some(help))) | 
|  | 58 | +            .collect::<Vec<_>>() | 
|  | 59 | +    } | 
|  | 60 | +} | 
|  | 61 | + | 
|  | 62 | +fn get_song_candidates( | 
|  | 63 | +    rt: &tokio::runtime::Handle, | 
|  | 64 | +    client: &MusicPlayerClient, | 
|  | 65 | +    ctx: tarpc::context::Context, | 
|  | 66 | +) -> Vec<(String, StyledStr)> { | 
|  | 67 | +    let response = rt.block_on(client.library_songs_brief(ctx)); | 
|  | 68 | +    if let Err(e) = response { | 
|  | 69 | +        eprintln!("Failed to fetch songs: {e}"); | 
|  | 70 | +        return vec![]; | 
|  | 71 | +    } | 
|  | 72 | +    let songs = response.unwrap().unwrap_or_default(); | 
|  | 73 | + | 
|  | 74 | +    songs | 
|  | 75 | +        .into_iter() | 
|  | 76 | +        .map(|song| { | 
|  | 77 | +            ( | 
|  | 78 | +                song.id.key().to_string(), | 
|  | 79 | +                StyledStr::from(format!( | 
|  | 80 | +                    "\"{}\" (by: {:?}, album: {})", | 
|  | 81 | +                    song.title, song.artist, song.album | 
|  | 82 | +                )), | 
|  | 83 | +            ) | 
|  | 84 | +        }) | 
|  | 85 | +        .collect() | 
|  | 86 | +} | 
|  | 87 | + | 
|  | 88 | +fn get_album_candidates( | 
|  | 89 | +    rt: &tokio::runtime::Handle, | 
|  | 90 | +    client: &MusicPlayerClient, | 
|  | 91 | +    ctx: tarpc::context::Context, | 
|  | 92 | +) -> Vec<(String, StyledStr)> { | 
|  | 93 | +    let response = rt.block_on(client.library_albums_brief(ctx)); | 
|  | 94 | +    if let Err(e) = response { | 
|  | 95 | +        eprintln!("Failed to fetch albums: {e}"); | 
|  | 96 | +        return vec![]; | 
|  | 97 | +    } | 
|  | 98 | +    let albums = response.unwrap().unwrap_or_default(); | 
|  | 99 | + | 
|  | 100 | +    albums | 
|  | 101 | +        .into_iter() | 
|  | 102 | +        .map(|album| { | 
|  | 103 | +            ( | 
|  | 104 | +                album.id.key().to_string(), | 
|  | 105 | +                StyledStr::from(format!( | 
|  | 106 | +                    "\"{}\" (by: {:?}, {} songs)", | 
|  | 107 | +                    album.title, album.artist, album.song_count | 
|  | 108 | +                )), | 
|  | 109 | +            ) | 
|  | 110 | +        }) | 
|  | 111 | +        .collect() | 
|  | 112 | +} | 
|  | 113 | + | 
|  | 114 | +fn get_artist_candidates( | 
|  | 115 | +    rt: &tokio::runtime::Handle, | 
|  | 116 | +    client: &MusicPlayerClient, | 
|  | 117 | +    ctx: tarpc::context::Context, | 
|  | 118 | +) -> Vec<(String, StyledStr)> { | 
|  | 119 | +    let response = rt.block_on(client.library_artists_brief(ctx)); | 
|  | 120 | +    if let Err(e) = response { | 
|  | 121 | +        eprintln!("Failed to fetch artists: {e}"); | 
|  | 122 | +        return vec![]; | 
|  | 123 | +    } | 
|  | 124 | +    let artists = response.unwrap().unwrap_or_default(); | 
|  | 125 | + | 
|  | 126 | +    artists | 
|  | 127 | +        .into_iter() | 
|  | 128 | +        .map(|artist| { | 
|  | 129 | +            ( | 
|  | 130 | +                artist.id.key().to_string(), | 
|  | 131 | +                StyledStr::from(format!( | 
|  | 132 | +                    "\"{}\" ({} albums, {} songs)", | 
|  | 133 | +                    artist.name, artist.albums, artist.songs | 
|  | 134 | +                )), | 
|  | 135 | +            ) | 
|  | 136 | +        }) | 
|  | 137 | +        .collect() | 
|  | 138 | +} | 
|  | 139 | + | 
|  | 140 | +fn get_playlist_candidates( | 
|  | 141 | +    rt: &tokio::runtime::Handle, | 
|  | 142 | +    client: &MusicPlayerClient, | 
|  | 143 | +    ctx: tarpc::context::Context, | 
|  | 144 | +) -> Vec<(String, StyledStr)> { | 
|  | 145 | +    let response = rt.block_on(client.playlist_list(ctx)); | 
|  | 146 | +    if let Err(e) = response { | 
|  | 147 | +        eprintln!("Failed to fetch playlists: {e}"); | 
|  | 148 | +        return vec![]; | 
|  | 149 | +    } | 
|  | 150 | +    let playlists = response.unwrap(); | 
|  | 151 | + | 
|  | 152 | +    playlists | 
|  | 153 | +        .into_iter() | 
|  | 154 | +        .map(|playlist| { | 
|  | 155 | +            ( | 
|  | 156 | +                playlist.id.key().to_string(), | 
|  | 157 | +                StyledStr::from(format!( | 
|  | 158 | +                    "\"{}\" ({} songs, {:?})", | 
|  | 159 | +                    playlist.name, playlist.songs, playlist.runtime | 
|  | 160 | +                )), | 
|  | 161 | +            ) | 
|  | 162 | +        }) | 
|  | 163 | +        .collect() | 
|  | 164 | +} | 
|  | 165 | + | 
|  | 166 | +fn get_dynamic_playlist_candidates( | 
|  | 167 | +    rt: &tokio::runtime::Handle, | 
|  | 168 | +    client: &MusicPlayerClient, | 
|  | 169 | +    ctx: tarpc::context::Context, | 
|  | 170 | +) -> Vec<(String, StyledStr)> { | 
|  | 171 | +    let response = rt.block_on(client.dynamic_playlist_list(ctx)); | 
|  | 172 | +    if let Err(e) = response { | 
|  | 173 | +        eprintln!("Failed to fetch dynamic playlists: {e}"); | 
|  | 174 | +        return vec![]; | 
|  | 175 | +    } | 
|  | 176 | +    let playlists = response.unwrap(); | 
|  | 177 | + | 
|  | 178 | +    playlists | 
|  | 179 | +        .into_iter() | 
|  | 180 | +        .map(|playlist| { | 
|  | 181 | +            ( | 
|  | 182 | +                playlist.id.key().to_string(), | 
|  | 183 | +                StyledStr::from(format!( | 
|  | 184 | +                    "\"{}\" ({})", | 
|  | 185 | +                    playlist.name, | 
|  | 186 | +                    playlist.query.compile_for_storage() | 
|  | 187 | +                )), | 
|  | 188 | +            ) | 
|  | 189 | +        }) | 
|  | 190 | +        .collect() | 
|  | 191 | +} | 
|  | 192 | + | 
|  | 193 | +fn get_collection_candidates( | 
|  | 194 | +    rt: &tokio::runtime::Handle, | 
|  | 195 | +    client: &MusicPlayerClient, | 
|  | 196 | +    ctx: tarpc::context::Context, | 
|  | 197 | +) -> Vec<(String, StyledStr)> { | 
|  | 198 | +    let response = rt.block_on(client.collection_list(ctx)); | 
|  | 199 | +    if let Err(e) = response { | 
|  | 200 | +        eprintln!("Failed to fetch collections: {e}"); | 
|  | 201 | +        return vec![]; | 
|  | 202 | +    } | 
|  | 203 | +    let collections = response.unwrap(); | 
|  | 204 | + | 
|  | 205 | +    collections | 
|  | 206 | +        .into_iter() | 
|  | 207 | +        .map(|collection| { | 
|  | 208 | +            ( | 
|  | 209 | +                collection.id.key().to_string(), | 
|  | 210 | +                StyledStr::from(format!( | 
|  | 211 | +                    "\"{}\" ({} songs, {:?})", | 
|  | 212 | +                    collection.name, collection.songs, collection.runtime | 
|  | 213 | +                )), | 
|  | 214 | +            ) | 
|  | 215 | +        }) | 
|  | 216 | +        .collect() | 
|  | 217 | +} | 
0 commit comments