nah_chat 0.8.0

Lightweight LLM chat completion API.
Documentation
/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

//! Error and related types.

/**
 * Error kinds that may occur in `nah_chat`.
 */
#[derive(Debug)]
pub enum ErrorKind {
  NetworkError,
  ModelServerError,
}

impl std::fmt::Display for ErrorKind {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    match self {
      ErrorKind::NetworkError => {
        write!(f, "Network error")
      }
      ErrorKind::ModelServerError => {
        write!(f, "Model server error")
      }
    }
  }
}

/**
 * Error type of `nah_chat`.
 */
#[derive(Debug)]
pub struct Error {
  pub kind: ErrorKind,
  pub message: Option<String>,
  pub cause: Option<Box<dyn std::error::Error>>,
}

impl std::error::Error for Error {
  fn cause(&self) -> Option<&dyn std::error::Error> {
    self.cause.as_ref().map(|e| e.as_ref())
  }
}

impl std::fmt::Display for Error {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    match &self.cause {
      None => {
        write!(
          f,
          "{}: {}",
          self.kind,
          self.message.clone().unwrap_or("None".to_string()),
        )
      }
      Some(cause) => {
        write!(
          f,
          "{}: {}\ncaused by {}",
          self.kind,
          self.message.clone().unwrap_or("None".to_string()),
          cause
        )
      }
    }
  }
}

pub type Result<T> = std::result::Result<T, Error>;

impl From<reqwest::Error> for Error {
  fn from(error: reqwest::Error) -> Self {
    Error {
      kind: ErrorKind::NetworkError,
      message: None,
      cause: Some(Box::new(error)),
    }
  }
}