summaryrefslogtreecommitdiffstats
path: root/fluent-bit/examples/wasi_serde_json/src/main.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-03-09 13:19:48 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-03-09 13:20:02 +0000
commit58daab21cd043e1dc37024a7f99b396788372918 (patch)
tree96771e43bb69f7c1c2b0b4f7374cb74d7866d0cb /fluent-bit/examples/wasi_serde_json/src/main.rs
parentReleasing debian version 1.43.2-1. (diff)
downloadnetdata-58daab21cd043e1dc37024a7f99b396788372918.tar.xz
netdata-58daab21cd043e1dc37024a7f99b396788372918.zip
Merging upstream version 1.44.3.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'fluent-bit/examples/wasi_serde_json/src/main.rs')
-rw-r--r--fluent-bit/examples/wasi_serde_json/src/main.rs32
1 files changed, 32 insertions, 0 deletions
diff --git a/fluent-bit/examples/wasi_serde_json/src/main.rs b/fluent-bit/examples/wasi_serde_json/src/main.rs
new file mode 100644
index 000000000..29ee25f4f
--- /dev/null
+++ b/fluent-bit/examples/wasi_serde_json/src/main.rs
@@ -0,0 +1,32 @@
+// Import rust's io and filesystem module
+use std::io::prelude::*;
+use std::fs;
+// Import pure and fast JSON library written in Rust
+use serde_json::json;
+// Import chrono library to handle time related operation conveniently
+use chrono::Utc;
+
+// Entry point to our WASI applications
+fn main() {
+ // Note that fractional second must be handled by `.%9f` not `%L` like as fluent-bit.
+ let time = Utc::now().format("%Y-%m-%dT%H:%M:%S.%9f %z").to_string();
+ // The type of `john` is `serde_json::Value`
+ let john = json!({
+ "name": "John Doe",
+ "age": 43,
+ "phones": [
+ "+44 1234567",
+ "+44 2345678"
+ ],
+ "time": format!("{}", time),
+ });
+ // Print out serialized JSON data for john
+ // This will handle writing to stdout for us using the WASI APIs (e.g fd_write)
+ println!("{}", john.to_string());
+
+ // Create a file (Testing for exposed wasi API)
+ let mut file = fs::File::create("helloworld.txt").expect("Unable to create file");
+
+ // Write the text to the file we created (Testing for exposed wasi API)
+ write!(file, "Hello world!\n").unwrap();
+} \ No newline at end of file