use std::path::PathBuf;
use std::sync::Arc;
use anyhow::{Context, Result};
use http_types::Url;
use crate::config::{ConfigOptsBuild, ConfigOptsClean, ConfigOptsProxy, ConfigOptsServe, ConfigOptsWatch};
#[derive(Clone, Debug)]
pub struct RtcBuild {
pub target: PathBuf,
pub release: bool,
pub dist: PathBuf,
pub public_url: String,
}
impl RtcBuild {
pub(super) fn new(opts: ConfigOptsBuild) -> Result<Self> {
let pre_target = opts.target.clone().unwrap_or_else(|| "index.html".into());
let target = pre_target
.canonicalize()
.with_context(|| format!("error getting canonical path to source HTML file {:?}", &pre_target))?;
let target_parent_dir = target
.parent()
.map(|path| path.to_owned())
.unwrap_or_else(|| PathBuf::from(std::path::MAIN_SEPARATOR.to_string()));
Ok(Self {
target,
release: opts.release,
dist: opts.dist.unwrap_or_else(|| target_parent_dir.join("dist")),
public_url: opts.public_url.unwrap_or_else(|| "/".into()),
})
}
}
#[derive(Clone, Debug)]
pub struct RtcWatch {
pub build: Arc<RtcBuild>,
pub ignore: Vec<PathBuf>,
}
impl RtcWatch {
pub(super) fn new(build_opts: ConfigOptsBuild, opts: ConfigOptsWatch) -> Result<Self> {
let build = Arc::new(RtcBuild::new(build_opts)?);
Ok(Self {
build,
ignore: opts.ignore.unwrap_or_default(),
})
}
}
#[derive(Clone, Debug)]
pub struct RtcServe {
pub watch: Arc<RtcWatch>,
pub port: u16,
pub open: bool,
pub proxy_backend: Option<Url>,
pub proxy_rewrite: Option<String>,
pub proxies: Option<Vec<ConfigOptsProxy>>,
}
impl RtcServe {
pub(super) fn new(
build_opts: ConfigOptsBuild, watch_opts: ConfigOptsWatch, opts: ConfigOptsServe, proxies: Option<Vec<ConfigOptsProxy>>,
) -> Result<Self> {
let watch = Arc::new(RtcWatch::new(build_opts, watch_opts)?);
Ok(Self {
watch,
port: opts.port.unwrap_or(8080),
open: opts.open,
proxy_backend: opts.proxy_backend,
proxy_rewrite: opts.proxy_rewrite,
proxies,
})
}
}
#[derive(Clone, Debug)]
pub struct RtcClean {
pub dist: PathBuf,
pub cargo: bool,
}
impl RtcClean {
pub(super) fn new(opts: ConfigOptsClean) -> Result<Self> {
Ok(Self {
dist: opts.dist.unwrap_or_else(|| "dist".into()),
cargo: opts.cargo,
})
}
}