Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move utility functions to nh_lib, under lib target #170

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ name = "nh"
version = "3.5.27"
edition = "2021"
license = "EUPL-1.2"

[lib]
name = "nh_lib"
path = "src/lib/main.rs"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
Expand Down
5 changes: 3 additions & 2 deletions src/home.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ use crate::*;
use crate::{
interface::NHRunnable,
interface::{FlakeRef, HomeArgs, HomeRebuildArgs, HomeSubcommand},
util::{compare_semver, get_nix_version},
};

use nh_lib::version::{compare_semver, get_nix_version};

#[derive(Error, Debug)]
enum HomeRebuildError {
#[error("Configuration \"{0}\" doesn't exist")]
Expand All @@ -34,7 +35,7 @@ impl NHRunnable for HomeArgs {

impl HomeRebuildArgs {
fn rebuild(&self, action: &HomeSubcommand) -> Result<()> {
let out_path: Box<dyn crate::util::MaybeTempPath> = match self.common.out_link {
let out_path: Box<dyn nh_lib::MaybeTempPath> = match self.common.out_link {
Some(ref p) => Box::new(p.clone()),
None => Box::new({
let dir = tempfile::Builder::new().prefix("nh-home").tempdir()?;
Expand Down
22 changes: 22 additions & 0 deletions src/lib/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
pub mod version;

extern crate semver;

use std::path::{Path, PathBuf};
use tempfile::TempDir;

pub trait MaybeTempPath: std::fmt::Debug {
fn get_path(&self) -> &Path;
}

impl MaybeTempPath for PathBuf {
fn get_path(&self) -> &Path {
self.as_ref()
}
}

impl MaybeTempPath for (PathBuf, TempDir) {
fn get_path(&self) -> &Path {
self.0.as_ref()
}
}
21 changes: 0 additions & 21 deletions src/util.rs → src/lib/version.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
extern crate semver;

use color_eyre::{eyre, Result};
use semver::Version;
use tempfile::TempDir;

use std::path::{Path, PathBuf};
use std::process::Command;
use std::str;

Expand Down Expand Up @@ -58,19 +53,3 @@ pub fn get_nix_version() -> Result<String> {

Err(eyre::eyre!("Failed to extract version"))
}

pub trait MaybeTempPath: std::fmt::Debug {
fn get_path(&self) -> &Path;
}

impl MaybeTempPath for PathBuf {
fn get_path(&self) -> &Path {
self.as_ref()
}
}

impl MaybeTempPath for (PathBuf, TempDir) {
fn get_path(&self) -> &Path {
self.0.as_ref()
}
}
1 change: 0 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ mod json;
mod logging;
mod nixos;
mod search;
mod util;

use crate::interface::NHParser;
use crate::interface::NHRunnable;
Expand Down
4 changes: 2 additions & 2 deletions src/nixos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use tracing::{debug, info, warn};
use crate::interface::NHRunnable;
use crate::interface::OsRebuildType::{self, Boot, Build, Switch, Test};
use crate::interface::{self, OsRebuildArgs};
use crate::util::{compare_semver, get_nix_version};
use crate::*;
use nh_lib::version::{compare_semver, get_nix_version};

const SYSTEM_PROFILE: &str = "/nix/var/nix/profiles/system";
const CURRENT_PROFILE: &str = "/run/current-system";
Expand Down Expand Up @@ -47,7 +47,7 @@ impl OsRebuildArgs {
None => hostname::get().context("Failed to get hostname")?,
};

let out_path: Box<dyn crate::util::MaybeTempPath> = match self.common.out_link {
let out_path: Box<dyn nh_lib::MaybeTempPath> = match self.common.out_link {
Some(ref p) => Box::new(p.clone()),
None => Box::new({
let dir = tempfile::Builder::new().prefix("nh-os").tempdir()?;
Expand Down