Skip to content

Commit

Permalink
Add documentation for MaybeMissing
Browse files Browse the repository at this point in the history
  • Loading branch information
alexjg committed Sep 27, 2023
1 parent c37b109 commit 14a6a35
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions autosurgeon/src/hydrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,44 @@ impl<T> HydrateResultExt<Option<T>> for Result<Option<T>, HydrateError> {
}
}

/// A convenience type to track whether a value was missing in the document
///
/// `autosurgeon` provides a [`Hydrate`] implementation for `Option<T>` which
/// will return `None` if the value in the document is [`automerge::ScalarValue::Null`].
/// This isn't always what you want, sometimes you want something to be `None`
/// if it isn't in the document at all. `MaybeMissing<T>` provides this behaviour.
///
/// # Example
///
/// ```rust
/// # use autosurgeon::{Hydrate, hydrate::MaybeMissing};
/// # use automerge::transaction::Transactable;
/// let mut doc = automerge::AutoCommit::new();
/// let name = MaybeMissing::<String>::hydrate(&doc, &automerge::ROOT, "name".into()).unwrap();
/// assert_eq!(name, MaybeMissing::Missing);
///
/// doc.put(&automerge::ROOT, "name", "Moist von Lipwig").unwrap();
/// let name = MaybeMissing::<String>::hydrate(&doc, &automerge::ROOT, "name".into()).unwrap();
/// assert_eq!(name, MaybeMissing::Present("Moist von Lipwig".to_string()));
/// ```
///
/// Note that this can be combined with `Option<T>` to get both behaviours
///
/// ```rust
/// # use autosurgeon::{Hydrate, hydrate::MaybeMissing};
/// # use automerge::transaction::Transactable;
/// let mut doc = automerge::AutoCommit::new();
/// let name = MaybeMissing::<Option<String>>::hydrate(&doc, &automerge::ROOT, "name".into()).unwrap();
/// assert_eq!(name, MaybeMissing::Missing);
///
/// doc.put(&automerge::ROOT, "name", "Moist von Lipwig").unwrap();
/// let name = MaybeMissing::<Option<String>>::hydrate(&doc, &automerge::ROOT, "name".into()).unwrap();
/// assert_eq!(name, MaybeMissing::Present(Some("Moist von Lipwig".to_string())));
///
/// doc.put(&automerge::ROOT, "name", automerge::ScalarValue::Null).unwrap();
/// let name = MaybeMissing::<Option<String>>::hydrate(&doc, &automerge::ROOT, "name".into()).unwrap();
/// assert_eq!(name, MaybeMissing::Present(None));
/// ```
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
pub enum MaybeMissing<T> {
#[default]
Expand Down

0 comments on commit 14a6a35

Please sign in to comment.