impl RequestCapture {
#[must_use]
pub fn new() -> Self {
Self {
input: String::new(),
params: HashMap::new(),
}
}
#[must_use]
pub fn with_input(mut self, input: &str) -> Self {
self.input = input.to_string();
self
}
#[must_use]
pub fn with_params(mut self, key: &str, value: &str) -> Self {
self.params.insert(key.to_string(), value.to_string());
self
}
#[must_use]
pub fn input(&self) -> &str {
&self.input
}
#[must_use]
pub fn params(&self) -> &HashMap<String, String> {
&self.params
}
#[must_use]
pub fn to_json(&self) -> String {
let params_json: Vec<String> = self
.params
.iter()
.map(|(k, v)| format!("\"{}\":\"{}\"", k, v))
.collect();
format!(
"{{\"input\":\"{}\",\"params\":{{{}}}}}",
self.input,
params_json.join(",")
)
}
pub fn from_json(json: &str) -> std::result::Result<Self, &'static str> {
let input_start = json.find("\"input\":\"").ok_or("Missing input")?;
let input_rest = &json[input_start + 9..];
let input_end = input_rest.find('"').ok_or("Invalid input")?;
let input = &input_rest[..input_end];
Ok(Self {
input: input.to_string(),
params: HashMap::new(),
})
}
}
impl Default for RequestCapture {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone)]
pub struct StateDump {
error: String,
stack_trace: String,
state: HashMap<String, String>,
}
impl StateDump {
#[must_use]
pub fn new() -> Self {
Self {
error: String::new(),
stack_trace: String::new(),
state: HashMap::new(),
}
}
#[must_use]
pub fn with_error(mut self, error: &str) -> Self {
self.error = error.to_string();
self
}
#[must_use]
pub fn with_stack_trace(mut self, trace: &str) -> Self {
self.stack_trace = trace.to_string();
self
}
#[must_use]
pub fn with_state(mut self, key: &str, value: &str) -> Self {
self.state.insert(key.to_string(), value.to_string());
self
}
#[must_use]
pub fn error(&self) -> &str {
&self.error
}
#[must_use]
pub fn stack_trace(&self) -> &str {
&self.stack_trace
}
#[must_use]
pub fn state(&self) -> &HashMap<String, String> {
&self.state
}
#[must_use]
pub fn to_json(&self) -> String {
let state_json: Vec<String> = self
.state
.iter()
.map(|(k, v)| format!("\"{}\":\"{}\"", k, v))
.collect();
format!(
"{{\"error\":\"{}\",\"stack_trace\":\"{}\",\"state\":{{{}}}}}",
self.error,
self.stack_trace.replace('\n', "\\n"),
state_json.join(",")
)
}
}
impl Default for StateDump {
fn default() -> Self {
Self::new()
}
}