vite-static-actix-web 1.2.0

Actix Web integration support for `vite-static`
Documentation
#![warn(clippy::pedantic)]

use std::rc::Rc;
use vite_static_shared::DynManifest;

use actix_web::http::header::{CacheControl, CacheDirective};

#[allow(unused_imports)]
use vite_static_shared::Manifest;

mod service;
pub use service::*;

/// Actix service for serving Vite Static.
///
/// This service uses [`Manifest::base()`] as root path.
///
/// ```no_run
/// # use actix_web::{
/// #     App, HttpServer,
/// #     http::header::{CacheControl, CacheDirective},
/// # };
/// # use vite_static_actix_web::*;
/// # use vite_static_shared::__tests::*;
/// #
/// # #[actix_web::main]
/// # async fn main() -> std::io::Result<()> {
/// HttpServer::new(|| {
///     App::new()
///         .service(ActixFiles::new(MyViteStatic.boxed()))
/// })
/// .bind(("127.0.0.1", 8080))?
/// .run()
/// .await
/// # }
/// ```
#[derive(Clone)]
pub struct ActixFiles {
    manifest: Rc<DynManifest<'static>>,
    cache_control: CacheControl,
}

impl ActixFiles {
    /// Creates new [`ActixFiles`] service.
    ///
    /// Takes [`DynManifest`] (boxed [`Manifest`]).
    ///
    /// ```
    /// # use vite_static_actix_web::*;
    /// # use vite_static_shared::__tests::*;
    /// #
    /// ActixFiles::new(MyViteStatic.boxed())
    /// # ;
    /// ```
    #[must_use]
    pub fn new(manifest: DynManifest<'static>) -> Self {
        Self {
            manifest: Rc::new(manifest),
            cache_control: CacheControl(vec![CacheDirective::MaxAge(604_800)]),
        }
    }

    /// Sets [`CacheControl`] for served static.
    ///
    /// By default, `CacheControl` is set to "Max Age of 7 days".
    ///
    /// ```rust
    /// # use actix_web::http::header::{CacheControl, CacheDirective};
    /// # use vite_static_actix_web::*;
    /// # use vite_static_shared::__tests::*;
    /// #
    /// ActixFiles::new(MyViteStatic.boxed())
    ///     .cache_control(CacheControl(vec![CacheDirective::MaxAge(604_800)])) // 7 days
    /// # ;
    /// ```
    #[must_use]
    pub fn cache_control(mut self, value: CacheControl) -> Self {
        self.cache_control = value;
        self
    }
}