pub struct Response<'a> { /* private fields */ }
Expand description
A type used to build an inspection response.
Implementations§
source§impl Response<'_>
impl Response<'_>
sourcepub fn field<T>(&mut self, name: &str, value: T) -> &mut Selfwhere
T: Inspect,
pub fn field<T>(&mut self, name: &str, value: T) -> &mut Selfwhere
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}"));
}
sourcepub fn sensitivity_field<T>(
&mut self,
name: &str,
sensitivity: SensitivityLevel,
value: T,
) -> &mut Selfwhere
T: Inspect,
pub fn sensitivity_field<T>(
&mut self,
name: &str,
sensitivity: SensitivityLevel,
value: T,
) -> &mut Selfwhere
T: Inspect,
Adds a field to the response with a SensitivityLevel
.
sourcepub fn hex<T>(&mut self, name: &str, value: T) -> &mut Self
pub fn hex<T>(&mut self, name: &str, value: T) -> &mut Self
Adds a hexadecimal field to the response.
This is a convenience method for field(name, Value::hex(value))
.
sourcepub fn counter<T>(&mut self, name: &str, value: T) -> &mut Self
pub fn counter<T>(&mut self, name: &str, value: T) -> &mut Self
Adds a counter field to the response.
This is a convenience method for field(name, Value::counter(value))
.
sourcepub fn sensitivity_counter<T>(
&mut self,
name: &str,
sensitivity: SensitivityLevel,
value: T,
) -> &mut Self
pub fn sensitivity_counter<T>( &mut self, name: &str, sensitivity: SensitivityLevel, value: T, ) -> &mut Self
Adds a counter field to the response.
This is a convenience method for sensitivity_field(name, sensitivity, Value::counter(value))
.
sourcepub fn binary<T>(&mut self, name: &str, value: T) -> &mut Self
pub fn binary<T>(&mut self, name: &str, value: T) -> &mut Self
Adds a binary field to the response.
This is a convenience method for field(name, Value::binary(value))
.
sourcepub fn display(&mut self, name: &str, value: &impl Display) -> &mut Self
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());
}
sourcepub fn display_debug(&mut self, name: &str, value: &impl Debug) -> &mut Self
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
.
sourcepub fn field_with<F, V>(&mut self, name: &str, f: F) -> &mut Self
pub fn field_with<F, V>(&mut self, name: &str, f: F) -> &mut Self
Adds a field to the response, calling f
to get its value.
sourcepub fn sensitivity_field_with<F, V>(
&mut self,
name: &str,
sensitivity: SensitivityLevel,
f: F,
) -> &mut Self
pub fn sensitivity_field_with<F, V>( &mut self, name: &str, sensitivity: SensitivityLevel, f: F, ) -> &mut Self
Adds a field to the response with a SensitivityLevel
, calling f
to get its value.
sourcepub fn field_mut<T>(&mut self, name: &str, value: &mut T) -> &mut Selfwhere
T: InspectMut + ?Sized,
pub fn field_mut<T>(&mut self, name: &str, value: &mut T) -> &mut Selfwhere
T: InspectMut + ?Sized,
Adds a mutable field to the response.
sourcepub fn sensitivity_field_mut<T>(
&mut self,
name: &str,
sensitivity: SensitivityLevel,
value: &mut T,
) -> &mut Selfwhere
T: InspectMut + ?Sized,
pub fn sensitivity_field_mut<T>(
&mut self,
name: &str,
sensitivity: SensitivityLevel,
value: &mut T,
) -> &mut Selfwhere
T: InspectMut + ?Sized,
Adds a mutable field to the response with a SensitivityLevel
.
sourcepub fn field_mut_with<F, V, E>(&mut self, name: &str, f: F) -> &mut Self
pub fn field_mut_with<F, V, E>(&mut self, name: &str, f: F) -> &mut Self
Adds a mutable field with custom get/update function.
sourcepub fn fields<I, N, C>(&mut self, name: &str, children: I) -> &mut Self
pub fn fields<I, N, C>(&mut self, name: &str, children: I) -> &mut Self
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}}}"#
);
sourcepub fn fields_mut<I, N, C>(&mut self, name: &str, children: I) -> &mut Self
pub fn fields_mut<I, N, C>(&mut self, name: &str, children: I) -> &mut Self
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}}}"#
);
sourcepub fn child<F: FnOnce(Request<'_>)>(&mut self, name: &str, f: F) -> &mut Self
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.
sourcepub fn sensitivity_child<F: FnOnce(Request<'_>)>(
&mut self,
name: &str,
sensitivity: SensitivityLevel,
f: F,
) -> &mut Self
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.
sourcepub fn merge(&mut self, child: impl InspectMut) -> &mut Self
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.