|
| 1 | +mod data_types; |
| 2 | +mod events; |
| 3 | +mod functions; |
| 4 | + |
| 5 | +use hercules::{Action, ConfigurationDefinition, HerculesEvent, Translation}; |
| 6 | +use tokio_stream::StreamExt; |
| 7 | + |
| 8 | +fn env(key: &str, default: &str) -> String { |
| 9 | + std::env::var(key).unwrap_or_else(|_| default.to_string()) |
| 10 | +} |
| 11 | + |
| 12 | +/// `FibonacciRuntimeFunction`, `FibonacciFunction`, `EmailAddress` and |
| 13 | +/// `UserCreatedRuntimeEvent` never appear below — attaching |
| 14 | +/// `#[hercules::runtime_function]` / `function` / `data_type` / |
| 15 | +/// `runtime_event` registered each of them automatically as part of |
| 16 | +/// `Action::new` (see `hercules::registration`). There's simply nothing left |
| 17 | +/// to wire up by hand for this example. |
| 18 | +fn build_action() -> Action { |
| 19 | + Action::new(env("ACTION_ID", "example-action"), env("VERSION", "0.0.0")) |
| 20 | + .aquila_url(env("AQUILA_URL", "127.0.0.1:8081")) |
| 21 | + .author("code0-tech") |
| 22 | + .icon("tabler:bolt") |
| 23 | + .documentation("A simple example action") |
| 24 | + .name([Translation::new("en-US", "Example Action")]) |
| 25 | + .configuration( |
| 26 | + ConfigurationDefinition::new("EXAMPLE_CONFIG", "string") |
| 27 | + .name([Translation::new("en-US", "Example Config")]), |
| 28 | + ) |
| 29 | +} |
| 30 | + |
| 31 | +#[tokio::main] |
| 32 | +async fn main() -> hercules::Result<()> { |
| 33 | + // RUST_LOG=hercules=debug,simple_example_rs=debug cargo run |
| 34 | + env_logger::init(); |
| 35 | + |
| 36 | + let action = build_action(); |
| 37 | + let mut events = action.subscribe(); |
| 38 | + |
| 39 | + // The returned `Connected` handle can be discarded immediately: the |
| 40 | + // background dispatch task (started inside `connect`) holds its own |
| 41 | + // `Arc` to the shared connection state, so nothing here needs to keep |
| 42 | + // it alive. |
| 43 | + action |
| 44 | + .connect(env("AUTH_TOKEN", "token"), None) |
| 45 | + .await |
| 46 | + .unwrap_or_else(|err| panic!("failed to connect to Aquila: {err}")); |
| 47 | + |
| 48 | + // React to connection lifecycle events on the main task for as long as |
| 49 | + // the process runs. Panicking here (unlike inside a spawned task) takes |
| 50 | + // the whole process down on an unrecoverable stream error. |
| 51 | + while let Some(event) = events.next().await { |
| 52 | + match event { |
| 53 | + HerculesEvent::Connected => log::info!("connected to Aquila"), |
| 54 | + HerculesEvent::Error(error) => panic!("Aquila stream error: {error}"), |
| 55 | + _ => {} |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + Ok(()) |
| 60 | +} |
| 61 | + |
| 62 | +#[cfg(test)] |
| 63 | +mod tests { |
| 64 | + use hercules::wire::Module; |
| 65 | + |
| 66 | + use super::build_action; |
| 67 | + |
| 68 | + /// Exercises the real registration path end to end — link-time |
| 69 | + /// `inventory` auto-registration, `Function`/`RuntimeFunction` merging, |
| 70 | + /// and `schemars`-driven data type resolution — by building the wire |
| 71 | + /// `Module` without connecting. |
| 72 | + #[test] |
| 73 | + fn builds_a_well_formed_module() { |
| 74 | + let module: Module = build_action().build_module(); |
| 75 | + |
| 76 | + assert_eq!(module.identifier, "example-action"); |
| 77 | + |
| 78 | + let email = module |
| 79 | + .definition_data_types |
| 80 | + .iter() |
| 81 | + .find(|dt| dt.identifier == "email_address") |
| 82 | + .expect("email_address data type"); |
| 83 | + assert_eq!(email.r#type, "string"); |
| 84 | + assert!(matches!( |
| 85 | + email.rules.first().and_then(|r| r.config.clone()), |
| 86 | + Some(tucana::shared::definition_data_type_rule::Config::Regex(_)) |
| 87 | + )); |
| 88 | + |
| 89 | + let fibonacci = module |
| 90 | + .function_definitions |
| 91 | + .iter() |
| 92 | + .find(|f| f.runtime_name == "fibonacci") |
| 93 | + .expect("fibonacci function"); |
| 94 | + assert_eq!(fibonacci.runtime_definition_name, "fibonacci_runtime"); |
| 95 | + let param = &fibonacci.parameter_definitions[0]; |
| 96 | + assert_eq!(param.runtime_name, "test"); |
| 97 | + assert_eq!( |
| 98 | + param.default_value.as_ref().and_then(|v| v.kind.clone()), |
| 99 | + Some(tucana::shared::value::Kind::NumberValue( |
| 100 | + tucana::shared::NumberValue { |
| 101 | + number: Some(tucana::shared::number_value::Number::Integer(10)) |
| 102 | + } |
| 103 | + )) |
| 104 | + ); |
| 105 | + |
| 106 | + // omit_definition on the runtime function means it isn't separately |
| 107 | + // published under its own identifier. |
| 108 | + assert!(module |
| 109 | + .function_definitions |
| 110 | + .iter() |
| 111 | + .all(|f| f.runtime_name != "fibonacci_runtime")); |
| 112 | + assert_eq!( |
| 113 | + module.runtime_function_definitions[0].runtime_name, |
| 114 | + "fibonacci_runtime" |
| 115 | + ); |
| 116 | + |
| 117 | + assert_eq!(module.runtime_flow_types[0].identifier, "user_created"); |
| 118 | + } |
| 119 | +} |
0 commit comments