pub struct ContextBuilder { /* private fields */ }Expand description
Fluent builder for constructing DualContextError.
§Purpose
Provides ergonomic API for building errors with:
- Public/internal separation
- Type-safe category assignment
- Sensible defaults
§State Tracking
Builder tracks context assignment to prevent accidental overwrites:
- Debug builds: Panics on double-set with clear diagnostics
- Release builds: Last-write-wins (no runtime overhead)
This is intentional: debug builds catch logic bugs, release builds prioritize performance and allow intentional overwrites in complex error construction.
If you need compile-time enforcement, consider the typestate pattern, but this was rejected for ergonomics (see DESIGN_DECISIONS.md).
§Example
use palisade_errors::{ContextBuilder, OperationCategory, SocAccess};
let err = ContextBuilder::new()
.public_lie("Access denied")
.internal_sensitive("Unauthorized: user lacks 'admin' role")
.category(OperationCategory::Detection)
.build();
// Public message: "Access denied"
assert_eq!(err.external_message(), "Access denied");
// Internal context requires SocAccess
let access = SocAccess::acquire();
let internal = err.internal().expose_sensitive(&access);
assert_eq!(internal, Some("Unauthorized: user lacks 'admin' role"));Implementations§
Source§impl ContextBuilder
impl ContextBuilder
Sourcepub fn public_lie(self, message: impl Into<Cow<'static, str>>) -> Self
pub fn public_lie(self, message: impl Into<Cow<'static, str>>) -> Self
Sourcepub fn public_truth(self, message: impl Into<Cow<'static, str>>) -> Self
pub fn public_truth(self, message: impl Into<Cow<'static, str>>) -> Self
Sourcepub fn internal_diagnostic(self, message: impl Into<Cow<'static, str>>) -> Self
pub fn internal_diagnostic(self, message: impl Into<Cow<'static, str>>) -> Self
Sourcepub fn internal_sensitive(self, message: impl Into<Cow<'static, str>>) -> Self
pub fn internal_sensitive(self, message: impl Into<Cow<'static, str>>) -> Self
Sourcepub fn internal_lie(self, message: impl Into<Cow<'static, str>>) -> Self
pub fn internal_lie(self, message: impl Into<Cow<'static, str>>) -> Self
Sourcepub fn category(self, category: OperationCategory) -> Self
pub fn category(self, category: OperationCategory) -> Self
Set operation category.
§Example
let builder = ContextBuilder::new()
.category(OperationCategory::Detection);Sourcepub fn build(self) -> DualContextError
pub fn build(self) -> DualContextError
Sourcepub fn try_build(self) -> Result<DualContextError, ContextBuilderError>
pub fn try_build(self) -> Result<DualContextError, ContextBuilderError>
Try to build the DualContextError, returning an error if incomplete.
§Errors
Returns Err(ContextBuilderError) if public or internal context is missing.
The error includes diagnostic information about builder state.
§Example
let result = ContextBuilder::new()
.public_lie("Error")
.try_build();
assert!(result.is_err()); // Missing internal contextTrait Implementations§
Auto Trait Implementations§
impl Freeze for ContextBuilder
impl RefUnwindSafe for ContextBuilder
impl Send for ContextBuilder
impl Sync for ContextBuilder
impl Unpin for ContextBuilder
impl UnwindSafe for ContextBuilder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more