Skip to content

Commit

Permalink
Added support for app level settings
Browse files Browse the repository at this point in the history
Also added a check for init.lua to make sure it exists as a file.

Closes #241
  • Loading branch information
dariusc93 committed Jan 31, 2019
1 parent 53898f7 commit 54e21fd
Showing 1 changed file with 33 additions and 11 deletions.
44 changes: 33 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub struct AppState {
pub init_args: Vec<String>,
pub package_path: Option<String>,
pub settings: Value,
pub app_settings: Option<(String, Value)>,
}

impl AppState {
Expand Down Expand Up @@ -72,6 +73,14 @@ impl AppState {
lua.globals().set("torchbear", tb_table)?;
}

// app table

if let Some((name, app_settings)) = &self.app_settings {
let tb_table = lua.create_table()?;
tb_table.set("settings", rlua_serde::to_value(&lua, app_settings).map_err(LuaError::external)?)?;

This comment has been minimized.

Copy link
@naturallymitchell

naturallymitchell Jan 31, 2019

Member

How does this work?
Serde isn't integrated with scl, so how can this use rlua-serde?

This comment has been minimized.

Copy link
@dariusc93

dariusc93 Jan 31, 2019

Author Contributor

It works the same way that we are loading the config but with this I am just passing the app_settings along which is a type Value from serde_json and converting that to be used with rlua-serde.

lua.globals().set(name.as_str(), tb_table)?;
}

// Lua package.path
match self.package_path {
Some(ref package_path) => {
Expand Down Expand Up @@ -169,28 +178,40 @@ impl ApplicationBuilder {
None => ()
}

let scl_path = match &init_path {
Some(p) => p.parent().unwrap_or(Path::new(".")).join("torchbear.scl"),
None => PathBuf::from("torchbear.scl"),
fn get_or (map: &Value, key: &str, val: &str) -> String {
map.get(key).map(|s| String::from(s.as_str().unwrap_or(val)) ).unwrap_or(String::from(val))
}

let root_path = match &init_path {
Some(p) => p.parent().unwrap_or(Path::new(".")),
None => Path::new("."),
};

let setting_file = scl_path.as_path();
let config_path = root_path.join("torchbear.scl");

let setting_file = config_path.as_path();

let config = if setting_file.exists() {
conf::Conf::load_file(&setting_file)?
} else {
SettingConfig::default()
};

fn get_or (map: &Value, key: &str, val: &str) -> String {
map.get(key).map(|s| String::from(s.as_str().unwrap_or(val)) ).unwrap_or(String::from(val))
}

let general = config.general.unwrap_or_default();

let init_path = init_path.unwrap_or(Path::new(&get_or(&general, "init", "init.lua")).to_path_buf());
let app_config: Option<(String, Value)> = general.get("app-name").and_then(Value::as_str).map(PathBuf::from).and_then(|name| {

This comment has been minimized.

Copy link
@naturallymitchell

naturallymitchell Jan 31, 2019

Member

How does app-name work?

This comment has been minimized.

Copy link
@dariusc93

dariusc93 Jan 31, 2019

Author Contributor

If there is a field under the general section called app-name (might look better to name it as name or project?), it will check to see if there is a configuration file using said name. eg app-name = "test" would check for test.scl and if it exists it and the data is valid it can be used under test.settings.

let mut config_path = root_path.join(&name);
config_path.set_extension("scl");
if config_path.exists() && config_path.is_file() {
conf::Conf::load_file(&config_path).map(|s| (name.to_string_lossy().to_string(), s)).ok()
} else {
None
}
});

let init_path = init_path.unwrap_or(PathBuf::from(&get_or(&general, "init", "init.lua")));

if !init_path.exists() {
if !init_path.exists() || !init_path.is_file() {
println!("Error: Specified init.lua not found. You may have not completed installing your app");
std::process::exit(1);
}
Expand All @@ -206,7 +227,8 @@ impl ApplicationBuilder {
init_path: init_path,
init_args: init_args,
package_path: package_path,
settings: general
settings: general,
app_settings: app_config,
};

if let Some(web) = config.web_server {
Expand Down

0 comments on commit 54e21fd

Please sign in to comment.