Skip to content

Commit 41f3716

Browse files
committed
refactor!(types): Separate display_name and keyword
Closed #183
1 parent cc1e6ed commit 41f3716

File tree

1 file changed

+42
-11
lines changed

1 file changed

+42
-11
lines changed

types/src/lib.rs

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,13 @@ impl Context {
147147

148148
impl std::fmt::Display for Song {
149149
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
150-
write!(f, "{}", self.keyword())
150+
write!(f, "{}", self.display_name())
151151
}
152152
}
153153

154154
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 {
157157
// {Song Name}
158158
let mut keyword = self.name.to_string();
159159

@@ -164,41 +164,62 @@ impl Song {
164164
let max_idx = self.artists.len() - 1;
165165

166166
// 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+
}
168172

169173
for (idx, artist) in self.artists.iter().enumerate() {
170174
// "[keyword] {artist.name}"
171175
keyword.push_str(&artist.name);
172176

173177
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+
}
176184
}
177185
}
178186

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}[ ...]
180189
keyword
181190
}
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+
}
182201
}
183202

184203
#[cfg(test)]
185204
mod tests {
186205
use crate::{Artist, Song};
187206

188207
#[test]
189-
fn test_keyword_with_no_artist() {
208+
fn test_name_with_no_artist() {
190209
let s = Song {
191210
id: "114514".to_string(),
192211
name: "Lost River".to_string(),
193212
artists: vec![],
194213
..Default::default()
195214
};
196215

216+
assert_eq!(s.display_name(), "Lost River");
197217
assert_eq!(s.keyword(), "Lost River");
218+
assert_eq!(format!("{s}"), "Lost River");
198219
}
199220

200221
#[test]
201-
fn test_keyword_with_single_artist() {
222+
fn test_name_with_single_artist() {
202223
let s = Song {
203224
id: "123".to_string(),
204225
name: "TT".to_string(),
@@ -209,11 +230,13 @@ mod tests {
209230
..Default::default()
210231
};
211232

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");
213236
}
214237

215238
#[test]
216-
fn test_keyword_with_multiple_artist() {
239+
fn test_display_name_with_multiple_artist() {
217240
let s = Song {
218241
id: "123".to_string(),
219242
name: "Hope for Tomorrow - Melchi Remix".to_string(),
@@ -234,8 +257,16 @@ mod tests {
234257
..Default::default()
235258
};
236259

260+
assert_eq!(
261+
s.display_name(),
262+
"Hope for Tomorrow - Melchi Remix - Alex H, Z8phyR, Melchi"
263+
);
237264
assert_eq!(
238265
s.keyword(),
266+
"Hope for Tomorrow - Melchi Remix Alex H Z8phyR Melchi"
267+
);
268+
assert_eq!(
269+
format!("{s}"),
239270
"Hope for Tomorrow - Melchi Remix - Alex H, Z8phyR, Melchi"
240271
);
241272
}

0 commit comments

Comments
 (0)