| 1 | pub mod cpu; |
| 2 | pub mod disk; |
| 3 | pub mod disk_io_graph; |
| 4 | pub mod flags; |
| 5 | mod ignore_list; |
| 6 | pub mod layout; |
| 7 | pub mod memory_graph; |
| 8 | pub mod network_graph; |
| 9 | pub mod process; |
| 10 | pub mod style; |
| 11 | pub mod temperature; |
| 12 | pub mod temperature_graph; |
| 13 | |
| 14 | use disk::DiskConfig; |
| 15 | use disk_io_graph::DiskIoGraphConfig; |
| 16 | use flags::GeneralConfig; |
| 17 | use network_graph::NetworkGraphConfig; |
| 18 | use serde::{Deserialize, Serialize}; |
| 19 | use style::StyleConfig; |
| 20 | use temperature::TempConfig; |
| 21 | use temperature_graph::TempGraphConfig; |
| 22 | |
| 23 | pub use self::ignore_list::IgnoreList; |
| 24 | use self::{cpu::CpuConfig, layout::Row, process::ProcessesConfig}; |
| 25 | use crate::options::config::memory_graph::MemoryGraphConfig; |
| 26 | |
| 27 | /// Overall config for `bottom`. |
| 28 | #[derive(Clone, Debug, Default, Deserialize)] |
| 29 | #[cfg_attr(feature = "generate_schema", derive(schemars::JsonSchema))] |
| 30 | #[cfg_attr(test, serde(deny_unknown_fields), derive(PartialEq))] |
| 31 | pub struct Config { |
| 32 | pub(crate) flags: Option<GeneralConfig>, |
| 33 | pub(crate) styles: Option<StyleConfig>, |
| 34 | pub(crate) row: Option<Vec<Row>>, |
| 35 | pub(crate) processes: Option<ProcessesConfig>, |
| 36 | pub(crate) disk: Option<DiskConfig>, |
| 37 | pub(crate) disk_io_graph: Option<DiskIoGraphConfig>, |
| 38 | pub(crate) temperature: Option<TempConfig>, |
| 39 | pub(crate) temperature_graph: Option<TempGraphConfig>, |
| 40 | #[serde(alias = "network")] |
| 41 | pub(crate) network_graph: Option<NetworkGraphConfig>, |
| 42 | #[serde(alias = "memory")] |
| 43 | pub(crate) memory_graph: Option<MemoryGraphConfig>, |
| 44 | pub(crate) cpu: Option<CpuConfig>, |
| 45 | } |
| 46 | |
| 47 | #[derive(Clone, Debug, Deserialize, Serialize)] |
| 48 | #[serde(untagged)] |
| 49 | #[cfg_attr(feature = "generate_schema", derive(schemars::JsonSchema))] |
| 50 | #[cfg_attr(test, derive(PartialEq, Eq))] |
| 51 | pub(crate) enum StringOrNum { |
| 52 | String(String), |
| 53 | Num(u64), |
| 54 | } |
| 55 | |
| 56 | impl From<String> for StringOrNum { |
| 57 | fn from(value: String) -> Self { |
| 58 | StringOrNum::String(value) |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | impl From<u64> for StringOrNum { |
| 63 | fn from(value: u64) -> Self { |
| 64 | StringOrNum::Num(value) |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | // I didn't do an invalid config test as a lot of them _are_ valid Config |
| 69 | // when parsed, but fail other checks. |
| 70 | #[cfg(test)] |
| 71 | mod test { |
| 72 | |
| 73 | // Test all valid configs in the integration test folder and ensure they are |
| 74 | // accepted. We need this separated as only test library code sets |
| 75 | // `serde(deny_unknown_fields)`. |
| 76 | #[test] |
| 77 | #[cfg(feature = "default")] |
| 78 | fn test_integration_valid_configs() { |
| 79 | use std::fs; |
| 80 | |
| 81 | use super::Config; |
| 82 | |
| 83 | for config_path in fs::read_dir("./tests/valid_configs").unwrap() { |
| 84 | let dir_entry = config_path.unwrap(); |
| 85 | let path = dir_entry.path(); |
| 86 | |
| 87 | if path.is_file() { |
| 88 | let config_path_str = path.display().to_string(); |
| 89 | let config_str = fs::read_to_string(path).unwrap(); |
| 90 | |
| 91 | toml_edit::de::from_str::<Config>(&config_str) |
| 92 | .unwrap_or_else(|_| panic!("incorrectly rejected '{config_path_str}'")); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | } |