Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename manifest fields, add example setting #5

Merged
merged 4 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions edgee-component.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
manifest_version = 1

[package]
[component]
name = "example-rust-component"
version = "1.0.0"

Expand All @@ -11,6 +11,10 @@ documentation = "https://github.com/edgee-cloud/example-rust-component"
repository = "https://github.com/edgee-cloud/example-rust-component"
wit-world-version = "0.4.0"

[package.build]
[component.build]
command = "cargo build --target wasm32-wasip2 --release --target-dir ./target && mv ./target/wasm32-wasip2/release/example_rust_component.wasm ./dc_component.wasm"
output_path = "dc_component.wasm"

[component.settings.example]
title = "Example Config Field"
type = "string"
46 changes: 40 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::exports::edgee::protocols::data_collection::{Dict, EdgeeRequest, Event, HttpMethod};
use exports::edgee::protocols::data_collection::Guest;
use std::collections::HashMap;

wit_bindgen::generate!({world: "data-collection", path: "wit", generate_all});
export!(Component);
Expand All @@ -20,48 +21,81 @@ struct Component;

impl Guest for Component {
#[allow(unused_variables)]
fn page(edgee_event: Event, settings: Dict) -> Result<EdgeeRequest, String> {
fn page(edgee_event: Event, settings_dict: Dict) -> Result<EdgeeRequest, String> {
let settings = Settings::new(settings_dict).map_err(|e| e.to_string())?;
Ok(EdgeeRequest {
method: HttpMethod::Post,
url: format!("https://example.com/{}", "page"),
headers: vec![
("Content-Type".to_string(), "application/json".to_string()),
("Authorization".to_string(), "Bearer XYZ".to_string()),
],
body: String::new(),
body: settings.example,
forward_client_headers: true,
})
}

#[allow(unused_variables)]
fn track(edgee_event: Event, settings: Dict) -> Result<EdgeeRequest, String> {
fn track(edgee_event: Event, settings_dict: Dict) -> Result<EdgeeRequest, String> {
let settings = Settings::new(settings_dict).map_err(|e| e.to_string())?;
Ok(EdgeeRequest {
method: HttpMethod::Post,
url: format!("https://example.com/{}", "track"),
headers: vec![
("Content-Type".to_string(), "application/json".to_string()),
("Authorization".to_string(), "Bearer XYZ".to_string()),
],
body: String::new(),
body: settings.example,
forward_client_headers: true,
})
}

#[allow(unused_variables)]
fn user(edgee_event: Event, settings: Dict) -> Result<EdgeeRequest, String> {
fn user(edgee_event: Event, settings_dict: Dict) -> Result<EdgeeRequest, String> {
let settings = Settings::new(settings_dict).map_err(|e| e.to_string())?;
Ok(EdgeeRequest {
method: HttpMethod::Post,
url: format!("https://example.com/{}", "user"),
headers: vec![
("Content-Type".to_string(), "application/json".to_string()),
("Authorization".to_string(), "Bearer XYZ".to_string()),
],
body: String::new(),
body: settings.example,
forward_client_headers: true,
})
}
}

pub struct Settings {
pub example: String,
}

impl Settings {
pub fn new(settings_dict: Dict) -> anyhow::Result<Self> {
let settings_map: HashMap<String, String> = settings_dict
.iter()
.map(|(key, value)| (key.to_string(), value.to_string()))
.collect();

/*
// required setting
// also needs -> use anyhow::Context;
let example = settings_map
.get("example")
.context("Missing example setting")?
.to_string();
*/

// optional setting
let example = settings_map
.get("example")
.map(String::to_string)
.unwrap_or_default();

Ok(Self { example })
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down