-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
82386ec
commit 6e608c3
Showing
7 changed files
with
311 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
target | ||
node_modules | ||
dist |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |