diff options
| -rw-r--r-- | src/access/registry.rs | 7 | ||||
| -rw-r--r-- | src/document/store.rs | 17 | ||||
| -rw-r--r-- | src/remake/delta.rs | 4 | ||||
| -rw-r--r-- | src/remake/records.rs | 4 | ||||
| -rw-r--r-- | src/remake/snapshot.rs | 3 | ||||
| -rw-r--r-- | src/swarm/members.rs | 5 | ||||
| -rw-r--r-- | src/swarm/peer/forget.rs | 5 |
7 files changed, 29 insertions, 16 deletions
diff --git a/src/access/registry.rs b/src/access/registry.rs index e0cbe47..5899bdd 100644 --- a/src/access/registry.rs +++ b/src/access/registry.rs @@ -35,12 +35,15 @@ pub(crate) fn user_path(name: &str) -> Vec<String> { /// Whether the swarm has a registry: until it has, every user a node lets in may do /// everything but write protocol data (`docs/access.md`, A swarm without a registry). pub(crate) fn exists(doc: &impl ReadDoc) -> bool { - store::is_map(doc, &users_dir()) + matches!(store::keys_at(doc, &users_dir()), Ok(Some(_))) } /// The registered users' names, sorted. pub(crate) fn names(doc: &impl ReadDoc) -> Vec<String> { - let mut names = store::keys_at(doc, &users_dir()).unwrap_or_default(); + let mut names = store::keys_at(doc, &users_dir()) + .ok() + .flatten() + .unwrap_or_default(); names.sort(); names } diff --git a/src/document/store.rs b/src/document/store.rs index b343a2e..2e27b44 100644 --- a/src/document/store.rs +++ b/src/document/store.rs @@ -650,16 +650,15 @@ fn summary( Ok(JsonValue::Object(out)) } -/// Whether `path` holds a map (one, or concurrent ones merged). -pub(crate) fn is_map(doc: &impl ReadDoc, path: &[String]) -> bool { - matches!(resolve_through(doc, path), Ok(Some(Resolved::Maps(_)))) -} - -/// Keys of the merged map at `path`; empty if it doesn't exist or isn't a map. -pub(crate) fn keys_at(doc: &impl ReadDoc, path: &[String]) -> Result<Vec<String>, StoreError> { +/// Keys of the merged map at `path`; None if there is no map there (nothing, or +/// another value). +pub(crate) fn keys_at( + doc: &impl ReadDoc, + path: &[String], +) -> Result<Option<Vec<String>>, StoreError> { Ok(match resolve_through(doc, path)? { - Some(Resolved::Maps(maps)) => merged_keys(doc, &maps).into_iter().collect(), - _ => Vec::new(), + Some(Resolved::Maps(maps)) => Some(merged_keys(doc, &maps).into_iter().collect()), + _ => None, }) } diff --git a/src/remake/delta.rs b/src/remake/delta.rs index 638231c..6864c4d 100644 --- a/src/remake/delta.rs +++ b/src/remake/delta.rs @@ -242,8 +242,8 @@ fn replay_path( // Replacing an ancestor of the protocol metadata as a whole would clobber it; replay // its children one by one instead. if contains_protocol_path(path) { - let mut keys = store::keys_at(src, path).unwrap_or_default(); - keys.extend(store::keys_at(dst, path).unwrap_or_default()); + let mut keys = store::keys_at(src, path).ok().flatten().unwrap_or_default(); + keys.extend(store::keys_at(dst, path).ok().flatten().unwrap_or_default()); keys.sort(); keys.dedup(); for key in keys { diff --git a/src/remake/records.rs b/src/remake/records.rs index 0c4c444..21b383c 100644 --- a/src/remake/records.rs +++ b/src/remake/records.rs @@ -360,6 +360,8 @@ pub(crate) fn load(doc: &impl ReadDoc, id: &str) -> Option<Record> { pub(crate) fn load_all(doc: &impl ReadDoc) -> Vec<(String, Record)> { let register = load_register(doc); store::keys_at(doc, &records_dir()) + .ok() + .flatten() .unwrap_or_default() .into_iter() .filter_map(|id| { @@ -434,6 +436,8 @@ fn register_path(voter: &str) -> Vec<String> { /// The votes register; unreadable entries are skipped with a warning. pub(crate) fn load_register(doc: &impl ReadDoc) -> Register { store::keys_at(doc, &meta::path(&[VOTES])) + .ok() + .flatten() .unwrap_or_default() .into_iter() .filter_map(|voter| { diff --git a/src/remake/snapshot.rs b/src/remake/snapshot.rs index 157a28d..220989e 100644 --- a/src/remake/snapshot.rs +++ b/src/remake/snapshot.rs @@ -231,6 +231,7 @@ pub(crate) fn verify_document(doc: &Document, epoch: Epoch) -> Result<EpochMetad pub(crate) fn epoch_of(doc: &Document) -> Result<Epoch, String> { let generation = store::keys_at(doc, &meta::path(&[EPOCHS])) .map_err(|error| error.to_string())? + .unwrap_or_default() .iter() .filter_map(|key| key.parse::<u64>().ok()) .max(); @@ -295,7 +296,7 @@ pub(crate) fn build( Ok(()) | Err(StoreError::NotFound) => {} Err(error) => return Err(error.into()), } - let mut protocol_keys = store::keys_at(&base, &meta::path(&[]))?; + let mut protocol_keys = store::keys_at(&base, &meta::path(&[]))?.unwrap_or_default(); protocol_keys.sort(); for key in protocol_keys .into_iter() diff --git a/src/swarm/members.rs b/src/swarm/members.rs index 1cb4062..cbd970d 100644 --- a/src/swarm/members.rs +++ b/src/swarm/members.rs @@ -40,7 +40,10 @@ pub(crate) fn member_path(pk: &str) -> Vec<String> { /// Public keys of all registered members, sorted. pub(crate) fn members(doc: &impl ReadDoc) -> Vec<String> { - let mut members = store::keys_at(doc, &members_dir()).unwrap_or_default(); + let mut members = store::keys_at(doc, &members_dir()) + .ok() + .flatten() + .unwrap_or_default(); members.sort(); members } diff --git a/src/swarm/peer/forget.rs b/src/swarm/peer/forget.rs index ce064a2..a1676b5 100644 --- a/src/swarm/peer/forget.rs +++ b/src/swarm/peer/forget.rs @@ -32,7 +32,10 @@ pub(crate) fn affects_forget_records(path: &[String]) -> bool { } fn forgotten_peers(doc: &Document) -> Vec<String> { - store::keys_at(doc, &forgotten_dir()).unwrap_or_default() + store::keys_at(doc, &forgotten_dir()) + .ok() + .flatten() + .unwrap_or_default() } /// The forget records this node acted on: peer → the record as it read then. |
