Struct inspect::Response

source ·
pub struct Response<'a> { /* private fields */ }
Expand description

A type used to build an inspection response.

Implementations§

source§

impl Response<'_>

source

pub fn field<T>(&mut self, name: &str, value: T) -> &mut Self
where T: Inspect,

Adds a field to the response.

fn inspect_data(a: u32, b: &str, c: bool, maybe: Option<u32>, req: Request) {
    req.respond()
       .field("a", a)
       .field("b", b)
       .field("c", c)
       .field("maybe", maybe)
       .field("computed", format_args!("{a}-{b}"));
}
source

pub fn sensitivity_field<T>( &mut self, name: &str, sensitivity: SensitivityLevel, value: T, ) -> &mut Self
where T: Inspect,

Adds a field to the response with a SensitivityLevel.

source

pub fn hex<T>(&mut self, name: &str, value: T) -> &mut Self
where T: Into<Value>,

Adds a hexadecimal field to the response.

This is a convenience method for field(name, Value::hex(value)).

source

pub fn counter<T>(&mut self, name: &str, value: T) -> &mut Self
where T: Into<Value>,

Adds a counter field to the response.

This is a convenience method for field(name, Value::counter(value)).

source

pub fn sensitivity_counter<T>( &mut self, name: &str, sensitivity: SensitivityLevel, value: T, ) -> &mut Self
where T: Into<Value>,

Adds a counter field to the response.

This is a convenience method for sensitivity_field(name, sensitivity, Value::counter(value)).

source

pub fn binary<T>(&mut self, name: &str, value: T) -> &mut Self
where T: Into<Value>,

Adds a binary field to the response.

This is a convenience method for field(name, Value::binary(value)).

source

pub fn display(&mut self, name: &str, value: &impl Display) -> &mut Self

Adds a string field that implements std::fmt::Display.

This is a convenience method for field_with(name, || value.to_string()). It lazily allocates the string only when the inspector requests it, so it is more efficient than using format! or to_string().

fn inspect_data(path: &Path, id: u32, req: Request) {
    req.respond().display("path", &path.display());
}
source

pub fn display_debug(&mut self, name: &str, value: &impl Debug) -> &mut Self

Adds a string field that implements std::fmt::Debug.

This is a convenience method for field(name, format_args!("{value:?}")).

Take care not to overuse this. This is best used for fields that have a short or natural Debug implementation, such as dataless enums. If your field is an aggregate type whose Debug implementation has a complicated structure, consider implementing Inspect on the type and calling field.

source

pub fn field_with<F, V>(&mut self, name: &str, f: F) -> &mut Self
where F: FnOnce() -> V, V: Inspect,

Adds a field to the response, calling f to get its value.

source

pub fn sensitivity_field_with<F, V>( &mut self, name: &str, sensitivity: SensitivityLevel, f: F, ) -> &mut Self
where F: FnOnce() -> V, V: Inspect,

Adds a field to the response with a SensitivityLevel, calling f to get its value.

source

pub fn field_mut<T>(&mut self, name: &str, value: &mut T) -> &mut Self
where T: InspectMut + ?Sized,

Adds a mutable field to the response.

source

pub fn sensitivity_field_mut<T>( &mut self, name: &str, sensitivity: SensitivityLevel, value: &mut T, ) -> &mut Self
where T: InspectMut + ?Sized,

Adds a mutable field to the response with a SensitivityLevel.

source

pub fn field_mut_with<F, V, E>(&mut self, name: &str, f: F) -> &mut Self
where F: FnOnce(Option<&str>) -> Result<V, E>, V: Into<Value>, E: Into<Box<dyn Error + Send + Sync>>,

Adds a mutable field with custom get/update function.

source

pub fn fields<I, N, C>(&mut self, name: &str, children: I) -> &mut Self
where I: Iterator<Item = (N, C)>, N: ToString, C: Inspect,

Inspects a list of children objects as children of the node name.

Each element of children should be a tuple (child_name, child). child_name must be convertible to a string.


struct Child(u32);
impl Inspect for Child {
    fn inspect(&self, req: Request) {
        req.respond().field("x", self.0);
    }
}

struct Parent(Vec<Child>);
impl Inspect for Parent {
    fn inspect(&self, req: Request) {
        req.respond().fields("children", self.0.iter().enumerate());
    }
}

assert_eq!(
    inspect(
        "",
        &Parent(vec![Child(5), Child(12)]),
    )
    .results()
    .to_string(),
    r#"{children: {0: {x: 5}, 1: {x: 12}}}"#
);
source

pub fn fields_mut<I, N, C>(&mut self, name: &str, children: I) -> &mut Self
where I: Iterator<Item = (N, C)>, N: ToString, C: InspectMut,

Inspects a list of children objects as children of the node name.

Each element of children should be a tuple (child_name, child). child_name must be convertible to a string.

struct Child(u32);
impl InspectMut for Child {
    fn inspect_mut(&mut self, req: Request) {
        req.respond().field("x", self.0);
    }
}

struct Parent(Vec<Child>);
impl InspectMut for Parent {
    fn inspect_mut(&mut self, req: Request) {
        req.respond().fields_mut("children", self.0.iter_mut().enumerate());
    }
}

assert_eq!(
    inspect(
        "",
        &mut Parent(vec![Child(5), Child(12)]),
    )
    .results()
    .to_string(),
    r#"{children: {0: {x: 5}, 1: {x: 12}}}"#
);
source

pub fn child<F: FnOnce(Request<'_>)>(&mut self, name: &str, f: F) -> &mut Self

Adds a child node to the inspection response.

If name is empty, then the results of the inspection are merged into this node.

source

pub fn sensitivity_child<F: FnOnce(Request<'_>)>( &mut self, name: &str, sensitivity: SensitivityLevel, f: F, ) -> &mut Self

Adds a child node to the inspection response with a SensitivityLevel.

If name is empty, then the results of the inspection are merged into this node.

source

pub fn merge(&mut self, child: impl InspectMut) -> &mut Self

Inspects an object and merges its responses into this node without creating a child node.

source

pub fn request(&mut self) -> Request<'_>

Gets another request for this response. The response to that request will be merged into this response.

Trait Implementations§

source§

impl Drop for Response<'_>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for Response<'a>

§

impl<'a> !RefUnwindSafe for Response<'a>

§

impl<'a> Send for Response<'a>

§

impl<'a> Sync for Response<'a>

§

impl<'a> Unpin for Response<'a>

§

impl<'a> !UnwindSafe for Response<'a>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

impl<T, U> Upcast<U> for T
where U: Downcast<T>,