Skip to content

Commit

Permalink
Rename manifest fields, add example setting (#5)
Browse files Browse the repository at this point in the history
* rename manifest fields, add example setting

* add settings utility

* fmt

* clippy
  • Loading branch information
alexcasalboni authored Feb 13, 2025
1 parent f3f0753 commit 813023c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 9 deletions.
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

0 comments on commit 813023c

Please sign in to comment.