diff options
Diffstat (limited to 'fluent-bit/examples/wasi_serde_json/src/main.rs')
-rw-r--r-- | fluent-bit/examples/wasi_serde_json/src/main.rs | 32 |
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 |