libredr_common/
lib.rs

1//! LibreDR is an open-source ray-tracing differentiable renderer
2#![warn(missing_docs)]
3#![warn(missing_debug_implementations)]
4
5use std::path::Path;
6use std::collections::HashMap;
7use std::collections::hash_map::Entry;
8use configparser::ini::Ini;
9use anyhow::{Error, Result};
10#[cfg(all(not(target_env = "msvc"), feature = "jemalloc"))]
11use tikv_jemallocator::Jemalloc;
12
13/// Constants to configure `Render`
14pub mod render;
15/// `Message` type shared by Client, Server, and Worker
16pub mod message;
17/// `Connection` type shared by Client, Server, and Worker
18pub mod connection;
19/// `Geometry` type shared by Client, Server, and Worker
20pub mod geometry;
21
22#[cfg(all(not(target_env = "msvc"), feature = "jemalloc"))]
23#[global_allocator]
24static GLOBAL: Jemalloc = Jemalloc;
25
26/// Global allocator for display
27pub const ALLOCATOR: &str = if cfg!(all(not(target_env = "msvc"), feature = "jemalloc")) {
28  "jemalloc"
29} else {
30  "default"
31};
32
33/// `CLAP_LONG_VERSION` for display
34pub const CLAP_LONG_VERSION: &str = const_format::concatcp!(
35  "LibreDR ", self::connection::build::CLAP_LONG_VERSION,
36  "\nallocator:", ALLOCATOR);
37
38/// Load an ini config file and merge it to the current config HashMap
39pub fn add_config(config: &mut HashMap<String, HashMap<String, String>>,
40    new_config_file: &Path) -> Result<()> {
41  let new_config = Ini::new()
42    .load(new_config_file)
43    .map_err(|err| format!("add_config: Error loading `{}`: {err}", new_config_file.display()))
44    .map_err(Error::msg)?;
45  for (section_key, new_section) in new_config {
46    let Entry::Occupied(mut section) = config.entry(section_key.to_owned()) else {
47      eprintln!("add_config: Warning: unexpected section `{section_key}`");
48      continue;
49    };
50    let section = section.get_mut();
51    for (entry_key, value) in new_section {
52      let Some(value) = value else {
53        continue;
54      };
55      match section.entry(entry_key) {
56        Entry::Occupied(mut entry) => entry.insert(value),
57        Entry::Vacant(vacant) => {
58          let entry_key = vacant.into_key();
59          eprintln!("add_config: Warning: unexpected entry `{entry_key}` in section `{section_key}`");
60          continue;
61        },
62      };
63    }
64  }
65  Ok(())
66}