Try to get rid of owned types

Inspired by https://www.youtube.com/watch?v=A4cKi7PTJSs. This turns a
lot of cloning Strings and PathBufs into Rcs.
This commit is contained in:
Adrian Heine 2023-06-17 14:53:38 +02:00
parent e3b425eb1c
commit d091265d27
20 changed files with 343 additions and 232 deletions

View file

@ -4,7 +4,7 @@ use crate::artifacts::{
};
use crate::templates::php::FpmPoolConfig;
use std::hash::Hash;
use std::path::PathBuf;
use std::path::Path;
pub trait Resource {
type Artifact;
@ -41,23 +41,41 @@ impl<D> Resource for KeyAndCertBundle<D> {
}
#[derive(Debug, Hash, PartialEq, Eq)]
pub struct File<P>(pub P, pub String);
pub struct File<P>(pub P, pub Rc<str>);
impl<P> Resource for File<P> {
type Artifact = ();
}
impl File<Rc<Path>> {
pub fn new(p: impl Into<Rc<Path>>, content: impl AsRef<str>) -> Self {
Self(p.into(), content.as_ref().into())
}
}
#[derive(Debug, Hash, PartialEq, Eq)]
pub struct GitCheckout<'a, P>(pub P, pub &'a str, pub &'a str);
impl<'a, P> Resource for GitCheckout<'a, P> {
type Artifact = ();
}
impl<'a> GitCheckout<'a, Rc<Path>> {
pub fn new(target: impl Into<Rc<Path>>, src: &'a str, head: &'a str) -> Self {
Self(target.into(), src, head)
}
}
#[derive(Debug, Hash, PartialEq, Eq)]
pub struct Dir<P>(pub P);
impl<P> Resource for Dir<P> {
type Artifact = ();
}
impl Dir<Rc<Path>> {
pub fn new(p: impl AsRef<Path>) -> Self {
Self(p.as_ref().into())
}
}
#[derive(Debug, Hash, PartialEq, Eq)]
pub struct UserForDomain<D>(pub D);
impl<D> Resource for UserForDomain<D> {
@ -106,10 +124,11 @@ impl<P> Resource for LoadedDirectory<P> {
type Artifact = PathArtifact;
}
pub fn get_saved_directory<P: Clone>(
pub fn get_saved_directory(
storage_name: &'static str,
target: P,
) -> (StoredDirectory<P>, LoadedDirectory<P>) {
target: impl Into<Rc<Path>>,
) -> (StoredDirectory<Rc<Path>>, LoadedDirectory<Rc<Path>>) {
let target = target.into();
(
StoredDirectory(storage_name, target.clone()),
LoadedDirectory(storage_name, target),
@ -117,7 +136,7 @@ pub fn get_saved_directory<P: Clone>(
}
#[derive(Debug, Hash, PartialEq, Eq)]
pub struct User(pub String);
pub struct User(pub Rc<str>);
impl Resource for User {
type Artifact = ();
}
@ -134,20 +153,32 @@ impl<P> Resource for NpmInstall<P> {
type Artifact = ();
}
impl NpmInstall<Rc<Path>> {
pub fn new(path: impl Into<Rc<Path>>) -> Self {
Self(path.into())
}
}
#[derive(Debug, Hash, PartialEq, Eq)]
pub struct Owner<P>(pub String, pub P);
pub struct Owner<P>(pub Rc<str>, pub P);
impl<P> Resource for Owner<P> {
type Artifact = ();
}
impl Owner<Rc<Path>> {
pub fn new(user: &UserNameArtifact, p: impl Into<Rc<Path>>) -> Self {
Self(user.0.clone(), p.into())
}
}
#[derive(Debug, Hash, PartialEq, Eq)]
pub struct ServeCustom<D>(pub D, pub String);
pub struct ServeCustom<D>(pub D, pub Rc<str>);
impl<D> Resource for ServeCustom<D> {
type Artifact = PathArtifact;
}
#[derive(Debug, Hash, PartialEq, Eq)]
pub struct ServePhp<D, P, C>(pub D, pub P, pub &'static str, pub String, pub C);
pub struct ServePhp<D, P, C>(pub D, pub P, pub &'static str, pub Rc<str>, pub C);
impl<D, P, C> Resource for ServePhp<D, P, C> {
type Artifact = PathArtifact;
}
@ -158,6 +189,25 @@ pub struct ServeService<D, P>(pub D, pub &'static str, pub P, pub P, pub P, pub
impl<D, P> Resource for ServeService<D, P> {
type Artifact = PathArtifact;
}
impl<D> ServeService<D, Rc<Path>> {
pub fn new(
domain: D,
service_name: &'static str,
exec: impl Into<Rc<Path>>,
static_path: impl Into<Rc<Path>>,
working_directory: impl Into<Rc<Path>>,
is_nodejs: bool,
) -> Self {
Self(
domain,
service_name,
exec.into(),
static_path.into(),
working_directory.into(),
is_nodejs,
)
}
}
#[derive(Debug, Hash, PartialEq, Eq)]
pub struct ServeRedir<D>(pub D, pub D);
@ -171,6 +221,12 @@ impl<D, P> Resource for ServeStatic<D, P> {
type Artifact = PathArtifact;
}
impl<D> ServeStatic<D, Rc<Path>> {
pub fn new(domain: D, path: impl Into<Rc<Path>>) -> Self {
Self(domain, path.into())
}
}
#[derive(Debug, Hash, PartialEq, Eq)]
pub struct DefaultServer;
impl Resource for DefaultServer {
@ -213,14 +269,26 @@ impl<P> Resource for WordpressPlugin<P> {
type Artifact = ();
}
impl WordpressPlugin<Rc<Path>> {
pub fn new(path: impl Into<Rc<Path>>, name: &'static str) -> Self {
Self(path.into(), name)
}
}
#[derive(Debug, Hash, PartialEq, Eq)]
pub struct WordpressTranslation<P>(pub P, pub &'static str, pub &'static str);
impl<P> Resource for WordpressTranslation<P> {
type Artifact = ();
}
impl WordpressTranslation<Rc<Path>> {
pub fn new(path: impl Into<Rc<Path>>, version: &'static str, lang: &'static str) -> Self {
Self(path.into(), version, lang)
}
}
#[derive(Debug, Hash, PartialEq, Eq)]
pub struct Cron<D>(pub D, pub String);
pub struct Cron<D>(pub D, pub Rc<str>);
impl<D> Resource for Cron<D> {
type Artifact = ();
}
@ -287,38 +355,38 @@ default_resources!(
Cron: Cron<D>,
Csr: Csr<D>,
DefaultServer: DefaultServer,
Dir: Dir<PathBuf>,
File: File<PathBuf>,
GitCheckout: GitCheckout<'a, PathBuf>,
Dir: Dir<Rc<Path>>,
File: File<Rc<Path>>,
GitCheckout: GitCheckout<'a, Rc<Path>>,
Key: Key<D>,
KeyAndCertBundle: KeyAndCertBundle<D>,
LoadedDirectory: LoadedDirectory<PathBuf>,
LoadedDirectory: LoadedDirectory<Rc<Path>>,
MariaDbDatabase: MariaDbDatabase<D>,
MariaDbUser: MariaDbUser<D>,
PostgresqlDatabase: PostgresqlDatabase<D>,
SystemdSocketService: SystemdSocketService<D, PathBuf>,
NpmInstall: NpmInstall<PathBuf>,
Owner: Owner<PathBuf>,
SystemdSocketService: SystemdSocketService<D, Rc<Path>>,
NpmInstall: NpmInstall<Rc<Path>>,
Owner: Owner<Rc<Path>>,
PhpFpmPool: PhpFpmPool<D>,
ServeCustom: ServeCustom<D>,
ServeService: ServeService<D, PathBuf>,
ServePhp: ServePhp<D, PathBuf, FpmPoolConfig>,
ServeService: ServeService<D, Rc<Path>>,
ServePhp: ServePhp<D, Rc<Path>, FpmPoolConfig>,
ServeRedir: ServeRedir<D>,
ServeStatic: ServeStatic<D, PathBuf>,
StoredDirectory: StoredDirectory<PathBuf>,
ServeStatic: ServeStatic<D, Rc<Path>>,
StoredDirectory: StoredDirectory<Rc<Path>>,
User: User,
UserForDomain: UserForDomain<D>,
WordpressPlugin: WordpressPlugin<PathBuf>,
WordpressTranslation: WordpressTranslation<PathBuf>,
WordpressPlugin: WordpressPlugin<Rc<Path>>,
WordpressTranslation: WordpressTranslation<Rc<Path>>,
);
pub fn serve_php<D, P: Into<PathBuf>, C: Into<FpmPoolConfig>>(
pub fn serve_php<D, C: Into<FpmPoolConfig>>(
domain: D,
path: P,
path: impl Into<Rc<Path>>,
root_filename: &'static str,
nginx_config: impl Into<String>,
nginx_config: impl Into<Rc<str>>,
pool_config: C,
) -> ServePhp<D, PathBuf, FpmPoolConfig> {
) -> ServePhp<D, Rc<Path>, FpmPoolConfig> {
ServePhp(
domain,
path.into(),