Remove two useless comments.
[stakingWatchdogWatchdog.git] / src / main.rs
index c854c74..9944ae1 100644 (file)
@@ -1,4 +1,5 @@
 use std::{
+    fmt,
     net::UdpSocket,
     thread,
     time::{self, Duration},
@@ -79,8 +80,9 @@ fn main() -> Result<()> {
                     // Send e-mail.
                     println!("Sending email...");
                     match send_email(
-                        "Watchdog Watchdog ERROR",
-                        &format!("Error: {:?}", error),
+                        "Watchdog Watchdog: Check alive error",
+                        &format!("Error: {}", error),
+                        &config.smtp_relay_address,
                         &config.smtp_login,
                         &config.smtp_password,
                     ) {
@@ -110,6 +112,22 @@ enum PingError {
     WrongMessageReceived(String),
 }
 
+impl fmt::Display for PingError {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        match self {
+            PingError::SocketReceiveError(error) => {
+                write!(f, "Didn't receive any response from watchdog: {}", error)
+            }
+            PingError::SocketSendError(error) => {
+                write!(f, "Unable to send the message: {}", error)
+            }
+            PingError::WrongMessageReceived(message) => {
+                write!(f, "Watchdog replay with a wrong message: {}", message)
+            }
+        }
+    }
+}
+
 fn ping(socket: &UdpSocket, rng: &mut ThreadRng) -> std::result::Result<Duration, PingError> {
     loop {
         let number: u64 = rng.gen();
@@ -152,7 +170,13 @@ fn ping(socket: &UdpSocket, rng: &mut ThreadRng) -> std::result::Result<Duration
     }
 }
 
-fn send_email(title: &str, body: &str, login: &str, pass: &str) -> Result<()> {
+fn send_email(
+    title: &str,
+    body: &str,
+    smtp_relay_address: &str,
+    login: &str,
+    pass: &str,
+) -> Result<()> {
     let email = Message::builder()
         .message_id(None)
         .from("Staking Watchdog Watchdog <redmine@d-lan.net>".parse()?)
@@ -163,12 +187,10 @@ fn send_email(title: &str, body: &str, login: &str, pass: &str) -> Result<()> {
 
     let creds = Credentials::new(login.to_string(), pass.to_string());
 
-    // Open a remote connection to gmail
-    let mailer = SmtpTransport::relay("mail.gandi.net")?
+    let mailer = SmtpTransport::relay(smtp_relay_address)?
         .credentials(creds)
         .build();
 
-    // Send the email
     let response = mailer.send(&email)?;
 
     println!("{:?}", response);