Skip to content

Commit

Permalink
feat(HookExt): Add extended hook functionality with custom deny lists
Browse files Browse the repository at this point in the history
- Added `HookExt` trait to provide extended hook functionality.
- Implemented `HookExt` for functions, tuples, and `ShadowHook` struct.
- Each implementation provides methods to return the default deny list and the inner hook function.

This change allows more flexible handling of hooks and deny lists, enabling better customization and control over the hook behavior.
  • Loading branch information
baoyachi committed Nov 13, 2024
1 parent ec8615d commit 84096a0
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 4 deletions.
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 84096a0

Please sign in to comment.