summaryrefslogtreecommitdiffstats
path: root/fluent-bit/examples/wasi_serde_json/src/main.rs
blob: 29ee25f4f3c870b8f90feb6eeea91a25de0739f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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();
}