telegraph_rs/types.rs
1use super::{error::Error, utils::*};
2use reqwest::multipart::Part;
3use serde::{Deserialize, Serialize};
4use std::{collections::HashMap, path::Path};
5
6/// This object represents a Telegraph account.
7#[derive(Debug, Clone, Deserialize)]
8pub struct Account {
9 /// Account name, helps users with several accounts remember which they are currently using.
10 ///
11 /// Displayed to the user above the "Edit/Publish" button on Telegra.ph, other users don't see this name.
12 pub short_name: Option<String>,
13 /// Default author name used when creating new articles.
14 pub author_name: Option<String>,
15 /// Profile link, opened when users click on the author's name below the title.
16 ///
17 /// Can be any link, not necessarily to a Telegram profile or channel.
18 pub author_url: Option<String>,
19 /// Optional. Only returned by the createAccount and revokeAccessToken method.
20 ///
21 /// Access token of the Telegraph account.
22 pub access_token: Option<String>,
23 /// Optional. URL to authorize a browser on telegra.ph and connect it to a Telegraph account.
24 ///
25 /// This URL is valid for only one use and for 5 minutes only.
26 pub auth_url: Option<String>,
27 /// Optional. Number of pages belonging to the Telegraph account.
28 pub page_count: Option<i32>,
29}
30
31/// This object represents a list of Telegraph articles belonging to an account. Most recently created articles first.
32#[derive(Debug, Clone, Deserialize)]
33pub struct PageList {
34 /// Total number of pages belonging to the target Telegraph account.
35 pub total_count: i32,
36 /// Requested pages of the target Telegraph account.
37 pub pages: Vec<Page>,
38}
39
40/// This object represents a page on Telegraph.
41#[derive(Debug, Clone, Deserialize)]
42pub struct Page {
43 /// Path to the page.
44 pub path: String,
45 /// URL of the page.
46 pub url: String,
47 /// Title of the page.
48 pub title: String,
49 /// Description of the page.
50 pub description: String,
51 /// Optional. Name of the author, displayed below the title.
52 pub author_name: Option<String>,
53 /// Optional. Profile link, opened when users click on the author's name below the title.
54 ///
55 /// Can be any link, not necessarily to a Telegram profile or channel.
56 pub author_url: Option<String>,
57 /// Optional. Image URL of the page.
58 pub image_url: Option<String>,
59 /// Optional. Content of the page.
60 pub content: Option<Vec<Node>>,
61 /// Number of page views for the page.
62 pub views: i32,
63 /// Optional. Only returned if access_token passed.
64 ///
65 /// True, if the target Telegraph account can edit the page.
66 pub can_edit: Option<bool>,
67}
68
69/// This object represents the number of page views for a Telegraph article.
70#[derive(Debug, Clone, Deserialize)]
71pub struct PageViews {
72 /// Number of page views for the target page.
73 pub views: i32,
74}
75
76/// This abstract object represents a DOM Node.
77///
78/// It can be a String which represents a DOM text node or a NodeElement object.
79#[derive(Debug, Clone, Deserialize, Serialize)]
80#[serde(untagged)]
81pub enum Node {
82 Text(String),
83 NodeElement(NodeElement),
84}
85
86/// This object represents a DOM element node.
87#[derive(Debug, Clone, Deserialize, Serialize)]
88pub struct NodeElement {
89 /// Name of the DOM element.
90 /// Available tags: a, aside, b, blockquote, br, code, em, figcaption, figure, h3, h4, hr, i, iframe, img, li, ol, p, pre, s, strong, u, ul, video.
91 pub tag: String,
92 /// Optional. Attributes of the DOM element.
93 ///
94 /// Key of object represents name of attribute, value represents value of attribute.
95 ///
96 /// Available attributes: href, src.
97 #[serde(skip_serializing_if = "Option::is_none")]
98 pub attrs: Option<HashMap<String, Option<String>>>,
99 /// Optional. List of child nodes for the DOM element.
100 #[serde(skip_serializing_if = "Option::is_none")]
101 pub children: Option<Vec<Node>>,
102}
103
104/// This object represents the upload result
105#[derive(Debug, Clone, Deserialize)]
106#[serde(untagged)]
107pub enum UploadResult {
108 Error { error: String },
109 Source(Vec<ImageInfo>),
110}
111
112#[derive(Debug, Clone, Deserialize)]
113pub struct ImageInfo {
114 /// Path of the file uploaded.
115 pub src: String,
116}
117
118#[cfg(feature = "upload")]
119pub trait Uploadable {
120 fn part(&self) -> Result<Part, Error>;
121}
122
123#[cfg(feature = "upload")]
124impl<T> Uploadable for T
125where
126 T: AsRef<Path>,
127{
128 fn part(&self) -> Result<Part, Error> {
129 let path = self.as_ref();
130 let bytes = read_to_bytes(path)?;
131 let part = Part::bytes(bytes)
132 .file_name(path.file_name().unwrap().to_string_lossy().to_string())
133 .mime_str(&guess_mime(path))?;
134 Ok(part)
135 }
136}