diff --git a/src/commands.rs b/src/commands.rs index 6e09b8f..1cf3ef7 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -138,7 +138,7 @@ pub fn export(file_path: PathBuf, json: bool, path: Option) { } } -pub fn import(file_path: PathBuf, json: bool) { +pub fn import(file_path: PathBuf, json: bool, store_path: Option) { match json { true => { println!("{}", json!({ @@ -164,6 +164,7 @@ pub fn import(file_path: PathBuf, json: bool) { } }; + database::insert_multiple(&bookmarks, json, store_path); info!("succesfully imported bookmarks from {}!", file_path.to_str().unwrap()); }, diff --git a/src/database.rs b/src/database.rs index eb73ba7..ac9eebe 100644 --- a/src/database.rs +++ b/src/database.rs @@ -45,6 +45,40 @@ fn open_database(json: bool, path: Option) -> Option { return Some(db); } +pub fn insert_multiple(entries: &Vec, json: bool, path: Option) { + let db = match open_database(json, path) { + Some(database) => database, + None => std::process::exit(exitcode::NOINPUT), + }; + + let mut batch = sled::Batch::default(); + + for i in entries { + let bytes; + match bincode::serialize(&i) { + Ok(result) => bytes = result, + Err(error) => { + if json { + println!("{}", json!({ + "status": "fail", + "reason": error.to_string(), + })); + } else { + error!("failed serializing entry: {}", error); + } + std::process::exit(exitcode::DATAERR); + }, + } + + batch.insert(i.link.as_str(), bytes); + } + + match db.apply_batch(batch) { + Ok(_) => info!("succesfully applied batch insert"), + Err(e) => warn!("error in applying batch insert: {}", e), + } +} + pub fn insert_entry(entry: &Bookmark, json: bool, path: Option) { let db = match open_database(json, path) { Some(database) => database, diff --git a/src/main.rs b/src/main.rs index b61cba3..dfe0c6e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -56,6 +56,6 @@ fn main() { Commands::Delete { url } => database::remove_entry(url, json, storage_path), Commands::List { } => commands::list(json, storage_path), Commands::Export { file } => commands::export(file.to_path_buf(), json, storage_path), - Commands::Import { file } => commands::import(file.to_path_buf(), json), + Commands::Import { file } => commands::import(file.to_path_buf(), json, storage_path), } }