| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
use synckit_config::{ConfigSpec, Posture}; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
macro_rules! config_keys { |
| 40 |
($( $(#[$m:meta])* $variant:ident => $key:literal : $posture:ident ),+ $(,)?) => { |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 45 |
pub enum ConfigKey { |
| 46 |
$( $(#[$m])* $variant ),+ |
| 47 |
} |
| 48 |
|
| 49 |
impl ConfigKey { |
| 50 |
|
| 51 |
pub const ALL: &'static [ConfigKey] = &[ $( ConfigKey::$variant ),+ ]; |
| 52 |
|
| 53 |
/// The on-disk `user_config.key` string. |
| 54 |
#[must_use] |
| 55 |
pub const fn as_str(self) -> &'static str { |
| 56 |
match self { $( ConfigKey::$variant => $key ),+ } |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
#[must_use] |
| 61 |
pub const fn posture(self) -> Posture { |
| 62 |
match self { $( ConfigKey::$variant => Posture::$posture ),+ } |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
#[must_use] |
| 68 |
pub fn from_key(key: &str) -> Option<ConfigKey> { |
| 69 |
match key { |
| 70 |
$( $key => Some(ConfigKey::$variant), )+ |
| 71 |
_ => None, |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
pub const CONFIG: ConfigSpec = |
| 84 |
ConfigSpec::new("user_config", &[ $( ($key, Posture::$posture) ),+ ]); |
| 85 |
}; |
| 86 |
} |
| 87 |
|
| 88 |
config_keys! { |
| 89 |
|
| 90 |
Theme => "theme": Synced, |
| 91 |
PreviewLoop => "preview_loop": Synced, |
| 92 |
PreviewAutoplay => "preview_autoplay": Synced, |
| 93 |
EditResultMode => "edit_result_mode": Synced, |
| 94 |
ForgeAutoTrimOvershoot => "forge.auto_trim_overshoot": Synced, |
| 95 |
CurrentVfsId => "current_vfs_id": Synced, |
| 96 |
SidebarVisible => "sidebar_visible": Synced, |
| 97 |
DetailVisible => "detail_visible": Synced, |
| 98 |
FilterPanelOpen => "filter_panel_open": Synced, |
| 99 |
VfsExplained => "vfs_explained": Synced, |
| 100 |
HintsDismissed => "hints_dismissed": Synced, |
| 101 |
SyncIntroDismissed => "sync_intro_dismissed": Synced, |
| 102 |
RowHeight => "row_height": Synced, |
| 103 |
SuggestionsDismissed => "suggestions.dismissed": Synced, |
| 104 |
ColumnConfig => "column_config": Synced, |
| 105 |
SampleTombstoneRetainDays => "sample_tombstone_retain_days": Synced, |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
LooseFiles => "loose_files": Local, |
| 110 |
|
| 111 |
UnsafeMode => "unsafe_mode": Local, |
| 112 |
|
| 113 |
MirrorEnabled => "mirror_enabled": Local, |
| 114 |
|
| 115 |
MirrorPath => "mirror_path": Local, |
| 116 |
|
| 117 |
ImportPreflightDisabled => "import_preflight_disabled": Local, |
| 118 |
} |
| 119 |
|
| 120 |
impl ConfigKey { |
| 121 |
|
| 122 |
#[must_use] |
| 123 |
pub const fn is_excluded_from_sync(self) -> bool { |
| 124 |
!self.posture().replicated() |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
#[must_use] |
| 133 |
pub fn key_excluded_from_sync(key: &str) -> bool { |
| 134 |
match ConfigKey::from_key(key) { |
| 135 |
Some(k) => k.is_excluded_from_sync(), |
| 136 |
None => true, |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
#[cfg(test)] |
| 141 |
mod tests { |
| 142 |
use super::*; |
| 143 |
|
| 144 |
#[test] |
| 145 |
fn as_str_round_trips_through_from_key() { |
| 146 |
for &k in ConfigKey::ALL { |
| 147 |
assert_eq!(ConfigKey::from_key(k.as_str()), Some(k), "{k:?}"); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
#[test] |
| 152 |
fn key_strings_are_unique() { |
| 153 |
let mut seen = std::collections::HashSet::new(); |
| 154 |
for &k in ConfigKey::ALL { |
| 155 |
assert!( |
| 156 |
seen.insert(k.as_str()), |
| 157 |
"duplicate key string {:?}", |
| 158 |
k.as_str() |
| 159 |
); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
#[test] |
| 164 |
fn device_local_keys_are_excluded_replicated_are_not() { |
| 165 |
assert!(key_excluded_from_sync("loose_files")); |
| 166 |
assert!(key_excluded_from_sync("mirror_path")); |
| 167 |
assert!(key_excluded_from_sync("mirror_enabled")); |
| 168 |
assert!(key_excluded_from_sync("import_preflight_disabled")); |
| 169 |
assert!(key_excluded_from_sync("unsafe_mode")); |
| 170 |
|
| 171 |
assert!(!key_excluded_from_sync("theme")); |
| 172 |
assert!(!key_excluded_from_sync("sample_tombstone_retain_days")); |
| 173 |
} |
| 174 |
|
| 175 |
#[test] |
| 176 |
fn unknown_keys_fail_closed() { |
| 177 |
assert!(key_excluded_from_sync("sync_cursor")); |
| 178 |
assert!(key_excluded_from_sync("totally_unknown_key")); |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
#[test] |
| 187 |
fn the_spec_matches_the_registry() { |
| 188 |
let spec_keys: Vec<&str> = CONFIG.keys().map(|(k, _)| k).collect(); |
| 189 |
let registry_keys: Vec<&str> = ConfigKey::ALL.iter().map(|k| k.as_str()).collect(); |
| 190 |
assert_eq!(spec_keys, registry_keys, "same keys, same order"); |
| 191 |
|
| 192 |
for &key in ConfigKey::ALL { |
| 193 |
assert_eq!( |
| 194 |
CONFIG.is_synced(key.as_str()), |
| 195 |
!key.is_excluded_from_sync(), |
| 196 |
"posture disagreement for {key:?}", |
| 197 |
); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
#[test] |
| 204 |
fn postures_map_to_the_shared_vocabulary() { |
| 205 |
assert_eq!(ConfigKey::Theme.posture(), Posture::Synced); |
| 206 |
assert_eq!(ConfigKey::MirrorPath.posture(), Posture::Local); |
| 207 |
assert!(ConfigKey::MirrorPath.is_excluded_from_sync()); |
| 208 |
assert!(!ConfigKey::Theme.is_excluded_from_sync()); |
| 209 |
} |
| 210 |
} |
| 211 |
|