Introduced separate 'Boxed' traits, to allow for mocks

This commit is contained in:
Brian Picciano 2023-05-16 17:17:47 +02:00
parent d9676a4ce7
commit cab7a837a7
6 changed files with 78 additions and 38 deletions

View File

@ -1,6 +1,6 @@
use std::error::Error;
use std::path::{Path, PathBuf};
use std::{fs, io};
use std::{fs, io, sync};
use crate::domain;
use crate::origin::Descr;
@ -39,20 +39,22 @@ pub enum SetError {
}
#[mockall::automock]
pub trait Store: std::marker::Send + std::marker::Sync {
pub trait Store {
fn get(&self, domain: &domain::Name) -> Result<Config, GetError>;
fn set(&self, domain: &domain::Name, config: &Config) -> Result<(), SetError>;
}
pub trait BoxedStore: Store + Send + Sync + Clone {}
struct FSStore {
dir_path: PathBuf,
}
pub fn new(dir_path: &Path) -> io::Result<impl Store> {
pub fn new(dir_path: &Path) -> io::Result<impl BoxedStore> {
fs::create_dir_all(dir_path)?;
Ok(FSStore {
Ok(sync::Arc::new(FSStore {
dir_path: dir_path.into(),
})
}))
}
impl FSStore {
@ -65,7 +67,9 @@ impl FSStore {
}
}
impl Store for FSStore {
impl BoxedStore for sync::Arc<FSStore> {}
impl Store for sync::Arc<FSStore> {
fn get(&self, domain: &domain::Name) -> Result<Config, GetError> {
let config_file =
fs::File::open(self.config_file_path(domain)).map_err(|e| match e.kind() {

View File

@ -2,7 +2,7 @@ use crate::domain::{self, checker, config};
use crate::origin;
use std::error::Error;
use std::future::Future;
use std::pin;
use std::{pin, sync};
#[derive(thiserror::Error, Debug)]
pub enum GetConfigError {
@ -113,8 +113,8 @@ impl From<config::SetError> for SyncWithConfigError {
}
}
//#[mockall::automock(type Origin=origin::MockOrigin;)]
pub trait Manager: Send + Sync {
//#[mockall::automock]
pub trait Manager {
fn get_config(&self, domain: &domain::Name) -> Result<config::Config, GetConfigError>;
fn get_origin(
&self,
@ -128,36 +128,47 @@ pub trait Manager: Send + Sync {
) -> pin::Pin<Box<dyn Future<Output = Result<(), SyncWithConfigError>> + Send + '_>>;
}
pub trait BoxedManager: Manager + Send + Sync + Clone {}
struct ManagerImpl<OriginStore, DomainConfigStore>
where
OriginStore: origin::store::BoxedStore,
DomainConfigStore: config::BoxedStore,
{
origin_store: OriginStore,
domain_config_store: DomainConfigStore,
domain_checker: checker::DNSChecker,
}
pub fn new<OriginStore, DomainConfigStore>(
origin_store: OriginStore,
domain_config_store: DomainConfigStore,
domain_checker: checker::DNSChecker,
) -> impl Manager
) -> impl BoxedManager
where
OriginStore: origin::store::Store,
DomainConfigStore: config::Store,
OriginStore: origin::store::BoxedStore,
DomainConfigStore: config::BoxedStore,
{
ManagerImpl {
sync::Arc::new(ManagerImpl {
origin_store,
domain_config_store,
domain_checker,
}
})
}
struct ManagerImpl<OriginStore, DomainConfigStore>
impl<OriginStore, DomainConfigStore> BoxedManager
for sync::Arc<ManagerImpl<OriginStore, DomainConfigStore>>
where
OriginStore: origin::store::Store,
DomainConfigStore: config::Store,
OriginStore: origin::store::BoxedStore,
DomainConfigStore: config::BoxedStore,
{
origin_store: OriginStore,
domain_config_store: DomainConfigStore,
domain_checker: checker::DNSChecker,
}
impl<OriginStore, DomainConfigStore> Manager for ManagerImpl<OriginStore, DomainConfigStore>
impl<OriginStore, DomainConfigStore> Manager
for sync::Arc<ManagerImpl<OriginStore, DomainConfigStore>>
where
OriginStore: origin::store::Store,
DomainConfigStore: config::Store,
OriginStore: origin::store::BoxedStore,
DomainConfigStore: config::BoxedStore,
{
fn get_config(&self, domain: &domain::Name) -> Result<config::Config, GetConfigError> {
Ok(self.domain_config_store.get(domain)?)

View File

@ -123,7 +123,6 @@ fn main() {
.expect("domain config store initialized");
let manager = domiply::domain::manager::new(origin_store, domain_config_store, domain_checker);
let manager = sync::Arc::new(manager);
let service = domiply::service::new(
manager,

View File

@ -41,13 +41,17 @@ pub enum AllDescrsError {
/// Used in the return from all_descrs from Store.
pub type AllDescrsResult<T> = Result<T, AllDescrsError>;
#[mockall::automock(
type Origin=origin::MockOrigin;
type AllDescrsIter=Vec<AllDescrsResult<origin::Descr>>;
)]
/// Describes a storage mechanism for Origins. Each Origin is uniquely identified by its Descr.
pub trait Store: Send + Sync + Clone {
pub trait Store {
type Origin<'store>: origin::Origin + 'store
where
Self: 'store;
type AllDescrsIter<'store>: IntoIterator<Item = AllDescrsResult<origin::Descr>> + Send + 'store
type AllDescrsIter<'store>: IntoIterator<Item = AllDescrsResult<origin::Descr>> + 'store
where
Self: 'store;
@ -58,3 +62,5 @@ pub trait Store: Send + Sync + Clone {
fn get(&self, descr: origin::Descr) -> Result<Self::Origin<'_>, GetError>;
fn all_descrs(&self) -> AllDescrsResult<Self::AllDescrsIter<'_>>;
}
pub trait BoxedStore: Store + Send + Sync + Clone {}

View File

@ -65,7 +65,7 @@ struct Store {
origins: sync::RwLock<collections::HashMap<origin::Descr, sync::Arc<Origin>>>,
}
pub fn new(dir_path: PathBuf) -> io::Result<impl super::Store> {
pub fn new(dir_path: PathBuf) -> io::Result<impl super::BoxedStore> {
fs::create_dir_all(&dir_path)?;
Ok(sync::Arc::new(Store {
dir_path,
@ -191,11 +191,13 @@ impl Store {
}
}
impl super::BoxedStore for sync::Arc<Store> {}
impl super::Store for sync::Arc<Store> {
type Origin<'store> = sync::Arc<Origin>
where Self: 'store;
type AllDescrsIter<'store> = Box<dyn Iterator<Item = store::AllDescrsResult<origin::Descr>> + Send + 'store>
type AllDescrsIter<'store> = Box<dyn Iterator<Item = store::AllDescrsResult<origin::Descr>> + 'store>
where Self: 'store;
fn sync(&self, descr: origin::Descr, limits: store::Limits) -> Result<(), store::SyncError> {

View File

@ -15,20 +15,26 @@ mod util;
type SvcResponse = Result<Response<hyper::body::Body>, String>;
#[derive(Clone)]
pub struct Service<'svc> {
domain_manager: sync::Arc<dyn domain::manager::Manager>,
pub struct Service<'svc, DomainManager>
where
DomainManager: domain::manager::BoxedManager,
{
domain_manager: DomainManager,
target_a: net::Ipv4Addr,
passphrase: String,
http_domain: String,
handlebars: handlebars::Handlebars<'svc>,
}
pub fn new<'svc>(
domain_manager: sync::Arc<dyn domain::manager::Manager>,
pub fn new<'svc, DomainManager>(
domain_manager: DomainManager,
target_a: net::Ipv4Addr,
passphrase: String,
http_domain: String,
) -> Service<'svc> {
) -> Service<'svc, DomainManager>
where
DomainManager: domain::manager::BoxedManager,
{
Service {
domain_manager,
target_a,
@ -60,7 +66,10 @@ struct DomainSyncArgs {
passphrase: String,
}
impl<'svc> Service<'svc> {
impl<'svc, DomainManager> Service<'svc, DomainManager>
where
DomainManager: domain::manager::BoxedManager,
{
fn serve_string(&self, status_code: u16, path: &'_ str, body: Vec<u8>) -> SvcResponse {
let content_type = mime_guess::from_path(path)
.first_or_octet_stream()
@ -280,10 +289,13 @@ impl<'svc> Service<'svc> {
}
}
pub async fn handle_request(
svc: sync::Arc<Service<'_>>,
pub async fn handle_request<DomainManager>(
svc: sync::Arc<Service<'_, DomainManager>>,
req: Request<Body>,
) -> Result<Response<Body>, Infallible> {
) -> Result<Response<Body>, Infallible>
where
DomainManager: domain::manager::BoxedManager,
{
match handle_request_inner(svc, req).await {
Ok(res) => Ok(res),
Err(err) => panic!("unexpected error {err}"),
@ -297,7 +309,13 @@ fn strip_port(host: &str) -> &str {
}
}
pub async fn handle_request_inner(svc: sync::Arc<Service<'_>>, req: Request<Body>) -> SvcResponse {
pub async fn handle_request_inner<DomainManager>(
svc: sync::Arc<Service<'_, DomainManager>>,
req: Request<Body>,
) -> SvcResponse
where
DomainManager: domain::manager::BoxedManager,
{
let maybe_host = match (
req.headers()
.get("Host")