use crate::exploration::mutation::ExplorationResult;
use crate::exploration::{MapNodeId, MutationInput};
pub struct WorkResultAdapter {
pub node_id: MapNodeId,
pub action_name: String,
pub target: Option<String>,
pub result: ExplorationResult,
}
impl WorkResultAdapter {
pub fn new(
node_id: MapNodeId,
action_name: &str,
target: Option<&str>,
success: bool,
discovery: Option<&serde_json::Value>,
) -> Self {
let result = if success {
if let Some(disc) = discovery {
if let Some(arr) = disc.as_array() {
let children: Vec<String> = arr
.iter()
.filter_map(|v| v.as_str().map(|s| s.to_string()))
.collect();
if !children.is_empty() {
ExplorationResult::Discover(children)
} else {
ExplorationResult::Success
}
} else {
ExplorationResult::Success
}
} else {
ExplorationResult::Success
}
} else {
ExplorationResult::Fail(None)
};
Self {
node_id,
action_name: action_name.to_string(),
target: target.map(|s| s.to_string()),
result,
}
}
}
impl MutationInput for WorkResultAdapter {
fn node_id(&self) -> MapNodeId {
self.node_id
}
fn action_name(&self) -> &str {
&self.action_name
}
fn target(&self) -> Option<&str> {
self.target.as_deref()
}
fn result(&self) -> &ExplorationResult {
&self.result
}
}