feat: fix minor bugs

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
2024-08-24 14:55:26 +02:00
parent c2b7e44ea3
commit 1ba6cf79c0
12 changed files with 405 additions and 21 deletions

View File

@@ -1,11 +1,13 @@
use std::path::{Path, PathBuf};
use fs_extra::dir::CopyOptions;
use serde::Deserialize;
use crate::project::{self, ProjectPlan};
pub const CUDDLE_PLAN_FOLDER: &str = "plan";
pub const CUDDLE_PROJECT_WORKSPACE: &str = ".cuddle";
pub const CUDDLE_PLAN_FILE: &str = "cuddle.plan.toml";
pub trait PlanPathExt {
fn plan_path(&self) -> PathBuf;
@@ -19,8 +21,60 @@ impl PlanPathExt for project::ProjectPlan {
}
}
pub struct Plan {}
pub struct RawPlan {
pub config: RawPlanConfig,
pub root: PathBuf,
}
impl RawPlan {
pub fn new(config: RawPlanConfig, root: &Path) -> Self {
Self {
config,
root: root.to_path_buf(),
}
}
pub fn from_file(content: &str, root: &Path) -> anyhow::Result<Self> {
let config: RawPlanConfig = toml::from_str(content)?;
Ok(Self::new(config, root))
}
pub async fn from_path(path: &Path) -> anyhow::Result<Self> {
let cuddle_file = path.join(CUDDLE_PLAN_FILE);
tracing::trace!(
path = cuddle_file.display().to_string(),
"searching for cuddle.toml project file"
);
if !cuddle_file.exists() {
anyhow::bail!("no cuddle.toml project file found");
}
let cuddle_plan_file = tokio::fs::read_to_string(cuddle_file).await?;
Self::from_file(&cuddle_plan_file, path)
}
}
#[derive(Debug, Clone, Deserialize, PartialEq)]
pub struct RawPlanConfig {
pub plan: RawPlanConfigSection,
}
#[derive(Debug, Clone, Deserialize, PartialEq)]
pub struct RawPlanConfigSection {
pub schema: Option<RawPlanSchema>,
}
#[derive(Debug, Clone, Deserialize, PartialEq)]
#[serde(untagged)]
pub enum RawPlanSchema {
Nickel { nickel: PathBuf },
JsonSchema { jsonschema: String },
}
pub struct Plan {}
impl Plan {
pub fn new() -> Self {
Self {}
@@ -113,8 +167,61 @@ impl Plan {
.copy_inside(false),
)?;
todo!()
Ok(ClonedPlan {})
}
}
pub struct ClonedPlan {}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_can_parse_schema_plan() -> anyhow::Result<()> {
let plan = RawPlan::from_file(
r##"
[plan]
schema = {nickel = "contract.ncl"}
"##,
&PathBuf::new(),
)?;
assert_eq!(
RawPlanConfig {
plan: RawPlanConfigSection {
schema: Some(RawPlanSchema::Nickel {
nickel: "contract.ncl".into()
}),
}
},
plan.config,
);
Ok(())
}
#[test]
fn test_can_parse_json_schema() -> anyhow::Result<()> {
let plan = RawPlan::from_file(
r##"
[plan]
schema = {jsonschema = "schema.json"}
"##,
&PathBuf::new(),
)?;
assert_eq!(
RawPlanConfig {
plan: RawPlanConfigSection {
schema: Some(RawPlanSchema::JsonSchema {
jsonschema: "schema.json".into()
}),
}
},
plan.config,
);
Ok(())
}
}