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" version = "0.4"
features = ["serde"] features = ["serde"]
[dev-dependencies]
toml = "0.4"
[features] [features]
default-features = [] default-features = []
all = ["toml"] all = ["toml"]

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

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

Loading…
Cancel
Save