summaryrefslogtreecommitdiffstats
path: root/examples/register/mod.rs
blob: 1c429b5979eb8905aff1274d965726521cefa013 (plain)
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
#![cfg_attr(not(feature = "toml"), allow(dead_code))]
#![cfg_attr(not(feature = "toml"), allow(unused_imports))]

pub use elefren::prelude::*;

use std::{error::Error, io};

use elefren::helpers::cli;
#[cfg(feature = "toml")]
use elefren::helpers::toml;

#[allow(dead_code)]
#[cfg(feature = "toml")]
fn main() -> Result<(), Box<dyn Error>> {
    register()?;
    Ok(())
}

#[allow(dead_code)]
#[cfg(feature = "toml")]
pub fn get_mastodon_data() -> Result<Mastodon, Box<dyn Error>> {
    if let Ok(data) = toml::from_file("mastodon-data.toml") {
        Ok(Mastodon::from(data))
    } else {
        register()
    }
}

#[cfg(feature = "toml")]
pub fn register() -> Result<Mastodon, Box<dyn Error>> {
    let website = read_line("Please enter your mastodon instance url:")?;
    let registration = Registration::new(website.trim())
        .client_name("elefren-examples")
        .scopes(Scopes::all())
        .website("https://github.com/pwoolcoc/elefren")
        .build()?;
    let mastodon = cli::authenticate(registration)?;

    // Save app data for using on the next run.
    toml::to_file(&*mastodon, "mastodon-data.toml")?;

    Ok(mastodon)
}

#[cfg(feature = "toml")]
pub fn read_line(message: &str) -> Result<String, Box<dyn Error>> {
    println!("{}", message);

    let mut input = String::new();
    io::stdin().read_line(&mut input)?;

    Ok(input.trim().to_string())
}

#[cfg(not(feature = "toml"))]
fn main() {}