context_weaver/core/processors/
wildcard.rs1use std::sync::Arc;
2
3use rand::Rng;
4
5use crate::{WorldInfoNode, WorldInfoProcessor};
6
7use super::{PluginBridge, WorldInfoProcessorFactory};
8
9#[derive(Debug, Clone)]
10pub struct WildcardProcessor {
11 options: Vec<String>,
12}
13
14impl WorldInfoNode for WildcardProcessor {
15 fn content(&self) -> Result<String, crate::WorldInfoError> {
16 self.process()
17 }
18
19 fn name(&self) -> String {
20 "weaver.core.wildcard".to_string()
21 }
22
23 fn cloned(&self) -> Box<dyn WorldInfoNode> {
24 Box::new(self.to_owned())
25 }
26}
27
28impl WorldInfoProcessor for WildcardProcessor {
29 fn process(&self) -> Result<String, crate::WorldInfoError> {
30 let length = self.options.len();
31
32 if length == 0 {
34 return Ok(String::from("").into());
35 }
36
37 let mut rng = rand::rng();
38 let index = rng.random_range(0..length);
39
40 Ok(self.options[index].clone().into())
41 }
42}
43
44pub struct WildcardProcessorFactory;
45
46impl<P: PluginBridge + 'static> WorldInfoProcessorFactory<P> for WildcardProcessorFactory {
47 fn create(&self, properties: &serde_json::Value, _bridge: &Arc<P>) -> Box<dyn WorldInfoProcessor> {
48 println!("Creating wildcard");
49 let raw_items: Vec<crate::WorldInfoType> = properties["items"].as_array().unwrap().iter().map(|x| x.into()).collect();
50 let mut items = Vec::new();
51
52 for item in raw_items {
53 match item {
54 crate::WorldInfoType::String(s) => items.push(s),
55 _ => continue
56 }
57 }
58
59 Box::new(WildcardProcessor { options: items })
60 }
61}