@@ -147,13 +147,13 @@ impl Context {
147
147
148
148
impl std:: fmt:: Display for Song {
149
149
fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
150
- write ! ( f, "{}" , self . keyword ( ) )
150
+ write ! ( f, "{}" , self . display_name ( ) )
151
151
}
152
152
}
153
153
154
154
impl Song {
155
- /// Generate the keyword of this song.
156
- pub fn keyword ( & self ) -> String {
155
+ /// Generate the name of this song.
156
+ fn get_name ( & self , has_separator : bool ) -> String {
157
157
// {Song Name}
158
158
let mut keyword = self . name . to_string ( ) ;
159
159
@@ -164,41 +164,62 @@ impl Song {
164
164
let max_idx = self . artists . len ( ) - 1 ;
165
165
166
166
// Add hyphen between the song name and the following artist name.
167
- keyword. push_str ( " - " ) ;
167
+ if has_separator {
168
+ keyword. push_str ( " - " ) ;
169
+ } else {
170
+ keyword. push ( ' ' ) ;
171
+ }
168
172
169
173
for ( idx, artist) in self . artists . iter ( ) . enumerate ( ) {
170
174
// "[keyword] {artist.name}"
171
175
keyword. push_str ( & artist. name ) ;
172
176
173
177
if idx != max_idx {
174
- // ", " if this is not the last item.
175
- keyword. push_str ( ", " ) ;
178
+ if has_separator {
179
+ // ", " if this is not the last item.
180
+ keyword. push_str ( ", " ) ;
181
+ } else {
182
+ keyword. push ( ' ' ) ;
183
+ }
176
184
}
177
185
}
178
186
179
- // {Song name} - {Artist 1's name}, {Artist 2's name}[, ...]
187
+ // (has_separator) {Song name} - {Artist 1's name}, {Artist 2's name}[, ...]
188
+ // (!has_separator) {Song name} {Artist 1's name} {Artist 2's name}[ ...]
180
189
keyword
181
190
}
191
+
192
+ /// Generate the display name of this song.
193
+ pub fn display_name ( & self ) -> String {
194
+ self . get_name ( true )
195
+ }
196
+
197
+ /// Generate the keyword of this song.
198
+ pub fn keyword ( & self ) -> String {
199
+ self . get_name ( false )
200
+ }
182
201
}
183
202
184
203
#[ cfg( test) ]
185
204
mod tests {
186
205
use crate :: { Artist , Song } ;
187
206
188
207
#[ test]
189
- fn test_keyword_with_no_artist ( ) {
208
+ fn test_name_with_no_artist ( ) {
190
209
let s = Song {
191
210
id : "114514" . to_string ( ) ,
192
211
name : "Lost River" . to_string ( ) ,
193
212
artists : vec ! [ ] ,
194
213
..Default :: default ( )
195
214
} ;
196
215
216
+ assert_eq ! ( s. display_name( ) , "Lost River" ) ;
197
217
assert_eq ! ( s. keyword( ) , "Lost River" ) ;
218
+ assert_eq ! ( format!( "{s}" ) , "Lost River" ) ;
198
219
}
199
220
200
221
#[ test]
201
- fn test_keyword_with_single_artist ( ) {
222
+ fn test_name_with_single_artist ( ) {
202
223
let s = Song {
203
224
id : "123" . to_string ( ) ,
204
225
name : "TT" . to_string ( ) ,
@@ -209,11 +230,13 @@ mod tests {
209
230
..Default :: default ( )
210
231
} ;
211
232
212
- assert_eq ! ( s. keyword( ) , "TT - Twice" ) ;
233
+ assert_eq ! ( s. display_name( ) , "TT - Twice" ) ;
234
+ assert_eq ! ( s. keyword( ) , "TT Twice" ) ;
235
+ assert_eq ! ( format!( "{s}" ) , "TT - Twice" ) ;
213
236
}
214
237
215
238
#[ test]
216
- fn test_keyword_with_multiple_artist ( ) {
239
+ fn test_display_name_with_multiple_artist ( ) {
217
240
let s = Song {
218
241
id : "123" . to_string ( ) ,
219
242
name : "Hope for Tomorrow - Melchi Remix" . to_string ( ) ,
@@ -234,8 +257,16 @@ mod tests {
234
257
..Default :: default ( )
235
258
} ;
236
259
260
+ assert_eq ! (
261
+ s. display_name( ) ,
262
+ "Hope for Tomorrow - Melchi Remix - Alex H, Z8phyR, Melchi"
263
+ ) ;
237
264
assert_eq ! (
238
265
s. keyword( ) ,
266
+ "Hope for Tomorrow - Melchi Remix Alex H Z8phyR Melchi"
267
+ ) ;
268
+ assert_eq ! (
269
+ format!( "{s}" ) ,
239
270
"Hope for Tomorrow - Melchi Remix - Alex H, Z8phyR, Melchi"
240
271
) ;
241
272
}
0 commit comments