Run cargo clippy on whole project (#322)

* Run cargo clippy on plume-common

Run clippy on plume-common and adjuste code accordingly

* Run cargo clippy on plume-model

Run clippy on plume-model and adjuste code accordingly

* Reduce need for allocation in plume-common

* Reduce need for allocation in plume-model

add a quick compilation failure if no database backend is enabled

* Run cargo clippy on plume-cli

* Run cargo clippy on plume
This commit is contained in:
fdb-hiroshima
2018-11-26 10:21:52 +01:00
committed by GitHub
parent 8a4702df92
commit 74c398d60c
36 changed files with 577 additions and 810 deletions
+41 -42
View File
@@ -16,17 +16,15 @@ pub fn random_hex() -> String {
}
/// Remove non alphanumeric characters and CamelCase a string
pub fn make_actor_id(name: String) -> String {
name.as_str()
.to_camel_case()
.to_string()
pub fn make_actor_id(name: &str) -> String {
name.to_camel_case()
.chars()
.filter(|c| c.is_alphanumeric())
.collect()
}
pub fn requires_login(message: &str, url: Uri) -> Flash<Redirect> {
Flash::new(Redirect::to(format!("/login?m={}", gettext(message.to_string()))), "callback", url.to_string())
pub fn requires_login<T: Into<Uri<'static>>>(message: &str, url: T) -> Flash<Redirect> {
Flash::new(Redirect::to(format!("/login?m={}", gettext(message.to_string()))), "callback", url.into().to_string())
}
#[derive(Debug)]
@@ -41,27 +39,26 @@ enum State {
pub fn md_to_html(md: &str) -> (String, HashSet<String>, HashSet<String>) {
let parser = Parser::new_ext(md, Options::all());
let (parser, mentions, hashtags): (Vec<Vec<Event>>, Vec<Vec<String>>, Vec<Vec<String>>) = parser.map(|evt| match evt {
let (parser, mentions, hashtags): (Vec<Event>, Vec<String>, Vec<String>) = parser.map(|evt| match evt {
Event::Text(txt) => {
let (evts, _, _, _, new_mentions, new_hashtags) = txt.chars().fold((vec![], State::Ready, String::new(), 0, vec![], vec![]), |(mut events, state, text_acc, n, mut mentions, mut hashtags), c| {
let (evts, _, _, _, new_mentions, new_hashtags) = txt.chars().fold((vec![], State::Ready, String::new(), 0, vec![], vec![]), |(mut events, state, mut text_acc, n, mut mentions, mut hashtags), c| {
match state {
State::Mention => {
let char_matches = c.is_alphanumeric() || c == '@' || c == '.' || c == '-' || c == '_';
if char_matches && (n < (txt.chars().count() - 1)) {
(events, State::Mention, text_acc + c.to_string().as_ref(), n + 1, mentions, hashtags)
text_acc.push(c);
(events, State::Mention, text_acc, n + 1, mentions, hashtags)
} else {
let mention = if char_matches {
text_acc + c.to_string().as_ref()
} else {
text_acc
};
let short_mention = mention.clone();
let short_mention = short_mention.splitn(1, '@').nth(0).unwrap_or("");
let link = Tag::Link(format!("/@/{}/", mention).into(), short_mention.to_string().into());
if char_matches {
text_acc.push(c)
}
let mention = text_acc;
let short_mention = mention.splitn(1, '@').nth(0).unwrap_or("");
let link = Tag::Link(format!("/@/{}/", &mention).into(), short_mention.to_owned().into());
mentions.push(mention);
mentions.push(mention.clone());
events.push(Event::Start(link.clone()));
events.push(Event::Text(format!("@{}", short_mention).into()));
events.push(Event::Text(format!("@{}", &short_mention).into()));
events.push(Event::End(link));
(events, State::Ready, c.to_string(), n + 1, mentions, hashtags)
@@ -70,24 +67,25 @@ pub fn md_to_html(md: &str) -> (String, HashSet<String>, HashSet<String>) {
State::Hashtag => {
let char_matches = c.is_alphanumeric();
if char_matches && (n < (txt.chars().count() -1)) {
(events, State::Hashtag, text_acc + c.to_string().as_ref(), n+1, mentions, hashtags)
text_acc.push(c);
(events, State::Hashtag, text_acc, n+1, mentions, hashtags)
} else {
let hashtag = if char_matches {
text_acc + c.to_string().as_ref()
} else {
text_acc
};
let link = Tag::Link(format!("/tag/{}", hashtag.to_camel_case()).into(), hashtag.to_string().into());
if char_matches {
text_acc.push(c);
}
let hashtag = text_acc;
let link = Tag::Link(format!("/tag/{}", &hashtag.to_camel_case()).into(), hashtag.to_owned().into());
hashtags.push(hashtag.clone());
events.push(Event::Start(link.clone()));
events.push(Event::Text(format!("#{}", hashtag).into()));
events.push(Event::Text(format!("#{}", &hashtag).into()));
events.push(Event::End(link));
(events, State::Ready, c.to_string(), n + 1, mentions, hashtags)
}
}
State::Ready => {
text_acc.push(c);
if c == '@' {
events.push(Event::Text(text_acc.into()));
(events, State::Mention, String::new(), n + 1, mentions, hashtags)
@@ -96,27 +94,28 @@ pub fn md_to_html(md: &str) -> (String, HashSet<String>, HashSet<String>) {
(events, State::Hashtag, String::new(), n + 1, mentions, hashtags)
} else if c.is_alphanumeric() {
if n >= (txt.chars().count() - 1) { // Add the text after at the end, even if it is not followed by a mention.
events.push(Event::Text((text_acc.clone() + c.to_string().as_ref()).into()))
events.push(Event::Text(text_acc.clone().into()))
}
(events, State::Word, text_acc + c.to_string().as_ref(), n + 1, mentions, hashtags)
(events, State::Word, text_acc, n + 1, mentions, hashtags)
} else {
if n >= (txt.chars().count() - 1) { // Add the text after at the end, even if it is not followed by a mention.
events.push(Event::Text((text_acc.clone() + c.to_string().as_ref()).into()))
events.push(Event::Text(text_acc.clone().into()))
}
(events, State::Ready, text_acc + c.to_string().as_ref(), n + 1, mentions, hashtags)
(events, State::Ready, text_acc, n + 1, mentions, hashtags)
}
}
State::Word => {
text_acc.push(c);
if c.is_alphanumeric() {
if n >= (txt.chars().count() - 1) { // Add the text after at the end, even if it is not followed by a mention.
events.push(Event::Text((text_acc.clone() + c.to_string().as_ref()).into()))
events.push(Event::Text(text_acc.clone().into()))
}
(events, State::Word, text_acc + c.to_string().as_ref(), n + 1, mentions, hashtags)
(events, State::Word, text_acc, n + 1, mentions, hashtags)
} else {
if n >= (txt.chars().count() - 1) { // Add the text after at the end, even if it is not followed by a mention.
events.push(Event::Text((text_acc.clone() + c.to_string().as_ref()).into()))
events.push(Event::Text(text_acc.clone().into()))
}
(events, State::Ready, text_acc + c.to_string().as_ref(), n + 1, mentions, hashtags)
(events, State::Ready, text_acc, n + 1, mentions, hashtags)
}
}
}
@@ -124,15 +123,15 @@ pub fn md_to_html(md: &str) -> (String, HashSet<String>, HashSet<String>) {
(evts, new_mentions, new_hashtags)
},
_ => (vec![evt], vec![], vec![])
}).fold((vec![],vec![],vec![]), |(mut parser, mut mention, mut hashtag), (p, m, h)| {
parser.push(p);
mention.push(m);
hashtag.push(h);
}).fold((vec![],vec![],vec![]), |(mut parser, mut mention, mut hashtag), (mut p, mut m, mut h)| {
parser.append(&mut p);
mention.append(&mut m);
hashtag.append(&mut h);
(parser, mention, hashtag)
});
let parser = parser.into_iter().flatten();
let mentions = mentions.into_iter().flatten().map(|m| String::from(m.trim()));
let hashtags = hashtags.into_iter().flatten().map(|h| String::from(h.trim()));
let parser = parser.into_iter();
let mentions = mentions.into_iter().map(|m| String::from(m.trim()));
let hashtags = hashtags.into_iter().map(|h| String::from(h.trim()));
// TODO: fetch mentionned profiles in background, if needed