telegram-client 1.8.69

Telegram client bindings for TDLib
Documentation
use std::any::Any;
use std::fmt::Debug;
use std::{error, fmt};

use rtdlib::errors::RTDError;

pub trait TGDatable: Debug {
  fn as_any(&self) -> &dyn Any;
}

#[derive(Debug)]
pub struct TGError {
  key: &'static str,
  message: Option<String>,
  data: Option<Box<dyn TGDatable>>,
  context: Option<Box<dyn std::error::Error>>,
}

pub type TGResult<T> = Result<T, TGError>;

impl TGError {
  pub fn new(key: &'static str) -> Self {
    Self {
      key,
      message: None,
      data: None,
      context: None,
    }
  }
  pub fn custom(message: impl AsRef<str>) -> Self {
    let mut error = Self::new("CUSTOM_ERROR");
    error.set_message(message.as_ref());
    error
  }
}

impl TGError {
  pub fn set_key(&mut self, key: &'static str) -> &mut Self {
    self.key = key;
    self
  }

  pub fn set_message(&mut self, message: impl AsRef<str>) -> &mut Self {
    self.message = Some(message.as_ref().to_string());
    self
  }

  pub fn set_data(&mut self, data: Box<dyn TGDatable>) -> &mut Self {
    self.data = Some(data);
    self
  }

  pub fn set_context(&mut self, context: Box<dyn std::error::Error>) -> &mut Self {
    self.context = Some(context);
    self
  }

  pub fn key(&self) -> &'static str {
    self.key
  }
  pub fn message(&self) -> &Option<String> {
    &self.message
  }
  pub fn data(&self) -> &Option<Box<dyn TGDatable>> {
    &self.data
  }
  pub fn context(&self) -> &Option<Box<dyn std::error::Error>> {
    &self.context
  }
}

impl fmt::Display for TGError {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    write!(
      f,
      "[{}]: {}",
      self.key,
      self.message.as_deref().unwrap_or("")
    )
  }
}

impl error::Error for TGError {
  fn source(&self) -> Option<&(dyn error::Error + 'static)> {
    self.context.as_deref()
  }
}

impl From<RTDError> for TGError {
  fn from(err: RTDError) -> Self {
    let mut tgerr = Self::new("RTDLIB_ERROR");
    tgerr.set_message(err.to_string());
    tgerr.set_context(Box::new(err));
    tgerr
  }
}

#[cfg(test)]
mod tests {
  use super::*;

  #[test]
  fn display_includes_key_and_message() {
    let mut err = TGError::new("CUSTOM");
    err.set_message("boom");
    assert_eq!(err.to_string(), "[CUSTOM]: boom");
  }

  #[test]
  fn display_without_message_is_key_only() {
    let err = TGError::new("CUSTOM");
    assert_eq!(err.to_string(), "[CUSTOM]: ");
  }
}