Skip to content

Commit c81afa0

Browse files
authored
Merge pull request #54 from Kuuuube/fix-edit-remove-snippets
Fix edit remove snippets
2 parents 4118677 + d5d8b14 commit c81afa0

1 file changed

Lines changed: 11 additions & 8 deletions

File tree

‎src/commands/snippets.rs‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,12 @@ pub async fn edit_snippet(
102102
ctx: Context<'_>,
103103
#[autocomplete = "autocomplete_snippet"]
104104
#[description = "The snippet's id"]
105-
id: String,
105+
#[rename = "id"]
106+
id_and_title: String,
106107
#[description = "The snippet's title"] title: Option<String>,
107108
#[description = "The snippet's content"] content: Option<String>,
108109
) -> Result<(), Error> {
109-
match get_snippet_lazy(&ctx, &id) {
110+
match get_snippet(&ctx, &id_and_title) {
110111
Some(mut snippet) => {
111112
if let Some(title) = title {
112113
snippet.title = title;
@@ -118,7 +119,8 @@ pub async fn edit_snippet(
118119

119120
{
120121
let mut rwlock_guard = ctx.data().state.write().unwrap();
121-
rwlock_guard.snippets.push(snippet.clone());
122+
let editing_snippet = rwlock_guard.snippets.iter_mut().find(|s| s.format_output().eq(&id_and_title)).unwrap();
123+
*editing_snippet = snippet.clone();
122124
println!("Snippet edited '{}: {}'", snippet.title, snippet.content);
123125
rwlock_guard.write();
124126
}
@@ -128,7 +130,7 @@ pub async fn edit_snippet(
128130
}
129131
None => {
130132
let title = &"Failed to edit snippet";
131-
let content = &&format!("The snippet '{id}' does not exist");
133+
let content = &&format!("The snippet '{id_and_title}' does not exist");
132134
respond_err(&ctx, title, content).await;
133135
}
134136
};
@@ -142,15 +144,16 @@ pub async fn remove_snippet(
142144
ctx: Context<'_>,
143145
#[autocomplete = "autocomplete_snippet"]
144146
#[description = "The snippet's id"]
145-
id: String,
147+
#[rename = "id"]
148+
id_and_title: String,
146149
) -> Result<(), Error> {
147-
match get_snippet_lazy(&ctx, &id) {
150+
match get_snippet(&ctx, &id_and_title) {
148151
Some(snippet) => {
149152
remove_snippet_confirm(&ctx, &snippet).await?;
150153
}
151154
None => {
152155
let title = &"Failed to remove snippet";
153-
let content = &&format!("The snippet '{id}' does not exist");
156+
let content = &&format!("The snippet '{id_and_title}' does not exist");
154157
respond_err(&ctx, title, content).await;
155158
}
156159
}
@@ -234,7 +237,7 @@ impl Embeddable for Snippet {
234237
}
235238

236239
// Exact matches the snippet id and name.
237-
fn _get_snippet(ctx: &Context<'_>, id: &str) -> Option<Snippet> {
240+
fn get_snippet(ctx: &Context<'_>, id: &str) -> Option<Snippet> {
238241
let data = ctx.data();
239242
let rwlock_guard = data.state.read().unwrap();
240243

0 commit comments

Comments
 (0)