use crate::blueprint::Blueprint;
use async_trait::async_trait;
pub mod types;
pub use types::{
BlueprintEpoch, BlueprintId, BlueprintStoreError, BlueprintVersion, CommitMetadata,
ContentHash, Trace, TraceOrigin, TraceRef, Traced,
};
pub mod git2_store;
pub mod inmemory;
pub use git2_store::Git2BlueprintStore;
pub use inmemory::InMemoryBlueprintStore;
pub(crate) mod git2_blob_store;
#[async_trait]
pub trait BlueprintStore: Send + Sync {
fn name(&self) -> &str;
async fn read_head(&self, id: &BlueprintId) -> Result<Traced<Blueprint>, BlueprintStoreError>;
async fn write_new(
&self,
id: &BlueprintId,
new_bp: &Blueprint,
parents: &[BlueprintVersion],
metadata: CommitMetadata,
) -> Result<BlueprintVersion, BlueprintStoreError>;
async fn read_version(
&self,
id: &BlueprintId,
version: BlueprintVersion,
) -> Result<Traced<Blueprint>, BlueprintStoreError>;
async fn history(
&self,
id: &BlueprintId,
limit: usize,
) -> Result<Vec<BlueprintVersion>, BlueprintStoreError>;
async fn read_commit_rationale(
&self,
_id: &BlueprintId,
_version: BlueprintVersion,
) -> Result<Option<String>, BlueprintStoreError> {
Ok(None)
}
async fn list_ids(&self) -> Result<Vec<BlueprintId>, BlueprintStoreError>;
async fn archive_id(&self, _id: &BlueprintId) -> Result<(), BlueprintStoreError> {
Err(BlueprintStoreError::Unsupported(
"archive_id is not supported by this backend".into(),
))
}
async fn unarchive_id(&self, _id: &BlueprintId) -> Result<(), BlueprintStoreError> {
Err(BlueprintStoreError::Unsupported(
"unarchive_id is not supported by this backend".into(),
))
}
async fn is_archived(&self, _id: &BlueprintId) -> Result<bool, BlueprintStoreError> {
Ok(false)
}
}
pub fn canonical_yaml(bp: &Blueprint) -> Result<String, BlueprintStoreError> {
Ok(serde_yaml::to_string(bp)?)
}
pub fn blueprint_content_hash(bp: &Blueprint) -> Result<ContentHash, BlueprintStoreError> {
let yaml = canonical_yaml(bp)?;
Ok(ContentHash::from_bytes(yaml.as_bytes()))
}
pub fn blueprint_version(bp: &Blueprint) -> Result<BlueprintVersion, BlueprintStoreError> {
Ok(BlueprintVersion(blueprint_content_hash(bp)?))
}