Skip to content

Commit

Permalink
Merge pull request #190 from baoyachi/hook_ext
Browse files Browse the repository at this point in the history
feat(HookExt): Add extended hook functionality with custom deny lists
  • Loading branch information
baoyachi authored Nov 14, 2024
2 parents ec8615d + bad046d commit 909510e
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "shadow-rs"
version = "0.35.2"
version = "0.36.0"
authors = ["baoyachi <[email protected]>"]
edition = "2021"
description = "A build-time information stored in your rust project"
Expand Down
70 changes: 70 additions & 0 deletions src/hook.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use crate::{default_deny, SdResult, ShadowConst};
use std::collections::BTreeSet;
use std::fs::File;

/// A trait that extends the functionality of hooks.
/// It provides methods to get the default deny list and the inner hook function.
pub trait HookExt {
/// Returns the default deny list.
fn default_deny(&self) -> BTreeSet<ShadowConst>;

/// Returns a reference to the inner hook function.
fn hook_inner(&self) -> &dyn Fn(&File) -> SdResult<()>;
}

/// Implement the `HookExt` trait for any function that takes a `&File` and returns a `SdResult<()>`.
impl<F> HookExt for F
where
F: Fn(&File) -> SdResult<()>,
{
/// Returns the default deny list using the `default_deny` function from the crate.
fn default_deny(&self) -> BTreeSet<ShadowConst> {
default_deny()
}

/// Returns a reference to the function itself.
fn hook_inner(&self) -> &dyn Fn(&File) -> SdResult<()> {
self
}
}

/// Implement the `HookExt` trait for a tuple containing a function and a deny list.
impl<F> HookExt for (F, BTreeSet<ShadowConst>)
where
F: Fn(&File) -> SdResult<()>,
{
/// Returns the deny list stored in the second element of the tuple.
fn default_deny(&self) -> BTreeSet<ShadowConst> {
self.1.clone()
}

/// Returns a reference to the function stored in the first element of the tuple.
fn hook_inner(&self) -> &dyn Fn(&File) -> SdResult<()> {
&self.0
}
}

/// A struct representing a shadow hook with an inner function and a deny list.
pub struct ShadowHook<F> {
/// The inner function that will be used as the hook.
pub hook: F,

/// The deny list associated with this hook.
pub deny: BTreeSet<ShadowConst>,
}

/// Implement the `HookExt` trait for the `ShadowHook` struct.
impl<F> HookExt for ShadowHook<F>
where
F: Fn(&File) -> SdResult<()>,
{
/// Returns the deny list associated with this `ShadowHook`.
fn default_deny(&self) -> BTreeSet<ShadowConst> {
self.deny.clone()
}

/// Returns a reference to the inner function of this `ShadowHook`.
fn hook_inner(&self) -> &dyn Fn(&File) -> SdResult<()> {
&self.hook
}
}
10 changes: 6 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ mod env;
mod err;
mod gen_const;
mod git;
mod hook;

/// Re-exported from the is_debug crate
pub use is_debug::*;
Expand All @@ -178,6 +179,7 @@ use crate::gen_const::{
};
pub use err::{SdResult, ShadowError};

use crate::hook::HookExt;
pub use {build::ShadowConst, env::*, git::*};

pub trait Format {
Expand Down Expand Up @@ -292,10 +294,10 @@ pub fn new_deny(deny_const: BTreeSet<ShadowConst>) -> SdResult<()> {
///
pub fn new_hook<F>(f: F) -> SdResult<()>
where
F: FnOnce(&File) -> SdResult<()>,
F: HookExt,
{
let shadow = Shadow::build(default_deny())?;
shadow.hook(f)
let shadow = Shadow::build(f.default_deny())?;
shadow.hook(f.hook_inner())
}

/// Returns the contents of [`std::env::vars`] as an ordered map.
Expand Down Expand Up @@ -328,7 +330,7 @@ impl Shadow {
/// The hook function is run as well, allowing it to append to `shadow-rs`'s output.
pub fn hook<F>(&self, f: F) -> SdResult<()>
where
F: FnOnce(&File) -> SdResult<()>,
F: Fn(&File) -> SdResult<()>,
{
let desc = r#"/// Below code generated by project custom from by build.rs"#;
writeln!(&self.f, "\n{desc}\n")?;
Expand Down

0 comments on commit 909510e

Please sign in to comment.