Skip to content

Commit

Permalink
add toml parsing for group files
Browse files Browse the repository at this point in the history
Adds TOML parsing for group files. Some of the
backend trait types for various backends need to
be modified for TOML compatibility.

Signed-off-by: innocentzero <[email protected]>
  • Loading branch information
InnocentZero authored and ripytide committed Aug 31, 2024
1 parent ae13bc6 commit cefbc65
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 21 deletions.
4 changes: 3 additions & 1 deletion src/backend/apt.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
use std::collections::BTreeMap;

use anyhow::Result;
use serde::{Deserialize, Serialize};

use crate::cmd::{command_found, run_args, run_args_for_stdout};
use crate::prelude::*;

#[derive(Debug, Copy, Clone, Default, derive_more::Display)]
pub struct Apt;

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct AptQueryInfo {
pub explicit: bool,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct AptMakeImplicit;

impl Backend for Apt {
Expand Down
5 changes: 3 additions & 2 deletions src/backend/dnf.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
use std::collections::BTreeMap;

use anyhow::Result;
use serde::{Deserialize, Serialize};

use crate::cmd::{command_found, run_args, run_args_for_stdout};
use crate::prelude::*;

#[derive(Debug, Copy, Clone, Default, derive_more::Display)]
pub struct Dnf;

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DnfQueryInfo {
pub user: bool,
}

#[derive(Debug, Clone, Default)]
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct DnfInstallOptions {
repo: Option<String>,
}
Expand Down
5 changes: 4 additions & 1 deletion src/backend/rustup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ use crate::cmd::run_args;
use crate::cmd::run_args_for_stdout;
use crate::prelude::*;
use anyhow::{anyhow, bail, Error, Result};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;

#[derive(Debug, Copy, Clone, Default, derive_more::Display)]
pub struct Rustup;

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, derive_more::Display)]
#[derive(
Debug, Clone, PartialEq, Eq, PartialOrd, Ord, derive_more::Display, Serialize, Deserialize,
)]
pub enum RustupPackageId {
Toolchain(String),
/// Toolchain, Component
Expand Down
6 changes: 2 additions & 4 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,8 @@ impl Config {
///
/// # Errors
/// - If a config file cannot be found.
pub fn load() -> Result<Self> {
let config_file = dirs::config_dir()
.context("getting the config directory")?
.join("pacdef/config.toml");
pub fn load(pacdef_dir: &Path) -> Result<Self> {
let config_file = pacdef_dir.join("config.toml");

let content = match std::fs::read_to_string(config_file) {
Ok(content) => content,
Expand Down
42 changes: 33 additions & 9 deletions src/groups.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
use crate::prelude::*;
use anyhow::Result;
use anyhow::{anyhow, Context, Result};
use path_absolutize::Absolutize;
use walkdir::WalkDir;

use std::{
collections::BTreeMap,
fs::{create_dir, read_to_string},
ops::{Deref, DerefMut},
path::Path,
};

/// A type representing a users group files with all their packages
#[derive(Default)]
#[derive(Debug, Default)]
pub struct Groups(BTreeMap<String, PackagesInstall>);

impl Groups {
Expand All @@ -22,17 +26,37 @@ impl Groups {
packages
}

/// Return a new, empty Group
pub fn new() -> Self {
Self(BTreeMap::new())
}

/// Loads Groups from a users pacdef config folder.
///
/// # Errors
/// - If the Group config file cannot be found.
pub fn load() -> Result<Self> {
todo!()
pub fn load(group_dir: &Path) -> Result<Self> {
let mut groups = Self::default();

let group_dir = group_dir.join("groups/");
if !group_dir.is_dir() {
//other directories were already created with the config file
create_dir(&group_dir).context("group directory doesn't exist, creating one")?;
}

let mut files = vec![];
for file in WalkDir::new(&group_dir).follow_links(true) {
let path = file?.path().absolutize_from(&group_dir)?.to_path_buf();
files.push(path);
}
for group_file in files.iter().filter(|path| path.is_file()) {
let group_name = group_file
.strip_prefix(&group_dir)?
.to_str()
.ok_or(anyhow!("Will not fail on Linux"))?
.to_string();
println!("group_name: {group_name}");
let file_contents = read_to_string(group_file).context("Reading group file")?;

let packages: PackagesInstall = toml::from_str(&file_contents)?;
groups.insert(group_name, packages);
}
Ok(groups)
}
}

Expand Down
8 changes: 5 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ fn main() -> Result<()> {

let main_arguments = MainArguments::parse();

let config = Config::load().context("loading config file")?;

let groups = Groups::load().context("failed to load groups")?;
let pacdef_dir = dirs::config_dir()
.map(|path| path.join("pacdef/"))
.context("getting the pacdef config directory")?;
let config = Config::load(&pacdef_dir).context("loading config file")?;
let groups = Groups::load(&pacdef_dir).context("failed to load groups")?;

if groups.is_empty() {
log::warn!("no group files found");
Expand Down
3 changes: 2 additions & 1 deletion src/packages.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::{
collections::{BTreeMap, BTreeSet},
fmt::Display,
Expand All @@ -20,7 +21,7 @@ macro_rules! generate_packages_ids {

macro_rules! generate_packages_install {
($($name:ident: $backend:ident),*) => {
#[derive(Debug, Clone, Default)]
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct PackagesInstall {
$(
pub $name: BTreeMap<<$backend as Backend>::PackageId, <$backend as Backend>::InstallOptions>,
Expand Down

0 comments on commit cefbc65

Please sign in to comment.