Skip to content

Commit 9f2e438

Browse files
committed
notifications: make update_xbiff() a method
Convert update_xbiff function into a NotificationCommand method and add rustdoc comment. No functional changes. Signed-off-by: Manos Pitsidianakis <[email protected]>
1 parent 6ccbd57 commit 9f2e438

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

meli/src/notifications.rs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,23 @@ impl NotificationCommand {
187187
pub fn new() -> Self {
188188
Self::default()
189189
}
190+
191+
/// Update an `xbiff` file at `path`.
192+
///
193+
/// The ASCII byte `z` is appended to the file, unless the file size is
194+
/// above 128 bytes in which case the file is truncated to 0 bytes.
195+
pub fn update_xbiff(path: &str) -> Result<()> {
196+
let mut file = std::fs::OpenOptions::new()
197+
.append(true) /* writes will append to a file instead of overwriting previous contents */
198+
.create(true) /* a new file will be created if the file does not yet already exist. */
199+
.open(path)?;
200+
if file.metadata()?.len() > 128 {
201+
file.set_len(0)?;
202+
} else {
203+
std::io::Write::write_all(&mut file, b"z")?;
204+
}
205+
Ok(())
206+
}
190207
}
191208

192209
impl std::fmt::Display for NotificationCommand {
@@ -209,7 +226,7 @@ impl Component for NotificationCommand {
209226
if context.settings.notifications.enable {
210227
if *kind == Some(NotificationType::NewMail) {
211228
if let Some(ref path) = context.settings.notifications.xbiff_file_path {
212-
if let Err(err) = update_xbiff(path) {
229+
if let Err(err) = Self::update_xbiff(path) {
213230
log::error!("Could not update xbiff file: {err}.");
214231
}
215232
}
@@ -326,19 +343,6 @@ impl Component for NotificationCommand {
326343
}
327344
}
328345

329-
fn update_xbiff(path: &str) -> Result<()> {
330-
let mut file = std::fs::OpenOptions::new()
331-
.append(true) /* writes will append to a file instead of overwriting previous contents */
332-
.create(true) /* a new file will be created if the file does not yet already exist. */
333-
.open(path)?;
334-
if file.metadata()?.len() > 128 {
335-
file.set_len(0)?;
336-
} else {
337-
std::io::Write::write_all(&mut file, b"z")?;
338-
}
339-
Ok(())
340-
}
341-
342346
#[derive(Debug)]
343347
/// On-screen-display messages.
344348
struct DisplayMessage {

0 commit comments

Comments
 (0)