Skip to main content

kdeets_lib/
lib.rs

1const HEADER: &str = "Crate versions for";
2const SETUP_HEADER: &str = "Local registry set up for";
3const LINE_CHAR: char = '🭶';
4
5mod combo;
6mod crate_versions;
7mod error;
8mod rust_versions;
9mod setup;
10
11pub use crate_versions::CrateVersions;
12pub use error::Error;
13pub use rust_versions::RustVersions;
14pub use setup::Setup;
15
16pub(crate) use combo::ComboIndex;
17
18use reqwest::blocking::ClientBuilder;
19use tame_index::index::RemoteSparseIndex;
20use tame_index::{IndexLocation, IndexUrl, SparseIndex};
21
22pub(crate) fn get_remote_combo_index() -> Result<ComboIndex, tame_index::error::Error> {
23    let index = get_sparse_index()?;
24    let builder = get_client_builder();
25    let client = builder.build()?;
26
27    let remote_index = RemoteSparseIndex::new(index, client);
28
29    Ok(ComboIndex::from(remote_index))
30}
31
32pub(crate) fn get_sparse_index() -> Result<SparseIndex, tame_index::error::Error> {
33    let il = IndexLocation::new(IndexUrl::CratesIoSparse);
34    SparseIndex::new(il)
35}
36
37pub(crate) fn get_client_builder() -> ClientBuilder {
38    // Create a certificate store using webpki_roots, which packages
39    let rcs: rustls::RootCertStore = webpki_roots::TLS_SERVER_ROOTS.iter().cloned().collect();
40    let client_config = rustls::ClientConfig::builder_with_provider(std::sync::Arc::new(
41        // Use `ring` as the crypto provider
42        rustls::crypto::ring::default_provider(),
43    ))
44    .with_protocol_versions(rustls::DEFAULT_VERSIONS)
45    .unwrap()
46    .with_root_certificates(rcs)
47    .with_no_client_auth();
48
49    reqwest::blocking::Client::builder()
50        // Set the TLS backend. Note that this *requires* that the version of
51        // rustls is the same as the one reqwest is using
52        .tls_backend_preconfigured(client_config)
53}
54
55#[cfg(test)]
56mod tests {
57
58    use std::vec;
59
60    use crate::ComboIndex;
61    use crate::get_remote_combo_index;
62    use tame_index::{PathBuf, index::LocalRegistry};
63    use tempfile::TempDir;
64
65    const TEST_REGISTRY: &str = "tests/registry";
66
67    pub(crate) fn get_temp_local_registry() -> (TempDir, String) {
68        let temp_dir = tempfile::tempdir().unwrap();
69        println!("Temp dir: {}", temp_dir.path().display());
70        let registry_path = temp_dir.path().join("registry");
71        let registry = registry_path.to_str().unwrap();
72
73        let options = fs_extra::dir::CopyOptions::new();
74
75        let from_path = vec![TEST_REGISTRY];
76
77        let _ = fs_extra::copy_items(&from_path, temp_dir.path().to_str().unwrap(), &options);
78        let _ = fs_extra::copy_items(&from_path, "/tmp/test/", &options);
79
80        (temp_dir, registry.to_string())
81    }
82
83    pub(crate) fn get_test_index(registry: &str) -> Result<ComboIndex, tame_index::error::Error> {
84        let local_registry = LocalRegistry::open(PathBuf::from(registry), false)?;
85
86        Ok(ComboIndex::from(local_registry))
87    }
88
89    #[test]
90    fn test_get_sparse_index_success() {
91        let result = get_remote_combo_index();
92        assert!(result.is_ok());
93        let index = result.unwrap();
94        assert!(matches!(index, ComboIndex::Sparse(_)));
95    }
96
97    #[test]
98    fn test_get_sparse_index_type() {
99        let result = get_remote_combo_index();
100        assert!(matches!(result, Ok(ComboIndex::Sparse(_))));
101    }
102
103    #[test]
104    fn test_sparse_index_error_handling() {
105        let result = get_remote_combo_index();
106        match result {
107            Ok(_) => (),
108            Err(e) => panic!("Expected Ok, got Err: {e:?}"),
109        }
110    }
111}