Skip to content

Commit

Permalink
Move some code around
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Levick <ryan.levick@fermyon.com>
  • Loading branch information
rylev committed Sep 17, 2024
1 parent c987268 commit 5d0c99c
Showing 1 changed file with 48 additions and 45 deletions.
93 changes: 48 additions & 45 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@ wasmtime::component::bindgen!({

use spin::mqtt_trigger::spin_mqtt_types as mqtt_types;

#[derive(Args)]
pub struct CliArgs {
/// If true, run each component once and exit
#[clap(long)]
pub test: bool,
}

// The trigger structure with all values processed and ready
#[derive(Clone)]
pub struct MqttTrigger {
Expand All @@ -35,44 +28,6 @@ pub struct MqttTrigger {
test: bool,
}

// Trigger settings (raw serialization format)
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
struct TriggerMetadata {
address: String,
username: String,
password: String,
keep_alive_interval: u64,
}

impl TriggerMetadata {
/// Resolve any variables inside the trigger metadata.
async fn resolve_variables<F: RuntimeFactors>(
&mut self,
trigger_app: &TriggerApp<MqttTrigger, F>,
) -> anyhow::Result<()> {
let address = resolve_variables(trigger_app, self.address.clone()).await?;
let username = resolve_variables(trigger_app, self.username.clone()).await?;
let password = resolve_variables(trigger_app, self.password.clone()).await?;
self.address = address;
self.username = username;
self.password = password;
Ok(())
}
}

// Per-component settings (raw serialization format)
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct ComponentConfig {
/// The component id
component: String,
/// The topic
topic: String,
/// The QoS level
qos: i32,
}

impl<F: RuntimeFactors> Trigger<F> for MqttTrigger {
const TYPE: &'static str = "mqtt";
type InstanceState = ();
Expand Down Expand Up @@ -143,6 +98,7 @@ impl<F: RuntimeFactors> Trigger<F> for MqttTrigger {
}

impl MqttTrigger {
/// Handle a specific MQTT event
async fn handle_mqtt_event<F: RuntimeFactors>(
&self,
trigger_app: &TriggerApp<Self, F>,
Expand All @@ -162,6 +118,7 @@ impl MqttTrigger {
.map_err(|err| anyhow!("failed to execute guest: {err}"))
}

/// Run the listener for a specific component
async fn run_listener<F: RuntimeFactors>(
&self,
trigger_app: &TriggerApp<Self, F>,
Expand Down Expand Up @@ -219,6 +176,52 @@ impl MqttTrigger {
}
}

/// Command line arguments
#[derive(Args)]
pub struct CliArgs {
/// If true, run each component once and exit
#[clap(long)]
pub test: bool,
}

// Trigger settings (raw serialization format)
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
struct TriggerMetadata {
address: String,
username: String,
password: String,
keep_alive_interval: u64,
}

impl TriggerMetadata {
/// Resolve any variables inside the trigger metadata.
async fn resolve_variables<F: RuntimeFactors>(
&mut self,
trigger_app: &TriggerApp<MqttTrigger, F>,
) -> anyhow::Result<()> {
let address = resolve_variables(trigger_app, self.address.clone()).await?;
let username = resolve_variables(trigger_app, self.username.clone()).await?;
let password = resolve_variables(trigger_app, self.password.clone()).await?;
self.address = address;
self.username = username;
self.password = password;
Ok(())
}
}

// Per-component settings (raw serialization format)
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct ComponentConfig {
/// The component id
component: String,
/// The topic
topic: String,
/// The QoS level
qos: i32,
}

/// Resolve variables in an expression against the variables in the provided trigger app.
async fn resolve_variables<F: RuntimeFactors>(
trigger_app: &TriggerApp<MqttTrigger, F>,
Expand Down

0 comments on commit 5d0c99c

Please sign in to comment.