Skip to content

Commit

Permalink
Add documentation for missing=
Browse files Browse the repository at this point in the history
  • Loading branch information
alexjg committed Sep 27, 2023
1 parent 14a6a35 commit 92e1d3f
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions autosurgeon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down

0 comments on commit 92e1d3f

Please sign in to comment.