| 1 | use serde::Deserialize; |
| 2 | |
| 3 | use super::IgnoreList; |
| 4 | use crate::{canvas::components::data_table::SortOrder, options::DiskWidgetColumn}; |
| 5 | |
| 6 | /// Disk configuration. |
| 7 | #[derive(Clone, Debug, Default, Deserialize)] |
| 8 | #[cfg_attr(feature = "generate_schema", derive(schemars::JsonSchema))] |
| 9 | #[cfg_attr(test, serde(deny_unknown_fields), derive(PartialEq, Eq))] |
| 10 | pub(crate) struct DiskConfig { |
| 11 | /// A filter over the disk names. |
| 12 | pub(crate) name_filter: Option<IgnoreList>, |
| 13 | |
| 14 | /// A filter over the mount names. |
| 15 | pub(crate) mount_filter: Option<IgnoreList>, |
| 16 | |
| 17 | /// Whether to include block devices that aren't currently mounted (currently Linux only). Defaults to false. |
| 18 | pub(crate) include_unmounted: Option<bool>, |
| 19 | |
| 20 | /// A list of disk widget columns. |
| 21 | // TODO: make this more composable(?) in the future, we might need to |
| 22 | // rethink how it's done for custom widgets. |
| 23 | #[serde(default)] |
| 24 | pub(crate) columns: Option<Vec<DiskWidgetColumn>>, |
| 25 | |
| 26 | /// The default sort column. |
| 27 | #[serde(default)] |
| 28 | pub(crate) default_sort: Option<DiskWidgetColumn>, |
| 29 | |
| 30 | /// The default sort order. Defaults to ascending. |
| 31 | #[serde(default)] |
| 32 | pub(crate) sort_order: SortOrder, |
| 33 | } |
| 34 | |
| 35 | #[cfg(test)] |
| 36 | mod test { |
| 37 | use super::DiskConfig; |
| 38 | |
| 39 | #[test] |
| 40 | fn none_column_setting() { |
| 41 | let config = ""; |
| 42 | let generated: DiskConfig = toml_edit::de::from_str(config).unwrap(); |
| 43 | assert!(generated.columns.is_none()); |
| 44 | } |
| 45 | |
| 46 | #[test] |
| 47 | fn empty_column_setting() { |
| 48 | let config = r#"columns = []"#; |
| 49 | let generated: DiskConfig = toml_edit::de::from_str(config).unwrap(); |
| 50 | assert!(generated.columns.unwrap().is_empty()); |
| 51 | } |
| 52 | |
| 53 | #[test] |
| 54 | fn valid_disk_column_settings() { |
| 55 | let config = r#"columns = ["disk", "mount", "used", "free", "total", "used%", "free%", "r/s", "w/s"]"#; |
| 56 | toml_edit::de::from_str::<DiskConfig>(config).expect("Should succeed!"); |
| 57 | } |
| 58 | |
| 59 | #[test] |
| 60 | fn bad_disk_column_settings() { |
| 61 | let config = r#"columns = ["diskk"]"#; |
| 62 | toml_edit::de::from_str::<DiskConfig>(config).expect_err("Should error out!"); |
| 63 | } |
| 64 | |
| 65 | /// Test that disk enum variants that are advertised in the schema are valid. |
| 66 | #[cfg(feature = "generate_schema")] |
| 67 | #[test] |
| 68 | fn ensure_disk_column_schema_is_accepted() { |
| 69 | use strum::VariantArray; |
| 70 | |
| 71 | use crate::options::{Config, DiskWidgetColumn}; |
| 72 | |
| 73 | for column in DiskWidgetColumn::VARIANTS { |
| 74 | for &name in column.get_schema_names() { |
| 75 | let config = format!("[disk]\ncolumns = [\"{name}\"]\n"); |
| 76 | toml_edit::de::from_str::<Config>(&config).unwrap_or_else(|e| { |
| 77 | panic!("schema name {name:?} was rejected:\n{e}\nconfig was:\n{config}") |
| 78 | }); |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | } |