Skip to content

Commit c606bba

Browse files
committed
Update
1 parent a8c5e1c commit c606bba

File tree

4 files changed

+43
-3
lines changed

4 files changed

+43
-3
lines changed

src/bookmark_query.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
11
use serde::{Deserialize, Serialize};
22
use structopt::StructOpt;
3+
34
#[derive(Serialize, Deserialize, Debug, Clone, StructOpt)]
45
pub struct BookmarkQuery {
56
#[structopt(long, short)]
67
pub id: Option<u32>,
78
#[structopt(long, short)]
89
pub name: Option<String>,
910
}
11+
12+
#[derive(Serialize, Deserialize, Debug, Clone, StructOpt)]
13+
pub struct BookmarkUpdateQuery {
14+
#[structopt(long, short)]
15+
pub id: u32,
16+
#[structopt(long, short)]
17+
pub name: Option<String>,
18+
#[structopt(long, short)]
19+
pub link: Option<String>,
20+
}

src/commands.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
use crate::{bookmark::Bookmark, bookmark_query::BookmarkQuery, data::Data, error::Result};
1+
use crate::{
2+
bookmark::Bookmark,
3+
bookmark_query::{BookmarkQuery, BookmarkUpdateQuery},
4+
data::Data,
5+
error::Result,
6+
};
27

38
/// Insert commands
49
/// Adds the bookmark to the data list and increments the last id
@@ -53,3 +58,22 @@ pub fn remove(data: &mut Data, query: BookmarkQuery) -> Result<Vec<Bookmark>> {
5358
data.write_to_file()?;
5459
Ok(removed)
5560
}
61+
62+
pub fn update(data: &mut Data, query: BookmarkUpdateQuery) -> Result<()> {
63+
data.bookmarks
64+
.iter_mut()
65+
.map(|e| {
66+
if e.id != query.id {
67+
return;
68+
}
69+
70+
let name = query.name.as_ref().unwrap_or(&e.name);
71+
let link = query.link.as_ref().unwrap_or(&e.link);
72+
73+
e.name = name.to_string();
74+
e.link = link.to_string();
75+
})
76+
.for_each(drop);
77+
78+
data.write_to_file()
79+
}

src/config.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use structopt::StructOpt;
22

3-
use crate::bookmark_query::BookmarkQuery;
3+
use crate::bookmark_query::{BookmarkQuery, BookmarkUpdateQuery};
44

55
#[derive(StructOpt, Debug)]
66
pub struct Config {
@@ -30,4 +30,8 @@ pub enum SubOpt {
3030
#[structopt(flatten)]
3131
query: BookmarkQuery,
3232
},
33+
Update {
34+
#[structopt(flatten)]
35+
query: BookmarkUpdateQuery,
36+
},
3337
}

src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub mod data;
66
pub mod error;
77
pub mod parser;
88

9-
use commands::{insert, list, remove};
9+
use commands::{insert, list, remove, update};
1010
use config::{Config, SubOpt};
1111
use data::read_data_file;
1212

@@ -37,6 +37,7 @@ pub fn run() -> Result<()> {
3737
i.colored_fmt()
3838
}
3939
}
40+
SubOpt::Update { query } => update(&mut data, query)?,
4041
}
4142
Ok(())
4243
}

0 commit comments

Comments
 (0)