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,28 +1,70 @@
use std::{env::current_dir, path::PathBuf};
use std::{
env::current_dir,
path::{Path, PathBuf},
};
use serde::Deserialize;
const CUDDLE_FILE_NAME: &str = "cuddle.toml";
pub const CUDDLE_PROJECT_FILE: &str = "cuddle.toml";
#[derive(Clone)]
pub struct RawProject {
config: RawConfig,
pub root: PathBuf,
}
impl RawProject {
pub fn new(config: RawConfig, root: &Path) -> Self {
Self {
config,
root: root.to_path_buf(),
}
}
pub fn from_file(content: &str, root: &Path) -> anyhow::Result<Self> {
let config: RawConfig = 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_PROJECT_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_project_file = tokio::fs::read_to_string(cuddle_file).await?;
Self::from_file(&cuddle_project_file, path)
}
}
#[derive(Clone)]
pub struct ProjectPlan {
config: Config,
config: ProjectPlanConfig,
pub root: PathBuf,
}
impl ProjectPlan {
pub fn new(config: Config, root: PathBuf) -> Self {
pub fn new(config: ProjectPlanConfig, root: PathBuf) -> Self {
Self { config, root }
}
pub fn from_file(content: &str, root: PathBuf) -> anyhow::Result<Self> {
let config: Config = toml::from_str(&content)?;
let config: ProjectPlanConfig = toml::from_str(content)?;
Ok(Self::new(config, root))
}
pub async fn from_current_path() -> anyhow::Result<Option<Self>> {
let cur_dir = current_dir()?;
let cuddle_file = cur_dir.join(CUDDLE_FILE_NAME);
let cuddle_file = cur_dir.join(CUDDLE_PROJECT_FILE);
tracing::trace!(
path = cuddle_file.display().to_string(),
@@ -60,7 +102,17 @@ pub enum Plan {
}
#[derive(Debug, Clone, Deserialize, PartialEq)]
pub struct Config {
pub struct RawConfig {
project: ProjectConfig,
}
#[derive(Debug, Clone, Deserialize, PartialEq)]
pub struct ProjectConfig {
name: String,
}
#[derive(Debug, Clone, Deserialize, PartialEq)]
pub struct ProjectPlanConfig {
plan: Option<PlanConfig>,
}
@@ -87,7 +139,7 @@ git = "https://github.com/kjuulh/some-cuddle-project"
)?;
assert_eq!(
Config {
ProjectPlanConfig {
plan: Some(PlanConfig::Git {
git: "https://github.com/kjuulh/some-cuddle-project".into()
})
@@ -108,7 +160,7 @@ plan = "https://github.com/kjuulh/some-cuddle-project"
)?;
assert_eq!(
Config {
ProjectPlanConfig {
plan: Some(PlanConfig::Bare(
"https://github.com/kjuulh/some-cuddle-project".into()
))
@@ -123,7 +175,7 @@ plan = "https://github.com/kjuulh/some-cuddle-project"
fn test_can_parse_simple_file_none() -> anyhow::Result<()> {
let project = ProjectPlan::from_file(r##""##, PathBuf::new())?;
assert_eq!(Config { plan: None }, project.config);
assert_eq!(ProjectPlanConfig { plan: None }, project.config);
Ok(())
}