Remove final Boxed types

This commit is contained in:
Brian Picciano 2023-06-18 13:53:02 +02:00
parent 3d3dfb34ed
commit 6941ceec8e
4 changed files with 27 additions and 60 deletions

View File

@ -38,23 +38,18 @@ pub enum SetError {
Unexpected(#[from] unexpected::Error),
}
/// Used in the return from all_domains from Store.
pub type AllDomainsResult<T> = Result<T, unexpected::Error>;
#[mockall::automock]
pub trait Store {
pub trait Store: Sync + Send {
fn get(&self, domain: &domain::Name) -> Result<Config, GetError>;
fn set(&self, domain: &domain::Name, config: &Config) -> Result<(), SetError>;
fn all_domains(&self) -> AllDomainsResult<Vec<AllDomainsResult<domain::Name>>>;
fn all_domains(&self) -> Result<Vec<domain::Name>, unexpected::Error>;
}
pub trait BoxedStore: Store + Send + Sync + Clone + 'static {}
struct FSStore {
dir_path: PathBuf,
}
pub fn new(dir_path: &Path) -> io::Result<impl BoxedStore> {
pub fn new(dir_path: &Path) -> io::Result<sync::Arc<dyn Store>> {
fs::create_dir_all(dir_path)?;
Ok(sync::Arc::new(FSStore {
dir_path: dir_path.into(),
@ -71,9 +66,7 @@ impl FSStore {
}
}
impl BoxedStore for sync::Arc<FSStore> {}
impl Store for sync::Arc<FSStore> {
impl Store for FSStore {
fn get(&self, domain: &domain::Name) -> Result<Config, GetError> {
let path = self.config_file_path(domain);
let config_file = fs::File::open(path.as_path()).map_err(|e| match e.kind() {
@ -103,11 +96,11 @@ impl Store for sync::Arc<FSStore> {
Ok(())
}
fn all_domains(&self) -> AllDomainsResult<Vec<AllDomainsResult<domain::Name>>> {
Ok(fs::read_dir(&self.dir_path)
fn all_domains(&self) -> Result<Vec<domain::Name>, unexpected::Error> {
fs::read_dir(&self.dir_path)
.or_unexpected()?
.map(
|dir_entry_res: io::Result<fs::DirEntry>| -> AllDomainsResult<domain::Name> {
|dir_entry_res: io::Result<fs::DirEntry>| -> Result<domain::Name, unexpected::Error> {
let domain = dir_entry_res.or_unexpected()?.file_name();
let domain = domain.to_str().ok_or(unexpected::Error::from(
"couldn't convert os string to &str",
@ -117,7 +110,7 @@ impl Store for sync::Arc<FSStore> {
.map_unexpected_while(|| format!("parsing {domain} as domain name"))
},
)
.collect())
.try_collect()
}
}

View File

@ -116,8 +116,6 @@ impl From<config::SetError> for SyncWithConfigError {
pub type GetAcmeHttp01ChallengeKeyError = acme::manager::GetHttp01ChallengeKeyError;
pub type AllDomainsResult<T> = config::AllDomainsResult<T>;
#[mockall::automock]
pub trait Manager: Sync + Send {
fn get_config(&self, domain: &domain::Name) -> Result<config::Config, GetConfigError>;
@ -138,15 +136,12 @@ pub trait Manager: Sync + Send {
token: &str,
) -> Result<String, GetAcmeHttp01ChallengeKeyError>;
fn all_domains(&self) -> AllDomainsResult<Vec<AllDomainsResult<domain::Name>>>;
fn all_domains(&self) -> Result<Vec<domain::Name>, unexpected::Error>;
}
struct ManagerImpl<DomainConfigStore>
where
DomainConfigStore: config::BoxedStore,
{
struct ManagerImpl {
origin_store: sync::Arc<dyn origin::store::Store>,
domain_config_store: DomainConfigStore,
domain_config_store: sync::Arc<dyn config::Store>,
domain_checker: checker::DNSChecker,
acme_manager: Option<sync::Arc<dyn acme::manager::Manager>>,
@ -170,15 +165,12 @@ fn sync_origins(origin_store: &dyn origin::store::Store) {
});
}
pub fn new<'mgr, DomainConfigStore>(
pub fn new(
origin_store: sync::Arc<dyn origin::store::Store>,
domain_config_store: DomainConfigStore,
domain_config_store: sync::Arc<dyn config::Store>,
domain_checker: checker::DNSChecker,
acme_manager: Option<sync::Arc<dyn acme::manager::Manager>>,
) -> sync::Arc<dyn Manager>
where
DomainConfigStore: config::BoxedStore,
{
) -> sync::Arc<dyn Manager> {
let canceller = CancellationToken::new();
let origin_sync_handler = {
@ -206,10 +198,7 @@ where
})
}
impl<DomainConfigStore> ManagerImpl<DomainConfigStore>
where
DomainConfigStore: config::BoxedStore,
{
impl ManagerImpl {
pub async fn stop(self) {
self.canceller.cancel();
self.origin_sync_handler
@ -218,10 +207,7 @@ where
}
}
impl<DomainConfigStore> Manager for ManagerImpl<DomainConfigStore>
where
DomainConfigStore: config::BoxedStore,
{
impl Manager for ManagerImpl {
fn get_config(&self, domain: &domain::Name) -> Result<config::Config, GetConfigError> {
Ok(self.domain_config_store.get(domain)?)
}
@ -278,7 +264,7 @@ where
Err(GetAcmeHttp01ChallengeKeyError::NotFound)
}
fn all_domains(&self) -> AllDomainsResult<Vec<AllDomainsResult<domain::Name>>> {
fn all_domains(&self) -> Result<Vec<domain::Name>, unexpected::Error> {
self.domain_config_store.all_domains()
}
}

View File

@ -234,21 +234,16 @@ async fn main() {
}
for domain in domains_iter.unwrap().into_iter() {
match domain {
Ok(domain) => {
let _ = https_params
.domain_acme_manager
.sync_domain(domain.clone())
.await
.inspect_err(|err| {
log::error!(
"Error while getting cert for {}: {err}",
domain.as_str(),
)
});
}
Err(err) => log::error!("Error iterating through domains: {err}"),
};
let _ = https_params
.domain_acme_manager
.sync_domain(domain.clone())
.await
.inspect_err(|err| {
log::error!(
"Error while getting cert for {}: {err}",
domain.as_str(),
)
});
}
}
})

View File

@ -292,13 +292,6 @@ impl<'svc> Service<'svc> {
}
};
let domains: Vec<domain::Name> = match domains.into_iter().try_collect() {
Ok(domains) => domains,
Err(e) => {
return self.render_error_page(500, format!("failed get all domains: {e}").as_str())
}
};
let mut domains: Vec<String> = domains
.into_iter()
.map(|domain| domain.as_str().to_string())