mastodon_async/helpers/
cli.rs

1use std::io::{self, BufRead, Write};
2
3use crate::{errors::Result, registration::Registered, Mastodon};
4
5/// Finishes the authentication process for the given `Registered` object,
6/// using the command-line
7pub async fn authenticate(registration: Registered) -> Result<Mastodon> {
8    let url = registration.authorize_url()?;
9
10    let code = {
11        let stdout = io::stdout();
12        let stdin = io::stdin();
13
14        let mut stdout = stdout.lock();
15        let mut stdin = stdin.lock();
16
17        writeln!(&mut stdout, "Click this link to authorize: {url}")?;
18        write!(&mut stdout, "Paste the returned authorization code: ")?;
19        stdout.flush()?;
20
21        let mut input = String::new();
22        stdin.read_line(&mut input)?;
23        input
24    };
25    let code = code.trim();
26
27    registration.complete(code).await
28}
29
30#[cfg(test)]
31mod tests {
32    use super::*;
33
34    #[test]
35    fn send_sync() {
36        fn assert_send_sync(_: impl Send + Sync) {}
37
38        let mock_reg = || -> Registered { unimplemented!() };
39        let no_run = || async move {
40            let _ = authenticate(mock_reg()).await;
41        };
42        assert_send_sync(no_run());
43    }
44}