From df6bad85fa0187453a99af655dd1bcea2728530c Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 16 Mar 2022 12:41:52 +0100 Subject: [PATCH 1/6] Add configuration file It is just a placeholder, and not used yet. --- fj.toml | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 fj.toml diff --git a/fj.toml b/fj.toml new file mode 100644 index 000000000..bfbf4af0a --- /dev/null +++ b/fj.toml @@ -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" From 6a8a645910d3fd8d5380ca9d40f5ec06a99c265a Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 16 Mar 2022 12:45:58 +0100 Subject: [PATCH 2/6] Add dependency on Figment --- Cargo.lock | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 4 +++ 2 files changed, 85 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index dc9264bd7..861043155 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -83,6 +83,15 @@ dependencies = [ "libloading", ] +[[package]] +name = "atomic" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b88d82667eca772c4aa12f0f1348b3ae643424c8876448f3f7bd5787032e234c" +dependencies = [ + "autocfg", +] + [[package]] name = "atty" version = "0.2.14" @@ -517,6 +526,20 @@ version = "1.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" 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]] name = "filetime" version = "0.2.15" @@ -542,6 +565,7 @@ dependencies = [ "bytemuck", "clap", "decorum", + "figment", "fj", "futures", "libloading", @@ -849,6 +873,12 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "inlinable_string" +version = "0.1.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8fae54786f62fb2918dcfae3d568594e50eb9b5c25bf04371af6fe7516452fb" + [[package]] name = "inotify" version = "0.9.6" @@ -1476,6 +1506,29 @@ version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" 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]] name = "percent-encoding" version = "2.1.0" @@ -1549,6 +1602,19 @@ dependencies = [ "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]] name = "profiling" version = "1.0.5" @@ -2003,6 +2069,15 @@ version = "1.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" 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]] name = "unicode-width" version = "0.1.9" @@ -2451,6 +2526,12 @@ version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d2d7d3948613f75c98fd9328cfdcc45acc4d360655289d0a7d4ec931392200a3" +[[package]] +name = "yansi" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fc79f4a1e39857fc00c3f662cbf2651c771f00e9c15fe2abc341806bd46bd71" + [[package]] name = "zip" version = "0.5.13" diff --git a/Cargo.toml b/Cargo.toml index 7832f3e2a..99001093b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -37,6 +37,10 @@ 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" From 9a9c7844efe5c5eeadf75e6b92729fffcff89766 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 16 Mar 2022 12:54:01 +0100 Subject: [PATCH 3/6] Add dependency on Serde --- Cargo.lock | 4 ++++ Cargo.toml | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 861043155..26488fdff 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -576,6 +576,7 @@ dependencies = [ "parking_lot 0.12.0", "parry2d-f64", "parry3d-f64", + "serde", "spade", "thiserror", "threemf", @@ -1792,6 +1793,9 @@ name = "serde" version = "1.0.136" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ce31e24b01e1e524df96f1c2fdd054405f8d7376249a5110886fb4b658484789" +dependencies = [ + "serde_derive", +] [[package]] name = "serde_derive" diff --git a/Cargo.toml b/Cargo.toml index 99001093b..b97c17457 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -45,6 +45,10 @@ features = ["env", "toml"] 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"] From 0f7350771dbdb7371ead3a0f7d283d9dc8404e44 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 16 Mar 2022 13:01:56 +0100 Subject: [PATCH 4/6] Load configuration file Nothing is done with the configuration yet, except log it. --- src/config.rs | 24 ++++++++++++++++++++++++ src/main.rs | 7 ++++++- 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 src/config.rs diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 000000000..8fecef0b0 --- /dev/null +++ b/src/config.rs @@ -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 { + Figment::new() + .merge(Toml::file("fj.toml")) + .merge(Env::prefixed("FJ_")) + .extract() + .context("Error loading configuration") + } +} diff --git a/src/main.rs b/src/main.rs index be7d6ea10..9ae00135f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ mod args; mod camera; +mod config; mod debug; mod graphics; mod input; @@ -15,7 +16,7 @@ use std::{collections::HashMap, sync::mpsc, time::Instant}; use futures::executor::block_on; use notify::Watcher as _; -use tracing::trace; +use tracing::{debug, trace}; use tracing_subscriber::fmt::format; use tracing_subscriber::EnvFilter; use winit::{ @@ -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 _, @@ -50,8 +52,11 @@ fn main() -> anyhow::Result<()> { .init(); let args = Args::parse(); + let config = Config::load()?; let model = Model::new(args.model); + debug!("Configuration: {:?}", config); + let mut parameters = HashMap::new(); for parameter in args.parameters { let mut parameter = parameter.splitn(2, '='); From 0c3380baad7c51ab3abd09811ab40be0a056da31 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 16 Mar 2022 13:55:58 +0100 Subject: [PATCH 5/6] Use `default_model`, if no model is specified --- src/args.rs | 4 ++-- src/main.rs | 6 ++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/args.rs b/src/args.rs index d1669cbd1..f47d14120 100644 --- a/src/args.rs +++ b/src/args.rs @@ -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, /// Export model to this path #[clap(short, long)] diff --git a/src/main.rs b/src/main.rs index 9ae00135f..45433ae73 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,7 +16,7 @@ use std::{collections::HashMap, sync::mpsc, time::Instant}; use futures::executor::block_on; use notify::Watcher as _; -use tracing::{debug, trace}; +use tracing::trace; use tracing_subscriber::fmt::format; use tracing_subscriber::EnvFilter; use winit::{ @@ -53,9 +53,7 @@ fn main() -> anyhow::Result<()> { let args = Args::parse(); let config = Config::load()?; - let model = Model::new(args.model); - - debug!("Configuration: {:?}", config); + let model = Model::new(args.model.unwrap_or(config.default_model)); let mut parameters = HashMap::new(); for parameter in args.parameters { From 71d25877d836f4cd5eabb5201de3a88cfc4e2875 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 16 Mar 2022 14:01:45 +0100 Subject: [PATCH 6/6] Use `default_path` from configuration --- src/main.rs | 5 ++++- src/model.rs | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 45433ae73..921c58135 100644 --- a/src/main.rs +++ b/src/main.rs @@ -53,7 +53,10 @@ fn main() -> anyhow::Result<()> { let args = Args::parse(); let config = Config::load()?; - let model = Model::new(args.model.unwrap_or(config.default_model)); + let model = Model::new( + config.default_path, + args.model.unwrap_or(config.default_model), + ); let mut parameters = HashMap::new(); for parameter in args.parameters { diff --git a/src/model.rs b/src/model.rs index a3bea9c30..76620bebe 100644 --- a/src/model.rs +++ b/src/model.rs @@ -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 }