1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Copyright 2022 Canvas02 <Canvas02@protonmail.com>.
// SPDX-License-Identifier: MIT

const PASTE_RS_URL: &str = "https://paste.rs/";

use core::result::Result as coreResult;

#[derive(Debug)]
pub struct Paste {
    pub id: String,
    pub status_code: Option<reqwest::StatusCode>,
}

// Use get_id() and get_url() methods
/*
impl fmt::Display for Paste {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let Paste(id) = self;
        write!(f, "{}", id)
    }
}
*/

impl Paste {
    /// Make a new paste struct from a string(Url, incomplete url, id)
    ///
    /// # Example
    /// ```
    /// let paste = Paste::from("osx").unwrap();
    /// let paste = Paste::from("https://paste.rs/osx").unwrap();
    /// let paste = Paste::from("paste.rs/osx").unwrap();
    /// ```
    pub fn from(val: &str) -> Result<Self> {
        if is_url(val) && is_paste_rs_url(val) {
            Ok(Paste {
                id: extract_paste_id(&val.to_string())?,
                status_code: None,
            })
        } else if !is_url(val) && is_paste_rs_url(val) {
            let full_url = format!("https://{}", val);
            Ok(Paste {
                id: extract_paste_id(&full_url.to_string())?,
                status_code: None,
            })
        } else if val.len() == 3 {
            Ok(Paste {
                id: val.to_string(),
                status_code: None,
            })
        } else if is_url(val) && !is_paste_rs_url(val) {
            // bail!("Invalid URL")
            Err(Error::InvalidUrl)
        } else {
            // bail!("Invalid argument")
            Err(Error::InvalidArguments)
        }
    }

    /// Make a new paste
    ///
    /// # Example
    /// ```
    ///  let res = Paste::new("Hello world!".to_string())
    ///        .await
    ///        .unwrap()
    ///        .get_url();
    ///
    /// dbg!(res);
    /// ```
    ///
    pub async fn new(data: String) -> Result<Self> {
        let client = reqwest::Client::new();
        let res = client
            .post(PASTE_RS_URL)
            .body(data)
            .header("Content-Type", "text/plain")
            .send()
            .await?;

        match res.error_for_status() {
            Ok(res) => {
                let res_stat = res.status();
                let res_text = res.text().await?;
                // let ref_res_text = &res.text().await?;
                Ok(Paste {
                    status_code: Some(res_stat),
                    id: extract_paste_id(&res_text)?,
                })
            }
            Err(e) => Err(Error::ReqwestError(e)),
        }
    }

    /// Get a paste's content
    ///
    /// # Example
    /// ```
    /// let paste = Paste::from("osx").unwrap();
    ///
    /// let paste_content = paste.get().unwrap();
    ///
    /// dbg!(paste_content);
    /// ```
    ///
    pub async fn get(&self) -> Result<String> {
        let res = reqwest::get(self.get_url()).await?.text().await?;
        Ok(res)
    }

    /* ! Unused code
    /// Get the id of a Paste
    pub fn get_id(&self) -> String {
        let Paste(id) = self;
        id.to_owned()
    }
    */

    /// Get the url of a Paste
    pub fn get_url(&self) -> String {
        format!("{}/{}", PASTE_RS_URL, self.id)
    }
}

// ######################## Util functions ########################

/// Is the string a url?
fn is_url(url: &str) -> bool {
    if url.contains("http://") || url.contains("https://") {
        true
    } else {
        false
    }
}

// is the string a paste.rs url?
fn is_paste_rs_url(url: &str) -> bool {
    if url.contains("paste.rs") {
        true
    } else {
        false
    }
}

fn extract_paste_id(url: &String) -> coreResult<String, Error> {
    // let url = url.to_owned();
    // url.replace_range(0..PASTE_RS_URL.len(), "");
    if url.contains(PASTE_RS_URL) {
        Ok(url.replace(PASTE_RS_URL, ""))
    } else {
        // bail!("Url is not a Paste.rs url")
        Err(Error::InvalidUrl)
    }
}

// Error
// Copyright 2022 Canvas02 <Canvas02@protonmail.com>.
// SPDX-License-Identifier: MIT

pub type Result<T> = coreResult<T, Error>;

#[derive(Debug)]
pub enum Error {
    InvalidUrl,
    InvalidArguments,
    ReqwestError(reqwest::Error),
}

impl std::convert::From<reqwest::Error> for Error {
    fn from(err: reqwest::Error) -> Self {
        Error::ReqwestError(err)
    }
}

impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match &self {
            Error::ReqwestError(err) => Some(err),
            _ => None,
        }
    }
}

impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match &self {
            Error::InvalidArguments => write!(f, "Invalid Arguments"),
            Error::InvalidUrl => write!(f, "Invalid Url"),
            Error::ReqwestError(req_err) => write!(f, "{}", req_err),
        }
    }
}