Skip to content

Commit

Permalink
FIXME
Browse files Browse the repository at this point in the history
  • Loading branch information
viperML committed Sep 7, 2024
1 parent 5dd64eb commit 62faee3
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 333 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "nh"
version = "3.5.25"
version = "4.0.0-alpha.1"
edition = "2021"
license = "EUPL-1.2"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
206 changes: 0 additions & 206 deletions src/home.rs

This file was deleted.

95 changes: 95 additions & 0 deletions src/installables.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
use core::fmt;
use std::fmt::Write;

#[derive(Debug, Clone)]
pub enum Installable {
Flake(FlakeInstallable),
}

#[derive(Debug, Clone)]
pub struct FlakeInstallable {
pub reference: String,
pub attribute: Vec<String>,
}

impl From<&str> for Installable {
fn from(value: &str) -> Self {
todo!()
}
}

impl Installable {
pub fn flake<S>(reference: S, attribute: &[S]) -> Self
where
S: AsRef<str>,
{
Installable::Flake(FlakeInstallable {
reference: reference.as_ref().to_string(),
attribute: attribute.iter().map(|s| s.as_ref().to_string()).collect(),
})
}

pub fn to_args(&self) -> Vec<String> {
let mut res = Vec::new();

match &self {
Installable::Flake(flake) => {
let mut f = String::new();
write!(f, "{}", flake.reference).unwrap();

if !flake.attribute.is_empty() {
write!(f, "#").unwrap();

let mut first = true;

for elem in &flake.attribute {
if !first {
write!(f, ".").unwrap();
}

if elem.contains('.') {
write!(f, r#""{}""#, elem).unwrap();
} else {
write!(f, "{}", elem).unwrap();
}

first = false;
}

res.push(f);
}
}
}

return res;
}
}

impl fmt::Display for Installable {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut first = true;

for elem in self.to_args() {
if !first {
write!(f, " ")?;
} else {
first = false;
}

write!(f, "{}", elem)?;
}

Ok(())
}
}

#[test]
fn test_display() {
let installable = Installable::flake(".", &["foo", "bar.local", "baz"]);

let args = installable.to_args();
assert_eq!(args, vec![String::from(".#foo.\"bar.local\".baz")]);

let displayed = format!("{}", installable);
assert_eq!(".#foo.\"bar.local\".baz", displayed);
}
5 changes: 3 additions & 2 deletions src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use clap::{builder::Styles, Args, Parser, Subcommand};
use color_eyre::Result;
use std::{ffi::OsString, ops::Deref, path::PathBuf};

use crate::installables::Installable;

#[derive(Debug, Clone, Default)]
pub struct FlakeRef(String);
impl From<&str> for FlakeRef {
Expand Down Expand Up @@ -61,7 +63,6 @@ pub trait NHRunnable {
#[command(disable_help_subcommand = true)]
pub enum NHCommand {
Os(OsArgs),
Home(HomeArgs),
Search(SearchArgs),
Clean(CleanProxy),
Completions(CompletionArgs),
Expand Down Expand Up @@ -130,7 +131,7 @@ pub struct CommonRebuildArgs {

/// Flake reference to build
#[arg(env = "FLAKE", value_hint = clap::ValueHint::DirPath)]
pub flakeref: FlakeRef,
pub installable: Installable,

/// Update flake inputs before building specified configuration
#[arg(long, short = 'u')]
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod clean;
mod commands;
mod completion;
mod home;
mod installables;
mod interface;
mod json;
mod logging;
Expand Down
Loading

0 comments on commit 62faee3

Please sign in to comment.