Skip to content

Commit

Permalink
feat: add rust client example
Browse files Browse the repository at this point in the history
  • Loading branch information
lbennett-stacki committed Aug 20, 2024
1 parent 82386ec commit 6e608c3
Show file tree
Hide file tree
Showing 7 changed files with 311 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
target
node_modules
dist
166 changes: 166 additions & 0 deletions clients/rust/Cargo.lock

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

7 changes: 7 additions & 0 deletions clients/rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "envy-client"
version = "0.1.0"
edition = "2021"

[dependencies]
tokio = { version = "1.39.3", features = ["fs", "macros", "rt-multi-thread"] }
83 changes: 83 additions & 0 deletions clients/rust/src/gen.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
use std::{collections::HashMap, error::Error};

#[derive(Debug)]
enum ConfigValue {
String(String),
Integer(i32),
Float(f64),
}

impl ConfigValue {
fn as_string(&self) -> Option<&String> {
if let ConfigValue::String(ref s) = self {
Some(s)
} else {
None
}
}

fn as_integer(&self) -> Option<i32> {
if let ConfigValue::Integer(i) = self {
Some(*i)
} else {
None
}
}

fn as_float(&self) -> Option<f64> {
if let ConfigValue::Float(f) = self {
Some(*f)
} else {
None
}
}
}

fn parse_string(value: &str) -> ConfigValue {
ConfigValue::String(value.to_string())
}

fn parse_integer(value: &str) -> ConfigValue {
ConfigValue::Integer(value.parse::<i32>().expect("Failed to parse integer"))
}

fn parse_float(value: &str) -> ConfigValue {
ConfigValue::Float(value.parse::<f64>().expect("Failed to parse float"))
}

#[derive(Debug)]
pub struct Config {
pub my_cool_var: String,
pub my_other_var: i32,
}

pub fn parse_config(content: &HashMap<String, String>) -> Result<Config, Box<dyn Error>> {
let parser_config: HashMap<&str, &str> =
vec![("my_cool_var", "string"), ("my_other_var", "integer")]
.into_iter()
.collect();

let parsers: HashMap<&str, fn(&str) -> ConfigValue> = vec![
("string", parse_string as fn(&str) -> ConfigValue),
("integer", parse_integer as fn(&str) -> ConfigValue),
("float", parse_float as fn(&str) -> ConfigValue),
]
.into_iter()
.collect();

let parsed_config: Config = Config {
my_cool_var: parsers
.get(parser_config.get("my_cool_var").unwrap())
.unwrap()(content.get("my_cool_var").unwrap())
.as_string()
.unwrap()
.clone(),
my_other_var: parsers
.get(parser_config.get("my_other_var").unwrap())
.unwrap()(content.get("my_other_var").unwrap())
.as_integer()
.unwrap(),
};

Ok(parsed_config)
}
49 changes: 49 additions & 0 deletions clients/rust/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
mod gen;

use r#gen::{parse_config, Config};
use std::collections::HashMap;
use tokio::fs;

async fn load_config(path_input: Option<&str>) -> Result<String, Box<dyn std::error::Error>> {
let path = path_input.unwrap_or(".nv/config.nvx");
let contents = fs::read_to_string(path).await?;
Ok(contents)
}

async fn test() -> Result<(), Box<dyn std::error::Error>> {
let config_name = "../../lang/packages/cli/.nv/simple-vars.nvx";
let result = load_config(Some(config_name)).await?;

let mut config_map: HashMap<String, String> = HashMap::new();
for line in result.lines() {
if let Some((key, value)) = line.split_once(':') {
config_map.insert(key.trim().to_string(), value.trim().to_string());
}
}

let parsed_config: Config = parse_config(&config_map).unwrap();

println!(
"my_cool_var: {}\nmy_other_var: {}",
config_map
.get("my_cool_var")
.unwrap_or(&"Not found".to_string()),
config_map
.get("my_other_var")
.unwrap_or(&"Not found".to_string())
);

println!(
"Parsed Config: {:?}\nmy_cool_var: {}\nmy_other_var: {}",
parsed_config, parsed_config.my_cool_var, parsed_config.my_other_var
);

Ok(())
}

#[tokio::main]
async fn main() {
if let Err(e) = test().await {
eprintln!("Error: {}", e);
}
}
2 changes: 1 addition & 1 deletion clients/typescript/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@envyhq/typescript",
"name": "@envyhq/client",
"version": "1.0.0",
"description": "Envy TypeScript client",
"main": "dist/index.js",
Expand Down
4 changes: 2 additions & 2 deletions lang/packages/cli/.nv/simple-vars.nvx
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
my_cool_var:wow
my_other_var:1234
my_cool_var:woweeee
my_other_var:12345

0 comments on commit 6e608c3

Please sign in to comment.