Skip to content

Commit

Permalink
Merge pull request #362 from hannobraun/config
Browse files Browse the repository at this point in the history
Add configuration file
  • Loading branch information
hannobraun authored Mar 16, 2022
2 parents 0d59d0f + 71d2587 commit f58dd35
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 5 deletions.
85 changes: 85 additions & 0 deletions Cargo.lock

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

8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,18 @@ winit = "0.26.1"
version = "3.1.6"
features = ["derive"]

[dependencies.figment]
version = "0.10.6"
features = ["env", "toml"]

[dependencies.fj]
version = "0.5.0"
path = "fj"

[dependencies.serde]
version = "1.0.136"
features = ["derive"]

[dependencies.tracing-subscriber]
version = "0.3.9"
features = ["env-filter", "fmt"]
7 changes: 7 additions & 0 deletions fj.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# The default path that models are loaded from. If the `--model` argument is a
# relative path, it is assumed to be relative to `default_path`.
default_path = "models"

# The default models that is loaded, if none is specified. If this is a relative
# path, it should be relative to `default_path`.
default_model = "star"
4 changes: 2 additions & 2 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use std::path::PathBuf;
#[derive(clap::Parser)]
pub struct Args {
/// The model to open
#[clap(short, long, default_value = "cuboid")]
pub model: PathBuf,
#[clap(short, long)]
pub model: Option<PathBuf>,

/// Export model to this path
#[clap(short, long)]
Expand Down
24 changes: 24 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use std::path::PathBuf;

use anyhow::Context as _;
use figment::{
providers::{Env, Format as _, Toml},
Figment,
};
use serde::Deserialize;

#[derive(Debug, Deserialize)]
pub struct Config {
pub default_path: PathBuf,
pub default_model: PathBuf,
}

impl Config {
pub fn load() -> Result<Self, anyhow::Error> {
Figment::new()
.merge(Toml::file("fj.toml"))
.merge(Env::prefixed("FJ_"))
.extract()
.context("Error loading configuration")
}
}
8 changes: 7 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod args;
mod camera;
mod config;
mod debug;
mod graphics;
mod input;
Expand Down Expand Up @@ -27,6 +28,7 @@ use crate::math::Scalar;
use crate::{
args::Args,
camera::Camera,
config::Config,
debug::DebugInfo,
graphics::{DrawConfig, Renderer},
kernel::shapes::ToShape as _,
Expand All @@ -50,7 +52,11 @@ fn main() -> anyhow::Result<()> {
.init();

let args = Args::parse();
let model = Model::new(args.model);
let config = Config::load()?;
let model = Model::new(
config.default_path,
args.model.unwrap_or(config.default_model),
);

let mut parameters = HashMap::new();
for parameter in args.parameters {
Expand Down
4 changes: 2 additions & 2 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ pub struct Model {
}

impl Model {
pub fn new(rel_path: PathBuf) -> Self {
let mut path = PathBuf::from("models");
pub fn new(base_path: PathBuf, rel_path: PathBuf) -> Self {
let mut path = base_path;
path.push(rel_path);

Self { path }
Expand Down

0 comments on commit f58dd35

Please sign in to comment.