Skip to content

Commit

Permalink
refactor: move utilities to utils module
Browse files Browse the repository at this point in the history
  • Loading branch information
koehlma committed Jan 28, 2024
1 parent 3f4b8f9 commit fc37611
Show file tree
Hide file tree
Showing 16 changed files with 145 additions and 29 deletions.
81 changes: 81 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions crates/rugpi-bakery/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@ sha1 = "0.10.5"
tempfile = "3.8.1"
thiserror = "1.0.43"
toml = "0.8.8"
tracing = "0.1.40"
tracing-subscriber = "0.3.18"
url = { version = "2.4.0", features = ["serde"] }
xscript.workspace = true
4 changes: 2 additions & 2 deletions crates/rugpi-bakery/src/bake/customize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use tempfile::tempdir;
use xscript::{cmd, run, vars, ParentEnv, Run};

use crate::{
caching::{mtime, mtime_recursive},
project::{
config::Architecture,
layers::{Layer, LayerConfig},
Expand All @@ -24,6 +23,7 @@ use crate::{
repositories::RepositoryIdx,
Project,
},
utils::caching::{mtime, mtime_recursive},
};

/// The arguments of the `customize` command.
Expand All @@ -46,7 +46,7 @@ pub fn customize(
let library = project.library()?;
// Collect the recipes to apply.
let config = layer.config(arch).unwrap();
let jobs = recipe_schedule(layer.repo, config, &library)?;
let jobs = recipe_schedule(layer.repo, config, library)?;
let mut last_modified = jobs
.iter()
.map(|job| job.recipe.modified)
Expand Down
2 changes: 1 addition & 1 deletion crates/rugpi-bakery/src/bake/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use tempfile::tempdir;
use xscript::{run, Run};

use crate::{
caching::{download, sha1},
project::{config::Architecture, Project},
utils::caching::{download, sha1},
};

pub mod customize;
Expand Down
7 changes: 5 additions & 2 deletions crates/rugpi-bakery/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ use clap::Parser;
use colored::Colorize;
use project::{config::Architecture, repositories::Source, ProjectLoader};
use rugpi_common::Anyhow;
use utils::logging::init_logging;

pub mod bake;
pub mod caching;
pub mod idx_vec;
pub mod project;
pub mod utils;

#[derive(Debug, Parser)]
pub struct Args {
Expand Down Expand Up @@ -59,6 +59,9 @@ pub struct UpdateTask {

fn main() -> Anyhow<()> {
let args = Args::parse();

init_logging();

let project = ProjectLoader::current_dir()?
.with_config_file(args.config.as_deref())
.load()?;
Expand Down
2 changes: 1 addition & 1 deletion crates/rugpi-bakery/src/project/layers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use super::{
recipes::{ParameterValue, RecipeName},
repositories::RepositoryIdx,
};
use crate::caching::ModificationTime;
use crate::utils::caching::ModificationTime;

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
Expand Down
2 changes: 1 addition & 1 deletion crates/rugpi-bakery/src/project/library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use super::{
recipes::{Recipe, RecipeLoader},
repositories::{ProjectRepositories, RepositoryIdx},
};
use crate::{
use crate::utils::{
caching::mtime,
idx_vec::{new_idx_type, IdxVec},
};
Expand Down
20 changes: 1 addition & 19 deletions crates/rugpi-bakery/src/project/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::{
use rugpi_common::Anyhow;

use self::{config::BakeryConfig, library::Library, repositories::ProjectRepositories};
use crate::utils::prelude::*;

pub mod config;
pub mod images;
Expand All @@ -17,25 +18,6 @@ pub mod library;
pub mod recipes;
pub mod repositories;

/// Extension trait for [`OnceCell`].
pub trait OnceCellExt<T> {
/// Gets the contents of the cell or tries to initialize it.
///
/// We can remove this once `get_or_try_init` lands in the standard library (see
/// [#109737](https://github.com/rust-lang/rust/issues/109737)).
fn try_get_or_init<E>(&self, init: impl FnOnce() -> Result<T, E>) -> Result<&T, E>;
}

impl<T> OnceCellExt<T> for OnceCell<T> {
fn try_get_or_init<E>(&self, init: impl FnOnce() -> Result<T, E>) -> Result<&T, E> {
if let Some(value) = self.get() {
return Ok(value);
}
self.set(init()?).ok();
Ok(self.get().unwrap())
}
}

/// A project.
#[derive(Debug)]
#[non_exhaustive]
Expand Down
2 changes: 1 addition & 1 deletion crates/rugpi-bakery/src/project/recipes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use rugpi_common::Anyhow;
use serde::{Deserialize, Serialize};

use super::repositories::RepositoryIdx;
use crate::caching::{mtime_recursive, ModificationTime};
use crate::utils::caching::{mtime_recursive, ModificationTime};

/// Auxiliary data structure for loading recipes.
#[derive(Debug)]
Expand Down
2 changes: 1 addition & 1 deletion crates/rugpi-bakery/src/project/repositories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use sha1::{Digest, Sha1};
use xscript::{read_str, run, LocalEnv, Run};

use super::Project;
use crate::idx_vec::{new_idx_type, IdxVec};
use crate::utils::idx_vec::{new_idx_type, IdxVec};

#[derive(Debug)]
#[non_exhaustive]
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ macro_rules! new_idx_type {
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
$vis struct $name(usize);

impl $crate::idx_vec::Idx for $name {
impl $crate::utils::idx_vec::Idx for $name {
fn as_usize(self) -> usize {
self.0
}
Expand Down
12 changes: 12 additions & 0 deletions crates/rugpi-bakery/src/utils/logging.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
pub fn init_logging() {
tracing_subscriber::fmt::init();
}

#[macro_export]
macro_rules! bug {
($msg:literal) => {
tracing::error!("[BUG] {}", $msg)
};
}

pub use bug;
7 changes: 7 additions & 0 deletions crates/rugpi-bakery/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//! Various utilities.
pub mod caching;
pub mod idx_vec;
pub mod logging;
pub mod once_cell_ext;
pub mod prelude;
24 changes: 24 additions & 0 deletions crates/rugpi-bakery/src/utils/once_cell_ext.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use std::cell::OnceCell;

use super::logging::bug;

/// Extension trait for [`OnceCell`].
pub trait OnceCellExt<T> {
/// Gets the contents of the cell or tries to initialize it.
///
/// We can remove this once `get_or_try_init` lands in the standard library (see
/// [#109737](https://github.com/rust-lang/rust/issues/109737)).
fn try_get_or_init<E>(&self, init: impl FnOnce() -> Result<T, E>) -> Result<&T, E>;
}

impl<T> OnceCellExt<T> for OnceCell<T> {
fn try_get_or_init<E>(&self, init: impl FnOnce() -> Result<T, E>) -> Result<&T, E> {
if let Some(value) = self.get() {
return Ok(value);
}
if self.set(init()?).is_err() {
bug!("concurrent initialization of `OnceCell`");
}
Ok(self.get().unwrap())
}
}
5 changes: 5 additions & 0 deletions crates/rugpi-bakery/src/utils/prelude.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//! Custom prelude.
pub use tracing::{debug, error, info, trace, warn};

pub use super::{logging::bug, once_cell_ext::OnceCellExt};

0 comments on commit fc37611

Please sign in to comment.