From b9d9f1d5f4b886be0e847e4cb20131a8a859f293 Mon Sep 17 00:00:00 2001 From: Schneems Date: Wed, 18 Dec 2024 14:22:53 -0600 Subject: [PATCH] More docs --- commons/src/layer/cache_buddy.rs | 10 ++- .../src/layer/fixtures/cache_buddy_example.md | 83 +++++++++++++++++++ 2 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 commons/src/layer/fixtures/cache_buddy_example.md diff --git a/commons/src/layer/cache_buddy.rs b/commons/src/layer/cache_buddy.rs index 57c74cfb..3880da45 100644 --- a/commons/src/layer/cache_buddy.rs +++ b/commons/src/layer/cache_buddy.rs @@ -40,6 +40,8 @@ //! build output. If the cache is cleared for any reason, then a user readable message is returned. This message should //! be printed to the buildpack user so they can understand what caused the cache to clear. //! +#![doc = include_str!("./fixtures/cache_buddy_example.md")] + use crate::display::SentenceList; use cache_diff::CacheDiff; use libcnb::build::BuildContext; @@ -64,6 +66,7 @@ use std::fmt::Debug; /// When a `CacheDiff::diff` is empty, the layer is kept and the old data is returned. Otherwise, /// the layer is deleted and the changes are returned. /// +#[doc = include_str!("./fixtures/cache_buddy_example.md")] #[derive(Default, Debug, Clone, Eq, PartialEq)] pub struct CacheBuddy { pub build: Option, @@ -212,6 +215,12 @@ impl AsRef for Meta { } /// Takes in a directory and returns a minimal build context for use in testing caching behavior +/// +/// Public for use in doc tests, but the interface is not stable. +/// +/// # Panics +/// +/// - If a context cannot be created #[cfg(test)] fn temp_build_context( from_dir: impl AsRef, @@ -284,7 +293,6 @@ mod tests { migrate_toml_chain! {TestMetadata} struct FakeBuildpack; - impl libcnb::Buildpack for FakeBuildpack { type Platform = GenericPlatform; type Metadata = GenericMetadata; diff --git a/commons/src/layer/fixtures/cache_buddy_example.md b/commons/src/layer/fixtures/cache_buddy_example.md new file mode 100644 index 00000000..328c8650 --- /dev/null +++ b/commons/src/layer/fixtures/cache_buddy_example.md @@ -0,0 +1,83 @@ +# Example + +``` +use commons::layer::cache_buddy::CacheBuddy; +use commons::layer::cache_buddy::Meta; +use cache_diff::CacheDiff; +use magic_migrate::TryMigrate; +use serde::Deserializer; + +use libcnb::layer::{LayerState, EmptyLayerCause}; +use libcnb::data::layer_name; + +# #[derive(Debug, serde::Serialize, serde::Deserialize, Clone, cache_diff::CacheDiff)] +# #[serde(deny_unknown_fields)] +# struct TestMetadata { +# value: String, +# } +# use magic_migrate::Migrate; +# magic_migrate::migrate_toml_chain!(TestMetadata); +# +# struct FakeBuildpack; +# +# impl libcnb::Buildpack for FakeBuildpack { +# type Platform = libcnb::generic::GenericPlatform; +# type Metadata = libcnb::generic::GenericMetadata; +# type Error = std::convert::Infallible; +# +# fn detect( +# &self, +# _context: libcnb::detect::DetectContext, +# ) -> libcnb::Result { +# todo!() +# } +# +# fn build( +# &self, +# _context: libcnb::build::BuildContext, +# ) -> libcnb::Result { +# todo!() +# } +# } +# fn install_ruby(path: &std::path::Path) { +# todo!(); +# } +# +# pub(crate) fn call( +# context: &libcnb::build::BuildContext, +# ) -> libcnb::Result<(), ::Error> { +# let metadata_owned = TestMetadata { value: "Hello".to_string() }; +# let metadata = &metadata_owned; +let layer_ref = CacheBuddy::new().layer(layer_name!("ruby"), context, metadata)?; +match &layer_ref.state { + // CacheDiff reported no difference, cache was kept + LayerState::Restored { cause } => { + println!(" - {cause}"); // States that the cache is being used + match cause { + Meta::Data(old) => { + // Inspect or use the old metadata from cache here if you like! + assert!(metadata.diff(old).is_empty()); + }, + Meta::Message(_) => unreachable!("Will only ever contain metadata when the cache is retained") + } + } + LayerState::Empty { cause } => { + match cause { + // Nothing in old cache + EmptyLayerCause::NewlyCreated => {} + // Problem restoring old cache (`TryMigrate` could not migrate the old metadata) + EmptyLayerCause::InvalidMetadataAction { cause } + // Difference with old cache + | EmptyLayerCause::RestoredLayerAction { cause } => { + // Report why the cache was cleared + println!(" - {cause}"); + } + } + println!(" - Installing"); + install_ruby(&layer_ref.path()); + println!(" - Done"); + } +} +# Ok(()) +# } +```