mirror of
https://github.com/teloxide/teloxide.git
synced 2024-12-23 06:51:01 +01:00
Replace impl Error + derive Display with thiserror
This commit is contained in:
parent
e436490fdd
commit
9b0a059c8b
5 changed files with 96 additions and 77 deletions
|
@ -15,6 +15,7 @@ tokio = "0.2.0-alpha.6"
|
|||
bytes = "0.4.12"
|
||||
futures-preview = "0.3.0-alpha.19"
|
||||
async-trait = "0.1.13"
|
||||
thiserror = "1.0.2"
|
||||
|
||||
[features]
|
||||
default = []
|
||||
|
|
|
@ -1,29 +1,21 @@
|
|||
use reqwest::StatusCode;
|
||||
|
||||
//<editor-fold desc="download">
|
||||
#[derive(Debug, Display, From)]
|
||||
#[derive(Debug, Error, From)]
|
||||
pub enum DownloadError {
|
||||
#[display(fmt = "Network error: {err}", err = _0)]
|
||||
NetworkError(reqwest::Error),
|
||||
#[error("A network error: {0}")]
|
||||
NetworkError(#[source] reqwest::Error),
|
||||
|
||||
#[display(fmt = "IO Error: {err}", err = _0)]
|
||||
Io(std::io::Error),
|
||||
#[error("An I/O error: {0}")]
|
||||
Io(#[source] std::io::Error),
|
||||
}
|
||||
|
||||
impl std::error::Error for DownloadError {
|
||||
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
||||
match self {
|
||||
DownloadError::NetworkError(err) => Some(err),
|
||||
DownloadError::Io(err) => Some(err),
|
||||
}
|
||||
}
|
||||
}
|
||||
//</editor-fold>
|
||||
|
||||
//<editor-fold desc="request">
|
||||
#[derive(Debug, Display)]
|
||||
#[derive(Debug, Error)]
|
||||
pub enum RequestError {
|
||||
#[display(fmt = "Telegram error #{}: {}", status_code, description)]
|
||||
#[error("A Telegram's error #{status_code}: {description}")]
|
||||
ApiError {
|
||||
status_code: StatusCode,
|
||||
description: String,
|
||||
|
@ -31,30 +23,19 @@ pub enum RequestError {
|
|||
|
||||
/// The group has been migrated to a supergroup with the specified
|
||||
/// identifier.
|
||||
#[display(fmt = "The group has been migrated to a supergroup with id {id}", id = _0)]
|
||||
#[error("The group has been migrated to a supergroup with ID #{0}")]
|
||||
MigrateToChatId(i64),
|
||||
|
||||
/// In case of exceeding flood control, the number of seconds left to wait
|
||||
/// before the request can be repeated
|
||||
#[display(fmt = "Retry after {secs} seconds", secs = _0)]
|
||||
#[error("Retry after {0} seconds")]
|
||||
RetryAfter(i32),
|
||||
|
||||
#[display(fmt = "Network error: {err}", err = _0)]
|
||||
NetworkError(reqwest::Error),
|
||||
#[error("A network error: {0}")]
|
||||
NetworkError(#[source] reqwest::Error),
|
||||
|
||||
#[display(fmt = "InvalidJson error caused by: {err}", err = _0)]
|
||||
InvalidJson(serde_json::Error),
|
||||
#[error("An error while parsing JSON: {0}")]
|
||||
InvalidJson(#[source] serde_json::Error),
|
||||
}
|
||||
|
||||
impl std::error::Error for RequestError {
|
||||
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
||||
match self {
|
||||
RequestError::ApiError { .. } => None,
|
||||
RequestError::MigrateToChatId(_) => None,
|
||||
RequestError::RetryAfter(_) => None,
|
||||
RequestError::NetworkError(err) => Some(err),
|
||||
RequestError::InvalidJson(err) => Some(err),
|
||||
}
|
||||
}
|
||||
}
|
||||
//</editor-fold>
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
#[macro_use]
|
||||
extern crate derive_more;
|
||||
extern crate thiserror;
|
||||
#[macro_use]
|
||||
extern crate serde;
|
||||
#[macro_use]
|
||||
extern crate derive_more;
|
||||
|
||||
pub use errors::{DownloadError, RequestError};
|
||||
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
use futures::SinkExt;
|
||||
|
||||
use async_trait::async_trait;
|
||||
|
||||
use crate::network;
|
||||
|
@ -7,14 +5,21 @@ use crate::requests::{Request, RequestContext, ResponseResult};
|
|||
use crate::types::{ChatId, Message, ParseMode, ReplyMarkup};
|
||||
|
||||
///TODO: add to bot api
|
||||
///Use this method to send animation files (GIF or H.264/MPEG-4 AVC video without sound). On success, the sent Message is returned. Bots can currently send animation files of up to 50 MB in size, this limit may be changed in the future.
|
||||
///Use this method to send animation files (GIF or H.264/MPEG-4 AVC video
|
||||
/// without sound). On success, the sent Message is returned. Bots can currently
|
||||
/// send animation files of up to 50 MB in size, this limit may be changed in
|
||||
/// the future.
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct SendAnimation<'a> {
|
||||
#[serde(skip_serializing)]
|
||||
ctx: RequestContext<'a>,
|
||||
///Unique identifier for the target chat or username of the target channel (in the format @channelusername)
|
||||
///Unique identifier for the target chat or username of the target channel
|
||||
/// (in the format @channelusername)
|
||||
pub chat_id: ChatId,
|
||||
///Animation to send. Pass a file_id as String to send an animation that exists on the Telegram servers (recommended), pass an HTTP URL as a String for Telegram to get an animation from the Internet, or upload a new animation using multipart/form-data. More info on Sending Files »
|
||||
///Animation to send. Pass a file_id as String to send an animation that
|
||||
/// exists on the Telegram servers (recommended), pass an HTTP URL as a
|
||||
/// String for Telegram to get an animation from the Internet, or upload a
|
||||
/// new animation using multipart/form-data. More info on Sending Files »
|
||||
pub animation: String,
|
||||
// InputFile or String
|
||||
///Duration of sent animation in seconds
|
||||
|
@ -26,23 +31,35 @@ pub struct SendAnimation<'a> {
|
|||
///Animation height
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub height: Option<i32>,
|
||||
///Thumbnail of the file sent; can be ignored if thumbnail generation for the file is supported server-side. The thumbnail should be in JPEG format and less than 200 kB in size. A thumbnail‘s width and height should not exceed 320. Ignored if the file is not uploaded using multipart/form-data. Thumbnails can’t be reused and can be only uploaded as a new file, so you can pass “attach://<file_attach_name>” if the thumbnail was uploaded using multipart/form-data under <file_attach_name> »
|
||||
///Thumbnail of the file sent; can be ignored if thumbnail generation for
|
||||
/// the file is supported server-side. The thumbnail should be in JPEG
|
||||
/// format and less than 200 kB in size. A thumbnail‘s width and height
|
||||
/// should not exceed 320. Ignored if the file is not uploaded using
|
||||
/// multipart/form-data. Thumbnails can’t be reused and can be only
|
||||
/// uploaded as a new file, so you can pass “attach://<file_attach_name>”
|
||||
/// if the thumbnail was uploaded using multipart/form-data under
|
||||
/// <file_attach_name> »
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub thumb: Option<String>,
|
||||
// InputFile or String Optional
|
||||
///Animation caption (may also be used when resending animation by file_id), 0-1024 characters
|
||||
///Animation caption (may also be used when resending animation by
|
||||
/// file_id), 0-1024 characters
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub caption: Option<String>,
|
||||
/// Send Markdown or HTML, if you want Telegram apps to show bold, italic, fixed-width text or inline URLs in the media caption.
|
||||
/// Send Markdown or HTML, if you want Telegram apps to show bold, italic,
|
||||
/// fixed-width text or inline URLs in the media caption.
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub parse_mode: Option<ParseMode>,
|
||||
///Sends the message silently. Users will receive a notification with no sound.
|
||||
///Sends the message silently. Users will receive a notification with no
|
||||
/// sound.
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub disable_notification: Option<bool>,
|
||||
///If the message is a reply, ID of the original message
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub reply_to_message_id: Option<i32>,
|
||||
///Additional interface options. A JSON-serialized object for an inline keyboard, custom reply keyboard, instructions to remove reply keyboard or to force a reply from the user.
|
||||
///Additional interface options. A JSON-serialized object for an inline
|
||||
/// keyboard, custom reply keyboard, instructions to remove reply keyboard
|
||||
/// or to force a reply from the user.
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub reply_markup: Option<ReplyMarkup>,
|
||||
}
|
||||
|
|
|
@ -4,14 +4,21 @@ use crate::network;
|
|||
use crate::requests::{Request, RequestContext, ResponseResult};
|
||||
use crate::types::{ChatId, Message, ParseMode, ReplyMarkup};
|
||||
//TODO: add action to bot api
|
||||
///Use this method to send video files, Telegram clients support mp4 videos (other formats may be sent as Document). On success, the sent Message is returned. Bots can currently send video files of up to 50 MB in size, this limit may be changed in the future.
|
||||
///Use this method to send video files, Telegram clients support mp4 videos
|
||||
/// (other formats may be sent as Document). On success, the sent Message is
|
||||
/// returned. Bots can currently send video files of up to 50 MB in size, this
|
||||
/// limit may be changed in the future.
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct SendVideo<'a> {
|
||||
#[serde(skip_serializing)]
|
||||
ctx: RequestContext<'a>,
|
||||
///Unique identifier for the target chat or username of the target channel (in the format @channelusername)
|
||||
///Unique identifier for the target chat or username of the target channel
|
||||
/// (in the format @channelusername)
|
||||
pub chat_id: ChatId,
|
||||
///Video to send. Pass a file_id as String to send a video that exists on the Telegram servers (recommended), pass an HTTP URL as a String for Telegram to get a video from the Internet, or upload a new video using multipart/form-data. More info on Sending Files »
|
||||
///Video to send. Pass a file_id as String to send a video that exists on
|
||||
/// the Telegram servers (recommended), pass an HTTP URL as a String for
|
||||
/// Telegram to get a video from the Internet, or upload a new video using
|
||||
/// multipart/form-data. More info on Sending Files »
|
||||
pub video: String,
|
||||
///Duration of sent video in seconds
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
|
@ -22,31 +29,42 @@ pub struct SendVideo<'a> {
|
|||
///Video height
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub height: Option<i32>,
|
||||
///Thumbnail of the file sent; can be ignored if thumbnail generation for the file is supported server-side. The thumbnail should be in JPEG format and less than 200 kB in size. A thumbnail‘s width and height should not exceed 320. Ignored if the file is not uploaded using multipart/form-data. Thumbnails can’t be reused and can be only uploaded as a new file, so you can pass “attach://<file_attach_name>” if the thumbnail was uploaded using multipart/form-data under <file_attach_name>. More info on Sending Files »
|
||||
///Thumbnail of the file sent; can be ignored if thumbnail generation for
|
||||
/// the file is supported server-side. The thumbnail should be in JPEG
|
||||
/// format and less than 200 kB in size. A thumbnail‘s width and height
|
||||
/// should not exceed 320. Ignored if the file is not uploaded using
|
||||
/// multipart/form-data. Thumbnails can’t be reused and can be only
|
||||
/// uploaded as a new file, so you can pass “attach://<file_attach_name>”
|
||||
/// if the thumbnail was uploaded using multipart/form-data under
|
||||
/// <file_attach_name>. More info on Sending Files »
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub thumb: Option<String>,
|
||||
//InputFile or String
|
||||
///Video caption (may also be used when resending videos by file_id), 0-1024 characters
|
||||
///Video caption (may also be used when resending videos by file_id),
|
||||
/// 0-1024 characters
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub caption: Option<String>,
|
||||
///Send Markdown or HTML, if you want Telegram apps to show bold, italic, fixed-width text or inline URLs in the media caption.
|
||||
///Send Markdown or HTML, if you want Telegram apps to show bold, italic,
|
||||
/// fixed-width text or inline URLs in the media caption.
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub parse_mode: Option<ParseMode>,
|
||||
///Pass True, if the uploaded video is suitable for streaming
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub supports_streaming: Option<bool>,
|
||||
/// Sends the message silently. Users will receive a notification with no sound.
|
||||
/// Sends the message silently. Users will receive a notification with no
|
||||
/// sound.
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub disable_notification: Option<bool>,
|
||||
///If the message is a reply, ID of the original message
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub reply_to_message_id: Option<i32>,
|
||||
///Additional interface options. A JSON-serialized object for an inline keyboard, custom reply keyboard, instructions to remove reply keyboard or to force a reply from the user.
|
||||
///Additional interface options. A JSON-serialized object for an inline
|
||||
/// keyboard, custom reply keyboard, instructions to remove reply keyboard
|
||||
/// or to force a reply from the user.
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub reply_markup: Option<ReplyMarkup>,
|
||||
}
|
||||
|
||||
|
||||
#[async_trait]
|
||||
impl<'a> Request for SendVideo<'a> {
|
||||
type ReturnValue = Message;
|
||||
|
@ -100,7 +118,7 @@ impl<'a> SendVideo<'a> {
|
|||
|
||||
pub fn video<T>(mut self, video: T) -> Self
|
||||
where
|
||||
T: Into<String>
|
||||
T: Into<String>,
|
||||
{
|
||||
self.video = video.into();
|
||||
self
|
||||
|
@ -108,70 +126,70 @@ impl<'a> SendVideo<'a> {
|
|||
|
||||
pub fn duration<T>(mut self, duration: T) -> Self
|
||||
where
|
||||
T: Into<u64>
|
||||
T: Into<u64>,
|
||||
{
|
||||
self.duration = Some(duration.into());
|
||||
self
|
||||
}
|
||||
pub fn width<T>(mut self, width: T) -> Self
|
||||
where
|
||||
T: Into<i32>
|
||||
T: Into<i32>,
|
||||
{
|
||||
self.width = Some(width.into());
|
||||
self
|
||||
}
|
||||
pub fn height<T>(mut self, height: T) -> Self
|
||||
where
|
||||
T: Into<i32>
|
||||
T: Into<i32>,
|
||||
{
|
||||
self.height = Some(height.into());
|
||||
self
|
||||
}
|
||||
pub fn thumb<T>(mut self, thumb: T) -> Self
|
||||
where
|
||||
T: Into<String>
|
||||
T: Into<String>,
|
||||
{
|
||||
self.thumb = Some(thumb.into());
|
||||
self
|
||||
}
|
||||
pub fn caption<T>(mut self, caption: T) -> Self
|
||||
where
|
||||
T: Into<String>
|
||||
T: Into<String>,
|
||||
{
|
||||
self.caption = Some(caption.into());
|
||||
self
|
||||
}
|
||||
pub fn parse_mode<T>(mut self, parse_mode: T) -> Self
|
||||
where
|
||||
T: Into<ParseMode>
|
||||
T: Into<ParseMode>,
|
||||
{
|
||||
self.parse_mode = Some(parse_mode.into());
|
||||
self
|
||||
}
|
||||
pub fn supports_streaming<T>(mut self, supports_streaming: T) -> Self
|
||||
where
|
||||
T: Into<bool>
|
||||
T: Into<bool>,
|
||||
{
|
||||
self.supports_streaming = Some(supports_streaming.into());
|
||||
self
|
||||
}
|
||||
pub fn disable_notification<T>(mut self, disable_notification: T) -> Self
|
||||
where
|
||||
T: Into<bool>
|
||||
T: Into<bool>,
|
||||
{
|
||||
self.disable_notification = Some(disable_notification.into());
|
||||
self
|
||||
}
|
||||
pub fn reply_to_message_id<T>(mut self, reply_to_message_id: T) -> Self
|
||||
where
|
||||
T: Into<i32>
|
||||
T: Into<i32>,
|
||||
{
|
||||
self.reply_to_message_id = Some(reply_to_message_id.into());
|
||||
self
|
||||
}
|
||||
pub fn reply_markup<T>(mut self, reply_markup: T) -> Self
|
||||
where
|
||||
T: Into<ReplyMarkup>
|
||||
T: Into<ReplyMarkup>,
|
||||
{
|
||||
self.reply_markup = Some(reply_markup.into());
|
||||
self
|
||||
|
|
Loading…
Reference in a new issue