Skip to content

Commit

Permalink
add rm_repo.rs (#16)
Browse files Browse the repository at this point in the history
* added basic remove repo function

* cleaner error reporting

* semver submodule to dependency

* merge bug

* refactroed to use macro

---------

Co-authored-by: Ishaan Subramanya <whthownothing@gmail.com>
  • Loading branch information
0x177 and FerrisWasTaken authored May 9, 2024
1 parent 137dd60 commit e59df2f
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Cargo.lock
!.vscode/*.code-snippets
.history/
*.vsix
.vscode/
/.vscode/

# Packaging
/packaging/**/pkg
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,4 @@ console = "0.15"

# GUI
relm4 = "0.8"
relm4-components = "0.8"
relm4-components = "0.8"
7 changes: 7 additions & 0 deletions paxy/src/actions/repository/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ pub enum Error {
#[non_exhaustive]
#[snafu(display("Could not downgrade:\n {source}"))]
CouldNotDowngrade { source: downgrade::Error },

#[non_exhaustive]
#[snafu(display("Could not remove:\n {source}"))]
CouldNotRemove { source: rm_repo::Error }
}

// region: IMPORTS
Expand All @@ -42,6 +46,7 @@ pub mod list;
pub mod search;
pub mod uninstall;
pub mod update;
pub mod rm_repo;

// endregion: MODULES

Expand All @@ -60,5 +65,7 @@ pub use search::*;
pub use uninstall::*;
#[allow(unused_imports)]
pub use update::*;
#[allow(unused_imports)]
pub use rm_repo::*;

// endregion: RE-EXPORTS
71 changes: 71 additions & 0 deletions paxy/src/actions/repository/rm_repo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//region: IMPORTS
use bson::Document;
use crate::data::config::{Config,load_conf};
use std::{
path::PathBuf,
io::Write,
};
use snafu::{Snafu,ensure,OptionExt,ResultExt};
//endregion: IMPORTS

#[derive(Debug, Snafu)]
pub enum Error {
#[snafu(display("failed to serialize repo data into byte stream {source}"))]
FailedToSerialize {source: bson::ser::Error},

#[snafu(display("failed to write to repo data file {source}"))]
FailedToWriteData {source: std::io::Error},

#[snafu(display("repo not found"))]
FailedToFindRepo {},
}

#[allow(dead_code)]
fn delete_repo(repo_name: &str) -> Result<(),Error> {
let mut config = load_conf();
let mut readable_data = config.repositories;

readable_data.get(repo_name).context(FailedToFindRepoSnafu{})?;
readable_data.remove(repo_name);
let mut buf = vec![];
let rbd_result = readable_data.to_writer(&mut buf);

rbd_result.context(FailedToSerializeSnafu{})?;

let mut repos_file_path: PathBuf = home!();
repos_file_path.push(".paxy");
repos_file_path.push("repos.bson");
let mut file = std::fs::OpenOptions::new().write(true).truncate(true).open(repos_file_path).unwrap();
let ftw_result = file.write_all(&buf);

ftw_result.context(FailedToWriteDataSnafu{})?;

config.repositories = readable_data;

let mut config_toml_path: PathBuf = home!();

config_toml_path.push(".paxy");
config_toml_path.push("config.toml");

let mut config_toml_file = std::fs::OpenOptions::new().write(true).truncate(true).open(config_toml_path).unwrap();
config_toml_file.write_all(
toml::to_string(&config)
.unwrap()
.as_bytes(),
)
.expect("Permission error");

Ok(())
}

//region: TESTS
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn delete_repo_test() {
Config::default();
assert_eq!(delete_repo("paxy-pkgs").is_ok(),true);
}
}
//endregion: TESTS
2 changes: 1 addition & 1 deletion paxy/src/data/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ pub enum InstallType {
}

#[allow(dead_code)]
fn load_conf() -> Config {
pub fn load_conf() -> Config {
let mut conf_path: PathBuf = home!();
conf_path.push(".paxy");
conf_path.push("config.toml");
Expand Down
2 changes: 1 addition & 1 deletion paxy/src/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ use snafu::Snafu;
// pub use some_module::*;

// endregion: RE-EXPORTS
mod config;
pub mod config;

0 comments on commit e59df2f

Please sign in to comment.