Merge pull request #362 from hannobraun/config

Add configuration file
This commit is contained in:
Hanno Braun 2022-03-16 14:08:49 +01:00 committed by GitHub
commit f58dd35ef6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 135 additions and 5 deletions

85
Cargo.lock generated
View File

@ -83,6 +83,15 @@ dependencies = [
"libloading", "libloading",
] ]
[[package]]
name = "atomic"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b88d82667eca772c4aa12f0f1348b3ae643424c8876448f3f7bd5787032e234c"
dependencies = [
"autocfg",
]
[[package]] [[package]]
name = "atty" name = "atty"
version = "0.2.14" version = "0.2.14"
@ -517,6 +526,20 @@ version = "1.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457"
[[package]]
name = "figment"
version = "0.10.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "790b4292c72618abbab50f787a477014fe15634f96291de45672ce46afe122df"
dependencies = [
"atomic",
"pear",
"serde",
"toml",
"uncased",
"version_check",
]
[[package]] [[package]]
name = "filetime" name = "filetime"
version = "0.2.15" version = "0.2.15"
@ -542,6 +565,7 @@ dependencies = [
"bytemuck", "bytemuck",
"clap", "clap",
"decorum", "decorum",
"figment",
"fj", "fj",
"futures", "futures",
"libloading", "libloading",
@ -552,6 +576,7 @@ dependencies = [
"parking_lot 0.12.0", "parking_lot 0.12.0",
"parry2d-f64", "parry2d-f64",
"parry3d-f64", "parry3d-f64",
"serde",
"spade", "spade",
"thiserror", "thiserror",
"threemf", "threemf",
@ -849,6 +874,12 @@ dependencies = [
"hashbrown", "hashbrown",
] ]
[[package]]
name = "inlinable_string"
version = "0.1.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c8fae54786f62fb2918dcfae3d568594e50eb9b5c25bf04371af6fe7516452fb"
[[package]] [[package]]
name = "inotify" name = "inotify"
version = "0.9.6" version = "0.9.6"
@ -1476,6 +1507,29 @@ version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0744126afe1a6dd7f394cb50a716dbe086cb06e255e53d8d0185d82828358fb5" checksum = "0744126afe1a6dd7f394cb50a716dbe086cb06e255e53d8d0185d82828358fb5"
[[package]]
name = "pear"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "15e44241c5e4c868e3eaa78b7c1848cadd6344ed4f54d029832d32b415a58702"
dependencies = [
"inlinable_string",
"pear_codegen",
"yansi",
]
[[package]]
name = "pear_codegen"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "82a5ca643c2303ecb740d506539deba189e16f2754040a42901cd8105d0282d0"
dependencies = [
"proc-macro2",
"proc-macro2-diagnostics",
"quote",
"syn",
]
[[package]] [[package]]
name = "percent-encoding" name = "percent-encoding"
version = "2.1.0" version = "2.1.0"
@ -1549,6 +1603,19 @@ dependencies = [
"unicode-xid", "unicode-xid",
] ]
[[package]]
name = "proc-macro2-diagnostics"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4bf29726d67464d49fa6224a1d07936a8c08bb3fba727c7493f6cf1616fdaada"
dependencies = [
"proc-macro2",
"quote",
"syn",
"version_check",
"yansi",
]
[[package]] [[package]]
name = "profiling" name = "profiling"
version = "1.0.5" version = "1.0.5"
@ -1726,6 +1793,9 @@ name = "serde"
version = "1.0.136" version = "1.0.136"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce31e24b01e1e524df96f1c2fdd054405f8d7376249a5110886fb4b658484789" checksum = "ce31e24b01e1e524df96f1c2fdd054405f8d7376249a5110886fb4b658484789"
dependencies = [
"serde_derive",
]
[[package]] [[package]]
name = "serde_derive" name = "serde_derive"
@ -2003,6 +2073,15 @@ version = "1.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcf81ac59edc17cc8697ff311e8f5ef2d99fcbd9817b34cec66f90b6c3dfd987" checksum = "dcf81ac59edc17cc8697ff311e8f5ef2d99fcbd9817b34cec66f90b6c3dfd987"
[[package]]
name = "uncased"
version = "0.9.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5baeed7327e25054889b9bd4f975f32e5f4c5d434042d59ab6cd4142c0a76ed0"
dependencies = [
"version_check",
]
[[package]] [[package]]
name = "unicode-width" name = "unicode-width"
version = "0.1.9" version = "0.1.9"
@ -2451,6 +2530,12 @@ version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2d7d3948613f75c98fd9328cfdcc45acc4d360655289d0a7d4ec931392200a3" checksum = "d2d7d3948613f75c98fd9328cfdcc45acc4d360655289d0a7d4ec931392200a3"
[[package]]
name = "yansi"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9fc79f4a1e39857fc00c3f662cbf2651c771f00e9c15fe2abc341806bd46bd71"
[[package]] [[package]]
name = "zip" name = "zip"
version = "0.5.13" version = "0.5.13"

View File

@ -37,10 +37,18 @@ winit = "0.26.1"
version = "3.1.6" version = "3.1.6"
features = ["derive"] features = ["derive"]
[dependencies.figment]
version = "0.10.6"
features = ["env", "toml"]
[dependencies.fj] [dependencies.fj]
version = "0.5.0" version = "0.5.0"
path = "fj" path = "fj"
[dependencies.serde]
version = "1.0.136"
features = ["derive"]
[dependencies.tracing-subscriber] [dependencies.tracing-subscriber]
version = "0.3.9" version = "0.3.9"
features = ["env-filter", "fmt"] features = ["env-filter", "fmt"]

7
fj.toml Normal file
View File

@ -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"

View File

@ -4,8 +4,8 @@ use std::path::PathBuf;
#[derive(clap::Parser)] #[derive(clap::Parser)]
pub struct Args { pub struct Args {
/// The model to open /// The model to open
#[clap(short, long, default_value = "cuboid")] #[clap(short, long)]
pub model: PathBuf, pub model: Option<PathBuf>,
/// Export model to this path /// Export model to this path
#[clap(short, long)] #[clap(short, long)]

24
src/config.rs Normal file
View File

@ -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")
}
}

View File

@ -1,5 +1,6 @@
mod args; mod args;
mod camera; mod camera;
mod config;
mod debug; mod debug;
mod graphics; mod graphics;
mod input; mod input;
@ -27,6 +28,7 @@ use crate::math::Scalar;
use crate::{ use crate::{
args::Args, args::Args,
camera::Camera, camera::Camera,
config::Config,
debug::DebugInfo, debug::DebugInfo,
graphics::{DrawConfig, Renderer}, graphics::{DrawConfig, Renderer},
kernel::shapes::ToShape as _, kernel::shapes::ToShape as _,
@ -50,7 +52,11 @@ fn main() -> anyhow::Result<()> {
.init(); .init();
let args = Args::parse(); 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(); let mut parameters = HashMap::new();
for parameter in args.parameters { for parameter in args.parameters {

View File

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