dprint 0.53.2

Binary for dprint code formatter—a pluggable and configurable code formatting platform.
use anyhow::Result;
use anyhow::anyhow;
use dprint_core::async_runtime::async_trait;
use std::cell::RefCell;
use std::collections::HashMap;
use url::Url;

use crate::environment::DownloadedFile;
use crate::environment::UrlDownloader;

type CachedDownloadResult = Result<Option<Vec<u8>>, String>;

pub struct CachedDownloader<TInner: UrlDownloader> {
  inner: TInner,
  results: RefCell<HashMap<String, CachedDownloadResult>>,
}

impl<TInner: UrlDownloader> CachedDownloader<TInner> {
  pub fn new(inner: TInner) -> Self {
    Self {
      inner,
      results: Default::default(),
    }
  }
}

#[async_trait(?Send)]
impl<TInner: UrlDownloader> UrlDownloader for CachedDownloader<TInner> {
  async fn download_file_no_redirects(&self, url: &Url) -> Result<Option<DownloadedFile>> {
    {
      if let Some(result) = self.results.borrow().get(url.as_ref()) {
        return match result {
          Ok(result) => Ok(result.clone().map(|content| DownloadedFile {
            headers: Default::default(),
            content,
          })),
          Err(err) => Err(anyhow!("{:#}", err)),
        };
      }
    }
    let result = self.inner.download_file_no_redirects(url).await;
    self.results.borrow_mut().insert(
      url.to_string(),
      match &result {
        Ok(result) => Ok(result.as_ref().map(|r| r.content.clone())),
        Err(err) => Err(format!("{:#}", err)),
      },
    );
    result
  }
}

#[cfg(test)]
mod test {
  use super::*;
  use crate::environment::TestEnvironmentBuilder;

  #[test]
  fn should_download_and_cache() {
    let mut builder = TestEnvironmentBuilder::new();
    let exists_url = Url::parse("http://localhost/test.txt").unwrap();
    let not_exists_url = Url::parse("http://localhost/non-existent.txt").unwrap();
    let environment = builder.add_remote_file(exists_url.as_str(), "1").build();
    environment.clone().run_in_runtime(async move {
      let downloader = CachedDownloader::new(environment.clone());

      // should cache when not exists
      assert!(downloader.download_file_no_redirects(&not_exists_url).await.as_ref().unwrap().is_none());
      environment.add_remote_file_bytes(not_exists_url.as_str(), Vec::new());
      assert!(downloader.download_file_no_redirects(&not_exists_url).await.as_ref().unwrap().is_none());

      // should get data and have it cached
      assert_eq!(
        downloader
          .download_file_no_redirects(&exists_url)
          .await
          .as_ref()
          .unwrap()
          .as_ref()
          .unwrap()
          .content,
        "1".as_bytes()
      );
      environment.add_remote_file_bytes(exists_url.as_str(), Vec::new());
      assert_eq!(
        downloader
          .download_file_no_redirects(&exists_url)
          .await
          .as_ref()
          .unwrap()
          .as_ref()
          .unwrap()
          .content,
        "1".as_bytes()
      );
    });
  }
}