Skip to main content

Crate aliased

Crate aliased 

Source
Expand description

Replace noisy Debug output with short, human-friendly aliases.

Register aliases for specific values up front, then format with aliased(...): the crate runs the value’s Debug (or {:#?}) output through string substitution and swaps each registered value’s debug representation for its alias.

§Flavors

Two flavors are available, selected by Cargo features:

Either or both may be enabled while testing. For release builds, it’s intended that you use neither feature, i.e. use default-features = false.

When using no features, the whole API still compiles, but as a no-op: aliased(...) formats with plain Debug and registration calls do nothing. This lets a production build switch aliasing off with default-features = false without touching call sites (and without pulling in aho-corasick / regex) for a minimal footprint.

§Example (global)

use aliased::*;

#[derive(Debug)]
struct Key([u8; 32]);

// All aliases for this type will be prefixed with "K|"
Key::alias_prefix("K");

let a = Key([1; 32]);
let b = Key([2; 32]);
let c = Key([3; 32]);

// The default Debug output is verbose.
assert_eq!(format!("{:?}", a), "Key([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])");

// Without applying any aliases, the `aliased()` output is unchanged.
assert_eq!(format!("{:?}", a.aliased()), "Key([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])");

// You can apply a numbered alias:

a.alias_numbered();
b.alias_numbered();
c.alias_numbered();
assert_eq!(format!("{:?}", a.aliased()), "⟪K|#000⟫");
assert_eq!(format!("{:?}", b.aliased()), "⟪K|#001⟫");
assert_eq!(format!("{:?}", c.aliased()), "⟪K|#002⟫");

// Or you can apply a named alias (even after already applying a numbered alias)

a.alias_named("alice");
b.alias_named("bob");
c.alias_named("carol");
assert_eq!(format!("{:?}", a.aliased()), "⟪K|alice⟫");
assert_eq!(format!("{:?}", b.aliased()), "⟪K|bob⟫");
assert_eq!(format!("{:?}", c.aliased()), "⟪K|carol⟫");

// Works for pretty-printed output too.
// Without `aliased()`, this output would be 34 lines long!.
assert_eq!(format!("{:#?}", a.aliased()), "⟪K|alice⟫");

§Example (contextual)

use aliased::AliasContext;
use aliased::contextual::*;

#[derive(Debug)]
struct Key([u8; 32]);

let ctx = AliasContext::new();
Key::alias_prefix(&ctx, "K");

let a = Key([1; 32]);
a.alias_named(&ctx, "alice");

assert_eq!(format!("{:?}", a.aliased(&ctx)), "⟪K|alice⟫");

Modules§

contextual
Aliasing API parameterized by an explicit AliasContext.

Structs§

AliasContext
A registry of value-to-alias mappings.
Aliased
Debug wrapper produced by Aliasing::aliased. Formatting it runs the inner value’s Debug (or {:#?}) output and substitutes aliases drawn from the global context.

Traits§

Aliasing
Extension trait providing aliasing methods backed by the process-wide global_ctx. Implemented for every T: Debug + 'static.