Skip to content

Commit 4a6fa6b

Browse files
authored
Merge pull request #58 from tinaun/playground
Small fixes
2 parents 9b3ee6d + 34e4c7e commit 4a6fa6b

File tree

5 files changed

+25
-14
lines changed

5 files changed

+25
-14
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ lazy_static = "1.4.0"
1818
log = "0.4.0"
1919
env_logger = "0.7.1"
2020
envy = "0.4"
21+
indexmap = "1.3"

src/commands.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::{
22
api,
33
state_machine::{CharacterSet, StateMachine},
44
};
5+
use indexmap::IndexMap;
56
use reqwest::blocking::Client as HttpClient;
67
use serenity::{model::channel::Message, prelude::Context};
78
use std::{collections::HashMap, sync::Arc};
@@ -35,15 +36,15 @@ pub struct Args<'m> {
3536
pub(crate) struct Commands {
3637
state_machine: StateMachine<Arc<Command>>,
3738
client: HttpClient,
38-
menu: Option<HashMap<&'static str, (&'static str, GuardFn)>>,
39+
menu: Option<IndexMap<&'static str, (&'static str, GuardFn)>>,
3940
}
4041

4142
impl Commands {
4243
pub(crate) fn new() -> Self {
4344
Self {
4445
state_machine: StateMachine::new(),
4546
client: HttpClient::new(),
46-
menu: Some(HashMap::new()),
47+
menu: Some(IndexMap::new()),
4748
}
4849
}
4950

@@ -168,7 +169,7 @@ impl Commands {
168169
);
169170
}
170171

171-
pub(crate) fn menu(&mut self) -> Option<HashMap<&'static str, (&'static str, GuardFn)>> {
172+
pub(crate) fn menu(&mut self) -> Option<IndexMap<&'static str, (&'static str, GuardFn)>> {
172173
self.menu.take()
173174
}
174175

src/crates.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,23 +80,31 @@ fn rustc_crate(crate_name: &str) -> Option<&str> {
8080
}
8181

8282
pub fn doc_search(args: Args) -> Result<()> {
83-
let crate_name = args
83+
let query = args
8484
.params
8585
.get("query")
8686
.ok_or("Unable to retrieve param: query")?;
8787

88-
if crate_name.contains("::") {
89-
let message = "`?docs` cannot retrieve documentation for items within a crate.";
90-
api::send_reply(&args, message)?;
91-
} else if let Some(rustc_crate) = rustc_crate(crate_name) {
92-
api::send_reply(&args, rustc_crate)?;
88+
let mut query_iter = query.splitn(2, "::");
89+
let crate_name = query_iter.next().unwrap();
90+
91+
let doc_url = if let Some(rustc_crate) = rustc_crate(crate_name) {
92+
Some(rustc_crate.to_string())
9393
} else if let Some(krate) = get_crate(&args)? {
9494
let name = krate.name;
95-
let message = krate
95+
krate
9696
.documentation
97-
.unwrap_or_else(|| format!("https://docs.rs/{}", name));
97+
.or_else(|| Some(format!("https://docs.rs/{}", name)))
98+
} else {
99+
None
100+
};
101+
102+
if let Some(mut url) = doc_url {
103+
if let Some(item_path) = query_iter.next() {
104+
url += &format!("?search={}", item_path);
105+
}
98106

99-
api::send_reply(&args, &message)?;
107+
api::send_reply(&args, &url)?;
100108
} else {
101109
let message = "No crates found.";
102110
api::send_reply(&args, message)?;

src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ use crate::db::DB;
2323
use commands::{Args, Commands, GuardFn, Result};
2424
use diesel::prelude::*;
2525
use envy;
26+
use indexmap::IndexMap;
2627
use serde::Deserialize;
2728
use serenity::{model::prelude::*, prelude::*};
28-
use std::collections::HashMap;
2929

3030
#[derive(Deserialize)]
3131
struct Config {
@@ -196,7 +196,7 @@ fn app() -> Result<()> {
196196
Ok(())
197197
}
198198

199-
fn main_menu(args: &Args, commands: &HashMap<&str, (&str, GuardFn)>) -> String {
199+
fn main_menu(args: &Args, commands: &IndexMap<&str, (&str, GuardFn)>) -> String {
200200
let mut menu = format!("Commands:\n");
201201

202202
menu = commands

0 commit comments

Comments
 (0)