Skip to content

Commit 503fa4d

Browse files
committed
Make initialization for BinContext easier to read
1 parent aaec038 commit 503fa4d

File tree

1 file changed

+37
-64
lines changed

1 file changed

+37
-64
lines changed

src/bin/cratesfyi.rs

Lines changed: 37 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -550,38 +550,45 @@ impl BinContext {
550550
}
551551
}
552552

553-
impl Context for BinContext {
554-
fn build_queue(&self) -> Result<Arc<BuildQueue>, Error> {
555-
Ok(self
556-
.build_queue
557-
.get_or_try_init::<_, Error>(|| {
558-
Ok(Arc::new(BuildQueue::new(
559-
self.pool()?,
560-
self.metrics()?,
561-
&*self.config()?,
562-
)))
563-
})?
564-
.clone())
565-
}
566-
567-
fn storage(&self) -> Result<Arc<Storage>, Error> {
568-
Ok(self
569-
.storage
570-
.get_or_try_init::<_, Error>(|| {
571-
Ok(Arc::new(Storage::new(
572-
self.pool()?,
573-
self.metrics()?,
574-
&*self.config()?,
575-
)?))
576-
})?
577-
.clone())
553+
macro_rules! lazy {
554+
( $(fn $name:ident($self:ident) -> $type:ty = $init:expr);+ $(;)? ) => {
555+
$(fn $name(&$self) -> Result<Arc<$type>, Error> {
556+
Ok($self
557+
.$name
558+
.get_or_try_init::<_, Error>(|| Ok(Arc::new($init)))?
559+
.clone())
560+
})*
578561
}
562+
}
579563

580-
fn config(&self) -> Result<Arc<Config>, Error> {
581-
Ok(self
582-
.config
583-
.get_or_try_init::<_, Error>(|| Ok(Arc::new(Config::from_env()?)))?
584-
.clone())
564+
impl Context for BinContext {
565+
lazy! {
566+
fn build_queue(self) -> BuildQueue = BuildQueue::new(
567+
self.pool()?,
568+
self.metrics()?,
569+
&*self.config()?,
570+
);
571+
fn storage(self) -> Storage = Storage::new(
572+
self.pool()?,
573+
self.metrics()?,
574+
&*self.config()?,
575+
)?;
576+
fn config(self) -> Config = Config::from_env()?;
577+
fn metrics(self) -> Metrics = Metrics::new()?;
578+
fn index(self) -> Index = {
579+
let config = self.config()?;
580+
let path = config.registry_index_path.clone();
581+
if let Some(registry_url) = config.registry_url.clone() {
582+
Index::from_url(path, registry_url)
583+
} else {
584+
Index::new(path)
585+
}?
586+
};
587+
fn repository_stats_updater(self) -> RepositoryStatsUpdater = {
588+
let config = self.config()?;
589+
let pool = self.pool()?;
590+
RepositoryStatsUpdater::new(&config, pool)
591+
};
585592
}
586593

587594
fn pool(&self) -> Result<Pool, Error> {
@@ -590,38 +597,4 @@ impl Context for BinContext {
590597
.get_or_try_init::<_, Error>(|| Ok(Pool::new(&*self.config()?, self.metrics()?)?))?
591598
.clone())
592599
}
593-
594-
fn metrics(&self) -> Result<Arc<Metrics>, Error> {
595-
Ok(self
596-
.metrics
597-
.get_or_try_init::<_, Error>(|| Ok(Arc::new(Metrics::new()?)))?
598-
.clone())
599-
}
600-
601-
fn index(&self) -> Result<Arc<Index>, Error> {
602-
Ok(self
603-
.index
604-
.get_or_try_init::<_, Error>(|| {
605-
let config = self.config()?;
606-
Ok(Arc::new(
607-
if let Some(registry_url) = config.registry_url.clone() {
608-
Index::from_url(config.registry_index_path.clone(), registry_url)
609-
} else {
610-
Index::new(config.registry_index_path.clone())
611-
}?,
612-
))
613-
})?
614-
.clone())
615-
}
616-
617-
fn repository_stats_updater(&self) -> Result<Arc<RepositoryStatsUpdater>, Error> {
618-
Ok(self
619-
.repository_stats_updater
620-
.get_or_try_init::<_, Error>(|| {
621-
let config = self.config()?;
622-
let pool = self.pool()?;
623-
Ok(Arc::new(RepositoryStatsUpdater::new(&config, pool)))
624-
})?
625-
.clone())
626-
}
627600
}

0 commit comments

Comments
 (0)