style: fmt & clippy processed

This commit is contained in:
VC
2022-08-11 12:29:40 +02:00
parent dab8725f99
commit b11595bfca
10 changed files with 297 additions and 209 deletions

View File

@@ -1,36 +1,37 @@
use crate::config::MastodonConfig;
use std::{
borrow::Cow,
collections::HashMap,
io::stdin,
};
use html_escape::decode_html_entities;
use egg_mode::{
entities::{MentionEntity, UrlEntity},
tweet::Tweet,
entities::{UrlEntity, MentionEntity},
};
use elefren::{
prelude::*,
apps::App,
scopes::Scopes,
};
use elefren::{apps::App, prelude::*, scopes::Scopes};
use html_escape::decode_html_entities;
use std::{borrow::Cow, collections::HashMap, io::stdin};
/// Decodes the Twitter mention to something that will make sense once Twitter has joined the
/// Fediverse
fn twitter_mentions(ums: &Vec<MentionEntity>) -> HashMap<String, String> {
ums.iter().map(|s|
(format!("@{}", s.screen_name), format!("@{}@twitter.com", s.screen_name))
).collect()
fn twitter_mentions(ums: &[MentionEntity]) -> HashMap<String, String> {
ums.iter()
.map(|s| {
(
format!("@{}", s.screen_name),
format!("@{}@twitter.com", s.screen_name),
)
})
.collect()
}
/// Decodes urls from UrlEntities
fn decode_urls(urls: &Vec<UrlEntity>) -> HashMap<String, String> {
fn decode_urls(urls: &[UrlEntity]) -> HashMap<String, String> {
urls.iter()
.filter(|s| s.expanded_url.is_some())
.map(|s|
(s.url.to_owned(), s.expanded_url.as_deref().unwrap().to_owned())
).collect()
.map(|s| {
(
s.url.to_owned(),
s.expanded_url.as_deref().unwrap().to_owned(),
)
})
.collect()
}
/// Gets Mastodon Data
@@ -66,36 +67,50 @@ pub fn build_basic_status(tweet: &Tweet) -> String {
/// Most of this function is a direct copy/paste of the official `elefren` crate
pub fn register(host: &str) {
let mut builder = App::builder();
builder.client_name(Cow::from(env!("CARGO_PKG_NAME").to_string()))
builder
.client_name(Cow::from(env!("CARGO_PKG_NAME").to_string()))
.redirect_uris(Cow::from("urn:ietf:wg:oauth:2.0:oob".to_string()))
.scopes(Scopes::write_all())
.website(Cow::from("https://framagit.org/veretcle/scootaloo".to_string()));
.website(Cow::from(
"https://framagit.org/veretcle/scootaloo".to_string(),
));
let app = builder.build().expect("Cannot build the app");
let registration = Registration::new(host).register(app).expect("Cannot build registration object");
let url = registration.authorize_url().expect("Cannot generate registration URI!");
let registration = Registration::new(host)
.register(app)
.expect("Cannot build registration object");
let url = registration
.authorize_url()
.expect("Cannot generate registration URI!");
println!("Click this link to authorize on Mastodon: {}", url);
println!("Paste the returned authorization code: ");
let mut input = String::new();
stdin().read_line(&mut input).expect("Unable to read back registration code!");
stdin()
.read_line(&mut input)
.expect("Unable to read back registration code!");
let code = input.trim();
let mastodon = registration.complete(code).expect("Unable to create access token!");
let mastodon = registration
.complete(code)
.expect("Unable to create access token!");
let toml = toml::to_string(&*mastodon).unwrap();
println!("Please insert the following block at the end of your configuration file:\n[mastodon]\n{}", toml);
println!(
"Please insert the following block at the end of your configuration file:\n[mastodon]\n{}",
toml
);
}
#[cfg(test)]
mod tests {
use super::*;
use egg_mode::tweet::TweetEntities;
use chrono::prelude::*;
use egg_mode::tweet::TweetEntities;
#[test]
fn test_twitter_mentions() {
@@ -109,7 +124,10 @@ mod tests {
let twitter_ums = vec![mention_entity];
let mut expected_mentions = HashMap::new();
expected_mentions.insert("@tamerelol".to_string(), "@tamerelol@twitter.com".to_string());
expected_mentions.insert(
"@tamerelol".to_string(),
"@tamerelol@twitter.com".to_string(),
);
let decoded_mentions = twitter_mentions(&twitter_ums);
@@ -135,7 +153,10 @@ mod tests {
let twitter_urls = vec![url_entity1, url_entity2];
let mut expected_urls = HashMap::new();
expected_urls.insert("https://t.me/tamerelol".to_string(), "https://www.nintendojo.fr/dojobar".to_string());
expected_urls.insert(
"https://t.me/tamerelol".to_string(),
"https://www.nintendojo.fr/dojobar".to_string(),
);
let decoded_urls = decode_urls(&twitter_urls);
@@ -200,4 +221,3 @@ mod tests {
assert_eq!(&t_out, "Mother 1 & 2 sur le NES/SNES online !\nDispo maintenant. cc @NintendoFrance@twitter.com https://www.youtube.com/watch?v=w5TrSaoYmZ8");
}
}