0.0.1-alpha.3
This commit is contained in:
15
crates/knowledge-sync-api/Cargo.toml
Normal file
15
crates/knowledge-sync-api/Cargo.toml
Normal file
@@ -0,0 +1,15 @@
|
||||
# file: crates/knowledge-sync-api/Cargo.toml
|
||||
# version: 1
|
||||
|
||||
[package]
|
||||
name = "knowledge-sync-api"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
publish.workspace = true
|
||||
repository.workspace = true
|
||||
|
||||
[dependencies]
|
||||
knowledge-domain-lib.workspace = true
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
57
crates/knowledge-sync-api/src/lib.rs
Normal file
57
crates/knowledge-sync-api/src/lib.rs
Normal file
@@ -0,0 +1,57 @@
|
||||
// file: crates/knowledge-sync-api/src/lib.rs
|
||||
// version: 1
|
||||
|
||||
//! Contracts for provider-neutral repository synchronization signals.
|
||||
//!
|
||||
//! A signal requests reconciliation of a source. It is not authoritative content and does not
|
||||
//! replace resolution of the target Git revision by the Git repository contract.
|
||||
|
||||
/// Coarse origin of a synchronization request.
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub enum RepositorySyncCause {
|
||||
/// An explicit request from an operator or application use case.
|
||||
Explicit,
|
||||
/// A periodic reconciliation independent of external hooks.
|
||||
Periodic,
|
||||
/// A non-authoritative external signal such as a Git hook, webhook or filesystem watcher.
|
||||
ExternalSignal,
|
||||
}
|
||||
|
||||
/// Request to reconcile one logical content source.
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub struct RepositorySyncSignal {
|
||||
source_id: knowledge_domain_lib::SourceId,
|
||||
cause: crate::RepositorySyncCause,
|
||||
}
|
||||
|
||||
impl crate::RepositorySyncSignal {
|
||||
/// Build a synchronization signal for one source.
|
||||
pub fn new(source_id: knowledge_domain_lib::SourceId, cause: crate::RepositorySyncCause) -> crate::RepositorySyncSignal {
|
||||
return crate::RepositorySyncSignal { source_id, cause };
|
||||
}
|
||||
|
||||
/// Return the source that must be reconciled.
|
||||
pub fn source_id(&self) -> &knowledge_domain_lib::SourceId {
|
||||
return &self.source_id;
|
||||
}
|
||||
|
||||
/// Return the coarse trigger origin.
|
||||
pub fn cause(&self) -> crate::RepositorySyncCause {
|
||||
return self.cause;
|
||||
}
|
||||
}
|
||||
|
||||
/// Producer of synchronization signals.
|
||||
///
|
||||
/// Concrete implementations may represent explicit queues, polling, hooks, webhooks or other
|
||||
/// event mechanisms. The scheduling/runtime model remains outside this passive contract.
|
||||
pub trait RepositoryEventSource {
|
||||
/// Implementation-specific recoverable error.
|
||||
type Error;
|
||||
|
||||
/// Return the next available signal, or `None` when no signal is currently available.
|
||||
fn next_signal(&mut self) -> Result<Option<crate::RepositorySyncSignal>, Self::Error>;
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod unit_tests;
|
||||
45
crates/knowledge-sync-api/src/unit_tests.rs
Normal file
45
crates/knowledge-sync-api/src/unit_tests.rs
Normal file
@@ -0,0 +1,45 @@
|
||||
// file: crates/knowledge-sync-api/src/unit_tests.rs
|
||||
// version: 1
|
||||
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
struct FakeEventError;
|
||||
|
||||
struct OneShotEventSource {
|
||||
signal: Option<crate::RepositorySyncSignal>,
|
||||
}
|
||||
|
||||
impl crate::RepositoryEventSource for OneShotEventSource {
|
||||
type Error = FakeEventError;
|
||||
|
||||
fn next_signal(&mut self) -> Result<Option<crate::RepositorySyncSignal>, Self::Error> {
|
||||
return Ok(self.signal.take());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn external_signal_is_only_a_request_to_reconcile_a_source() {
|
||||
let source_result = knowledge_domain_lib::SourceId::new("snippets-extra");
|
||||
assert!(source_result.is_ok());
|
||||
let source_id = match source_result {
|
||||
Ok(value) => value,
|
||||
Err(_) => return,
|
||||
};
|
||||
let signal = crate::RepositorySyncSignal::new(source_id, crate::RepositorySyncCause::ExternalSignal);
|
||||
let mut event_source = OneShotEventSource { signal: Some(signal) };
|
||||
let first_result = crate::RepositoryEventSource::next_signal(&mut event_source);
|
||||
assert!(first_result.is_ok());
|
||||
let first = match first_result {
|
||||
Ok(value) => value,
|
||||
Err(_) => return,
|
||||
};
|
||||
assert!(first.is_some());
|
||||
let signal = match first {
|
||||
Some(value) => value,
|
||||
None => return,
|
||||
};
|
||||
assert_eq!(signal.source_id().as_str(), "snippets-extra");
|
||||
assert_eq!(signal.cause(), crate::RepositorySyncCause::ExternalSignal);
|
||||
let second_result = crate::RepositoryEventSource::next_signal(&mut event_source);
|
||||
assert_eq!(second_result, Ok(None));
|
||||
return;
|
||||
}
|
||||
Reference in New Issue
Block a user