Skip to content

Commit afa6d48

Browse files
committed
Improve features & add various utility commands
- Adds buttons to manipulate issue embeds - Add issue tokens to allow for linking other repositories - Add edit-embed support.
1 parent d65c15d commit afa6d48

6 files changed

Lines changed: 461 additions & 76 deletions

File tree

src/commands/snippets.rs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ async fn autocomplete_snippet<'a>(
1212
) -> impl Stream<Item = String> + 'a {
1313
let snippet_list: Vec<String> = {
1414
ctx.data()
15-
.snip
16-
.lock()
15+
.state
16+
.read()
1717
.unwrap()
1818
.snippets
1919
.iter()
@@ -65,10 +65,10 @@ pub async fn create_snippet(
6565
) -> Result<(), Error> {
6666
// I really don't like the code I wrote here.
6767
let embed = {
68-
let mut mutex_guard = ctx.data().snip.lock().unwrap();
68+
let mut rwlock_guard = ctx.data().state.write().unwrap();
6969

70-
if let Some(position) = mutex_guard.snippets.iter().position(|s| s.id.eq(&id)) {
71-
mutex_guard.snippets.remove(position);
70+
if let Some(position) = rwlock_guard.snippets.iter().position(|s| s.id.eq(&id)) {
71+
rwlock_guard.snippets.remove(position);
7272
}
7373

7474
let snippet = Snippet {
@@ -77,16 +77,16 @@ pub async fn create_snippet(
7777
content: content.replace(r"\n", "\n"),
7878
};
7979

80-
mutex_guard.snippets.push(snippet.clone());
80+
rwlock_guard.snippets.push(snippet.clone());
8181

82-
mutex_guard.snippets = mutex_guard.snippets.clone();
82+
rwlock_guard.snippets = rwlock_guard.snippets.clone();
8383
println!("New snippet created '{}: {}'", id, title);
84-
mutex_guard.write();
84+
rwlock_guard.write();
8585

8686
let mut embed = snippet.embed();
8787
embed = embed.colour(super::OK_COLOUR);
8888

89-
if mutex_guard.snippets.len() > 25 {
89+
if rwlock_guard.snippets.len() > 25 {
9090
embed = embed.field(
9191
"Warning",
9292
"There are more than 25 snippets, some may not appear in the snippet list.",
@@ -123,10 +123,10 @@ pub async fn edit_snippet(
123123
}
124124

125125
{
126-
let mut mutex_guard = ctx.data().snip.lock().unwrap();
127-
mutex_guard.snippets.push(snippet.clone());
126+
let mut rwlock_guard = ctx.data().state.write().unwrap();
127+
rwlock_guard.snippets.push(snippet.clone());
128128
println!("Snippet edited '{}: {}'", snippet.title, snippet.content);
129-
mutex_guard.write();
129+
rwlock_guard.write();
130130
}
131131

132132
let embed = snippet.embed().colour(super::OK_COLOUR);
@@ -178,7 +178,7 @@ pub async fn delete_snippet(
178178
track_edits
179179
)]
180180
pub async fn list_snippets(ctx: Context<'_>) -> Result<(), Error> {
181-
let snippets = { ctx.data().snip.lock().unwrap().snippets.clone() };
181+
let snippets = { ctx.data().state.read().unwrap().snippets.clone() };
182182

183183
let mut embed = CreateEmbed::default().title("Snippets").color(Colour::TEAL);
184184

@@ -238,9 +238,9 @@ impl Embeddable for Snippet {
238238
// Exact matches the snippet id and name.
239239
async fn get_snippet(ctx: &Context<'_>, id: &str) -> Option<Snippet> {
240240
let data = ctx.data();
241-
let mutex_guard = data.snip.lock().unwrap();
241+
let rwlock_guard = data.state.read().unwrap();
242242

243-
mutex_guard
243+
rwlock_guard
244244
.snippets
245245
.iter()
246246
.find(|s| s.format_output().eq(id))
@@ -250,9 +250,9 @@ async fn get_snippet(ctx: &Context<'_>, id: &str) -> Option<Snippet> {
250250
// Matches the snippet by checking if its starts with the id and name.
251251
async fn get_snippet_lazy(ctx: &Context<'_>, id: &str) -> Option<Snippet> {
252252
let data = ctx.data();
253-
let mutex_guard = data.snip.lock().unwrap();
253+
let rwlock_guard = data.state.read().unwrap();
254254

255-
mutex_guard
255+
rwlock_guard
256256
.snippets
257257
.iter()
258258
.find(|s| s.format_output().starts_with(id))
@@ -261,15 +261,15 @@ async fn get_snippet_lazy(ctx: &Context<'_>, id: &str) -> Option<Snippet> {
261261

262262
async fn rm_snippet(ctx: &Context<'_>, snippet: &Snippet) {
263263
let data = ctx.data();
264-
let mut mutex_guard = data.snip.lock().unwrap();
264+
let mut rwlock_guard = data.state.write().unwrap();
265265

266-
let index = mutex_guard
266+
let index = rwlock_guard
267267
.snippets
268268
.iter()
269269
.position(|s| s.id == snippet.id)
270270
.expect("Snippet was not found in vec");
271271

272272
println!("Removing snippet '{}: {}'", snippet.id, snippet.title);
273-
mutex_guard.snippets.remove(index);
274-
mutex_guard.write();
273+
rwlock_guard.snippets.remove(index);
274+
rwlock_guard.write();
275275
}

0 commit comments

Comments
 (0)