Use the new toml helpers instead of the toml crate

master
Paul Woolcock 6 years ago
parent 384d3d2c41
commit 6ddea87765
  1. 3
      Cargo.toml
  2. 31
      README.md
  3. 17
      examples/register.rs

@ -24,9 +24,6 @@ toml = { version = "0.4.6", optional = true }
version = "0.4"
features = ["serde"]
[dev-dependencies]
toml = "0.4"
[features]
default-features = []
all = ["toml"]

@ -30,7 +30,6 @@ extern crate elefren;
```rust
extern crate elefren;
extern crate toml;
use std::io;
use std::fs::File;
@ -38,13 +37,11 @@ use std::io::prelude::*;
use elefren::{Data, Mastodon, Registration};
use elefren::apps::{AppBuilder, Scopes};
use elefren::data::toml; // requires `features = ["toml"]`
fn main() {
let mastodon = match File::open("mastodon-data.toml") {
Ok(mut file) => {
let mut config = String::new();
file.read_to_string(&mut config).unwrap();
let data: Data = toml::from_str(&config).unwrap();
let mastodon = match toml::from_file("mastodon-data.toml") {
Ok(data) => {
Mastodon::from(data)
},
Err(_) => register(),
@ -56,16 +53,12 @@ fn main() {
}
fn register() -> Mastodon {
let app = AppBuilder {
client_name: "elefren-examples",
redirect_uris: "urn:ietf:wg:oauth:2.0:oob",
scopes: Scopes::Read,
website: Some("https://github.com/pwoolcoc/elefren"),
};
let mut app = App::builder();
app.client_name("elefren-examples");
let mut registration = Registration::new("https://mastodon.social");
registration.register(app).unwrap();;
let url = registration.authorise().unwrap();
let registration = Registration::new("https://mastodon.social");
.register(app).unwrap();
let url = registration.authorize_url().unwrap();
println!("Click this link to authorize on Mastodon: {}", url);
println!("Paste the returned authorization code: ");
@ -73,13 +66,11 @@ fn register() -> Mastodon {
let mut input = String::new();
io::stdin().read_line(&mut input).unwrap();
let code = input.trim();
let mastodon = registration.create_access_token(code.to_string()).unwrap();
let code = input.trim().to_string();
let mastodon = registration.complete(code).unwrap();
// Save app data for using on the next run.
let toml = toml::to_string(&*mastodon).unwrap();
let mut file = File::create("mastodon-data.toml").unwrap();
file.write_all(toml.as_bytes()).unwrap();
toml::to_file(&*mastodon, "mastodon-data.toml").unwrap();
mastodon
}

@ -1,14 +1,14 @@
extern crate elefren;
extern crate toml;
pub use self::elefren::{Data, MastodonClient};
use std::{error::Error, fs, io};
use std::{error::Error, io};
use self::elefren::{
apps::{App, Scopes},
Mastodon,
Registration,
data::toml,
};
#[allow(dead_code)]
@ -19,8 +19,7 @@ fn main() -> Result<(), Box<Error>> {
#[allow(dead_code)]
pub fn get_mastodon_data() -> Result<Mastodon, Box<Error>> {
if let Ok(config) = fs::read_to_string("mastodon-data.toml") {
let data: Data = toml::from_str(&config)?;
if let Ok(data) = toml::from_file("mastodon-data.toml") {
Ok(Mastodon::from(data))
} else {
register()
@ -39,14 +38,12 @@ pub fn register() -> Result<Mastodon, Box<Error>> {
let url = registered.authorize_url()?;
println!("Click this link to authorize on Mastodon: {}", url);
let input = read_line("Paste the returned authorization code: ")?;
let code = read_line("Paste the returned authorization code: ")?;
let code = input.trim();
let mastodon = registered.complete(code.to_string())?;
let mastodon = registered.complete(code)?;
// Save app data for using on the next run.
let toml = toml::to_string(&*mastodon)?;
fs::write("mastodon-data.toml", toml.as_bytes())?;
toml::to_file(&*mastodon, "mastodon-data.toml")?;
Ok(mastodon)
}
@ -57,5 +54,5 @@ pub fn read_line(message: &str) -> Result<String, Box<Error>> {
let mut input = String::new();
io::stdin().read_line(&mut input)?;
Ok(input)
Ok(input.trim().to_string())
}

Loading…
Cancel
Save