lastfm_edit/
edit.rs

1/// Represents a scrobble edit operation.
2///
3/// This structure contains all the information needed to edit a specific scrobble
4/// on Last.fm, including both the original and new metadata values.
5///
6/// # Examples
7///
8/// ```rust
9/// use lastfm_edit::ScrobbleEdit;
10///
11/// // Create an edit to fix a track name
12/// let edit = ScrobbleEdit::from_track_info(
13///     "Paranoid Andriod", // original (misspelled)
14///     "OK Computer",
15///     "Radiohead",
16///     1640995200
17/// )
18/// .with_track_name("Paranoid Android"); // corrected
19///
20/// // Create an edit to change artist name
21/// let edit = ScrobbleEdit::from_track_info(
22///     "Creep",
23///     "Pablo Honey",
24///     "Radio Head", // original (wrong)
25///     1640995200
26/// )
27/// .with_artist_name("Radiohead") // corrected
28/// .with_edit_all(true); // update all instances
29/// ```
30#[derive(Debug, Clone)]
31pub struct ScrobbleEdit {
32    /// Original track name as it appears in the scrobble
33    pub track_name_original: String,
34    /// Original album name as it appears in the scrobble
35    pub album_name_original: String,
36    /// Original artist name as it appears in the scrobble
37    pub artist_name_original: String,
38    /// Original album artist name as it appears in the scrobble
39    pub album_artist_name_original: String,
40
41    /// New track name to set
42    pub track_name: String,
43    /// New album name to set
44    pub album_name: String,
45    /// New artist name to set
46    pub artist_name: String,
47    /// New album artist name to set
48    pub album_artist_name: String,
49
50    /// Unix timestamp of the scrobble to edit
51    ///
52    /// This identifies the specific scrobble instance to modify.
53    pub timestamp: u64,
54    /// Whether to edit all instances or just this specific scrobble
55    ///
56    /// When `true`, Last.fm will update all scrobbles with matching metadata.
57    /// When `false`, only this specific scrobble (identified by timestamp) is updated.
58    pub edit_all: bool,
59}
60
61/// Response from a scrobble edit operation.
62///
63/// This structure contains the result of attempting to edit a scrobble,
64/// including success status and any error messages.
65///
66/// # Examples
67///
68/// ```rust
69/// use lastfm_edit::EditResponse;
70///
71/// let response = EditResponse {
72///     success: true,
73///     message: Some("Track name updated successfully".to_string()),
74/// };
75///
76/// if response.success {
77///     println!("Edit succeeded: {}", response.message.unwrap_or_default());
78/// } else {
79///     eprintln!("Edit failed: {}", response.message.unwrap_or_default());
80/// }
81/// ```
82#[derive(Debug)]
83pub struct EditResponse {
84    /// Whether the edit operation was successful
85    pub success: bool,
86    /// Optional message describing the result or any errors
87    pub message: Option<String>,
88}
89
90impl ScrobbleEdit {
91    /// Create a new [`ScrobbleEdit`] with all required fields.
92    ///
93    /// This is the most general constructor that allows setting all fields.
94    /// For convenience, consider using [`from_track_info`](Self::from_track_info) instead.
95    ///
96    /// # Arguments
97    ///
98    /// * `track_name_original` - The current track name in the scrobble
99    /// * `album_name_original` - The current album name in the scrobble
100    /// * `artist_name_original` - The current artist name in the scrobble
101    /// * `album_artist_name_original` - The current album artist name in the scrobble
102    /// * `track_name` - The new track name to set
103    /// * `album_name` - The new album name to set
104    /// * `artist_name` - The new artist name to set
105    /// * `album_artist_name` - The new album artist name to set
106    /// * `timestamp` - Unix timestamp identifying the scrobble
107    /// * `edit_all` - Whether to edit all matching scrobbles or just this one
108    #[allow(clippy::too_many_arguments)]
109    pub fn new(
110        track_name_original: String,
111        album_name_original: String,
112        artist_name_original: String,
113        album_artist_name_original: String,
114        track_name: String,
115        album_name: String,
116        artist_name: String,
117        album_artist_name: String,
118        timestamp: u64,
119        edit_all: bool,
120    ) -> Self {
121        Self {
122            track_name_original,
123            album_name_original,
124            artist_name_original,
125            album_artist_name_original,
126            track_name,
127            album_name,
128            artist_name,
129            album_artist_name,
130            timestamp,
131            edit_all,
132        }
133    }
134
135    /// Create an edit request from track information (convenience constructor).
136    ///
137    /// This constructor creates a [`ScrobbleEdit`] with the new values initially
138    /// set to the same as the original values. Use the builder methods like
139    /// [`with_track_name`](Self::with_track_name) to specify what should be changed.
140    ///
141    /// # Arguments
142    ///
143    /// * `original_track` - The current track name
144    /// * `original_album` - The current album name
145    /// * `original_artist` - The current artist name
146    /// * `timestamp` - Unix timestamp identifying the scrobble
147    ///
148    /// # Examples
149    ///
150    /// ```rust
151    /// use lastfm_edit::ScrobbleEdit;
152    ///
153    /// let edit = ScrobbleEdit::from_track_info(
154    ///     "Highway to Hell",
155    ///     "Highway to Hell",
156    ///     "AC/DC",
157    ///     1640995200
158    /// )
159    /// .with_track_name("Highway to Hell (Remastered)");
160    /// ```
161    pub fn from_track_info(
162        original_track: &str,
163        original_album: &str,
164        original_artist: &str,
165        timestamp: u64,
166    ) -> Self {
167        Self::new(
168            original_track.to_string(),
169            original_album.to_string(),
170            original_artist.to_string(),
171            original_artist.to_string(), // album_artist defaults to artist
172            original_track.to_string(),
173            original_album.to_string(),
174            original_artist.to_string(),
175            original_artist.to_string(), // album_artist defaults to artist
176            timestamp,
177            false, // edit_all defaults to false
178        )
179    }
180
181    /// Set the new track name.
182    ///
183    /// # Examples
184    ///
185    /// ```rust
186    /// # use lastfm_edit::ScrobbleEdit;
187    /// let edit = ScrobbleEdit::from_track_info("Wrong Name", "Album", "Artist", 1640995200)
188    ///     .with_track_name("Correct Name");
189    /// ```
190    pub fn with_track_name(mut self, track_name: &str) -> Self {
191        self.track_name = track_name.to_string();
192        self
193    }
194
195    /// Set the new album name.
196    ///
197    /// # Examples
198    ///
199    /// ```rust
200    /// # use lastfm_edit::ScrobbleEdit;
201    /// let edit = ScrobbleEdit::from_track_info("Track", "Wrong Album", "Artist", 1640995200)
202    ///     .with_album_name("Correct Album");
203    /// ```
204    pub fn with_album_name(mut self, album_name: &str) -> Self {
205        self.album_name = album_name.to_string();
206        self
207    }
208
209    /// Set the new artist name.
210    ///
211    /// This also sets the album artist name to the same value.
212    ///
213    /// # Examples
214    ///
215    /// ```rust
216    /// # use lastfm_edit::ScrobbleEdit;
217    /// let edit = ScrobbleEdit::from_track_info("Track", "Album", "Wrong Artist", 1640995200)
218    ///     .with_artist_name("Correct Artist");
219    /// ```
220    pub fn with_artist_name(mut self, artist_name: &str) -> Self {
221        self.artist_name = artist_name.to_string();
222        self.album_artist_name = artist_name.to_string();
223        self
224    }
225
226    /// Set whether to edit all instances of this track.
227    ///
228    /// When `true`, Last.fm will update all scrobbles with the same metadata.
229    /// When `false` (default), only the specific scrobble is updated.
230    ///
231    /// # Examples
232    ///
233    /// ```rust
234    /// # use lastfm_edit::ScrobbleEdit;
235    /// let edit = ScrobbleEdit::from_track_info("Track", "Album", "Artist", 1640995200)
236    ///     .with_track_name("New Name")
237    ///     .with_edit_all(true); // Update all instances
238    /// ```
239    pub fn with_edit_all(mut self, edit_all: bool) -> Self {
240        self.edit_all = edit_all;
241        self
242    }
243}