XTSoftwareLabs/shimforge
shimforge
XTSoftwareLabs/shimforge
shimforge
A Rust crate for mocking concrete types and functions (e.g., std::fs, reqwest) without requiring traits or changes to production code. Supports thread-local and global sessions.
Visit project- First shared
- Sep 23, 2026
- HN activity
- 10
Show HN@xtsoftwarelabsSep 23, 2026
Show HN: a Rust crate to mock concrete type without using trait
The approach in this crate is you don't change any thing in your production code but directly mock functions such as std, reqwest and etc. // Production code use std::fs; pub fn claim_slot() -> Result<(), String> { fs::create_dir_all("/var/run/dispatcher") .map_err(|error| format!("cannot claim a slot: {error}")) } // Test code use shimforge::{Session, mock}; use std::io; #[test] fn a_denied_slot_is_reported() { let mut session = Session::new(); let mkdir = mock!(session, fs::create_dir_all::<&str>, fn(&str) -> io::Result<()>); mkdir .expect() .with(|path| *path == "/var/run/dispatcher") .once() .returning(|_| Err(io::ErrorKind::PermissionDenied.into())); let error = claim_slot().unwrap_err(); assert!(error.starts_with("cannot claim a slot")); }
Community