diff --git a/src/server/publish.rs b/src/server/publish.rs index 5ecbba7..261c157 100644 --- a/src/server/publish.rs +++ b/src/server/publish.rs @@ -18,6 +18,11 @@ use actix_http::body::MessageBody; use actix_service::ServiceFactory; use std::sync::OnceLock; +/// Name of the header to guard current documents +static HEADER_NAME: OnceLock = OnceLock::new(); +/// Values of the header to guard current documents +static HEADER_VALUES: OnceLock> = OnceLock::new(); + #[allow(clippy::expect_used)] /// Remove leading and trailing `/`s from the `path` string. fn clean_path(path: &str) -> String { @@ -175,10 +180,7 @@ pub fn init_app( match stelae_guard { Some(guard) => { - static HEADER_NAME: OnceLock = OnceLock::new(); HEADER_NAME.get_or_init(|| guard); - - static HEADER_VALUES: OnceLock> = OnceLock::new(); HEADER_VALUES.get_or_init(|| { state .archive diff --git a/src/stelae/archive.rs b/src/stelae/archive.rs index b68cd09..ef73e1f 100644 --- a/src/stelae/archive.rs +++ b/src/stelae/archive.rs @@ -49,7 +49,7 @@ impl Archive { let root: Stele; if let Some(individual_path) = path { tracing::info!("Serving individual Stele at path: {:?}", individual_path); - root = Stele::new(self.path.clone(), None, None, Some(individual_path), true)?; + root = Stele::new(&self.path, None, None, Some(individual_path), true)?; } else { let conf = self.get_config()?; @@ -59,7 +59,7 @@ impl Archive { tracing::info!("Serving {}/{} at path: {:?}", &org, &name, self.path); root = Stele::new( - self.path.clone(), + &self.path, Some(name), Some(org.clone()), Some(self.path.clone().join(org)), @@ -109,7 +109,7 @@ impl Archive { continue; } let child = Stele::new( - self.path.clone(), + &self.path, Some(name), Some(org.clone()), Some(parent_dir.join(org)), @@ -140,9 +140,9 @@ fn raise_error_if_in_existing_archive(path: &Path) -> anyhow::Result { #[derive(Deserialize, Serialize)] pub struct Config { /// The root Stele for this archive - root: stele::Config, + pub root: stele::Config, /// Whether this is a shallow archive (all repos depth=1) - shallow: bool, + pub shallow: bool, /// Custom HTTP headers used to interact with the Stele pub headers: Option, } diff --git a/src/stelae.rs b/src/stelae/mod.rs similarity index 100% rename from src/stelae.rs rename to src/stelae/mod.rs diff --git a/src/stelae/stele.rs b/src/stelae/stele.rs index 6516612..ee56fee 100644 --- a/src/stelae/stele.rs +++ b/src/stelae/stele.rs @@ -1,7 +1,7 @@ //! The Stele module contains the Stele object for interacting with //! Stelae. -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use super::types::repositories::Repository; use crate::{ @@ -34,7 +34,7 @@ impl Stele { /// Will panic if unable to determine the current root Stele. #[allow(clippy::unwrap_used, clippy::shadow_reuse)] pub fn new( - archive_path: PathBuf, + archive_path: &Path, name: Option, org: Option, path: Option, @@ -52,13 +52,13 @@ impl Stele { }); let path = path.unwrap_or_else(|| archive_path.join(&org)); let mut stele = Self { - archive_path: archive_path.clone(), + archive_path: archive_path.to_path_buf(), repositories: None, root, auth_repo: Repo { archive_path: archive_path.to_string_lossy().to_string(), path: path.join(&name), - org: org.clone(), + org, name: name.clone(), repo: GitRepository::open(path.join(&name)).unwrap(), }, @@ -109,8 +109,8 @@ impl Stele { /// Returns the first fallback repository found, or None if no fallback repository is found. #[must_use] pub fn get_fallback_repo(&self) -> Option { - if let &Some(ref repositories) = &self.repositories { - for (name, repository) in &repositories.repositories { + if let Some(repositories) = &self.repositories { + for repository in repositories.repositories.values() { let custom = &repository.custom; if let Some(ref is_fallback) = custom.is_fallback { if *is_fallback { diff --git a/src/stelae/types/repositories.rs b/src/stelae/types/repositories.rs index ebc33c2..379fb2c 100644 --- a/src/stelae/types/repositories.rs +++ b/src/stelae/types/repositories.rs @@ -20,12 +20,12 @@ use serde_json::Value; /// { /// "scopes": ["us/ca/cities/san-mateo"], /// "repositories": { -/// "test_org_1/data_repo_1": { +/// "`test_org_1/data_repo_1"`: { /// "custom": { /// "routes": ["example-route-glob-pattern-1"] /// } /// }, -/// "test_org_1/data_repo_2": { +/// "`test_org_1/data_repo_2"`: { /// "custom": { /// "serve-prefix": "_prefix" /// } @@ -85,6 +85,7 @@ impl Repositories { /// Get the repositories sorted by the length of their routes, longest first. /// /// This is needed for serving current documents because Actix routes are matched in the order they are added. + #[must_use] pub fn get_sorted_repositories(&self) -> Vec<&Repository> { let mut result = Vec::new(); for repository in self.repositories.values() { @@ -103,11 +104,13 @@ impl Repositories { } } +#[allow(clippy::missing_trait_methods)] impl<'de> Deserialize<'de> for Repositories { fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { + /// Visitor for the Repositories struct struct RepositoriesVisitor; impl<'de> Visitor<'de> for RepositoriesVisitor { @@ -131,7 +134,7 @@ impl<'de> Deserialize<'de> for Repositories { "repositories" => { let repos: HashMap = map.next_value()?; let mut new_repos = HashMap::new(); - for (key, value) in repos { + for (map_key, value) in repos { let custom_value = value.get("custom").ok_or_else(|| { serde::de::Error::custom("Missing 'custom' field") })?; @@ -142,10 +145,10 @@ impl<'de> Deserialize<'de> for Repositories { )) })?; let repo = Repository { - name: key.clone(), + name: map_key.clone(), custom, }; - new_repos.insert(key, repo); + new_repos.insert(map_key, repo); } repositories = new_repos; } @@ -160,8 +163,8 @@ impl<'de> Deserialize<'de> for Repositories { }) } } - - const FIELDS: &'static [&'static str] = &["scopes", "repositories"]; + /// Expected fields in the `repositories.json` file. + const FIELDS: &[&'static str] = &["scopes", "repositories"]; deserializer.deserialize_struct("Repositories", FIELDS, RepositoriesVisitor) } } diff --git a/src/utils/git.rs b/src/utils/git.rs index cb795d4..4881021 100644 --- a/src/utils/git.rs +++ b/src/utils/git.rs @@ -36,6 +36,7 @@ impl fmt::Debug for Repo { } } +#[allow(clippy::missing_trait_methods, clippy::unwrap_used)] impl Clone for Repo { fn clone(&self) -> Self { Self { diff --git a/src/utils/http.rs b/src/utils/http.rs index b993568..7e5e7fa 100644 --- a/src/utils/http.rs +++ b/src/utils/http.rs @@ -16,7 +16,7 @@ pub fn get_contenttype(path: &str) -> ContentType { .extension() .map_or("html", |ext| ext.to_str().map_or("", |ext_str| ext_str)); let mime = file_extension_to_mime(extension).first_or(mime::TEXT_HTML); - if (mime.type_(), mime.subtype().as_str()) == ((mime::APPLICATION, "rdf")) { + if (mime.type_(), mime.subtype().as_str()) == (mime::APPLICATION, "rdf") { return ContentType(mime::TEXT_PLAIN); } ContentType(mime)