From 92e1d3f28185d85f847d41fd49a4e9b65fbf2e4d Mon Sep 17 00:00:00 2001 From: Alex Good Date: Wed, 27 Sep 2023 09:51:25 +0100 Subject: [PATCH] Add documentation for `missing=` --- autosurgeon/src/lib.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/autosurgeon/src/lib.rs b/autosurgeon/src/lib.rs index ef6f046..6344670 100644 --- a/autosurgeon/src/lib.rs +++ b/autosurgeon/src/lib.rs @@ -416,6 +416,37 @@ //! } //! } //! ``` +//! +//! #### Providing default values with `missing=` +//! +//! Occasionally you may want to provide a default value for a field which +//! wasn't found in the document. This can be done using the `missing` attribute. +//! The value of the `missing` attribute should be the name of a function which +//! returns a value of the annotated field. +//! +//! ```rust +//! # use autosurgeon::{Reconcile, Hydrate, ReadDoc, Prop, HydrateError, hydrate}; +//! # use automerge::transaction::Transactable; +//! #[derive(Hydrate)] +//! struct Contact { +//! name: String, +//! #[autosurgeon(missing="Visibility::default")] +//! visibility: Visibility +//! } +//! +//! #[derive(Hydrate, Default, Debug, PartialEq)] +//! enum Visibility { +//! #[default] +//! Public, +//! Private, +//! } +//! +//! let mut doc = automerge::AutoCommit::new(); +//! doc.put(&automerge::ROOT, "name", "Sherlock Holmes".to_string()); +//! let contact: Contact = hydrate(&doc).unwrap(); +//! assert_eq!(contact.visibility, Visibility::Public); +//! +//! ``` #[doc = include_str!("../../README.md")] #[cfg(doctest)]