From 6cb65e55c1d1212aa31404ef75d2d02af0ee1616 Mon Sep 17 00:00:00 2001 From: kjuulh Date: Fri, 23 Aug 2024 23:22:35 +0200 Subject: [PATCH] refactor: extract file Signed-off-by: kjuulh --- crates/cuddle/src/main.rs | 76 +----------------------------------- crates/cuddle/src/project.rs | 71 +++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 74 deletions(-) create mode 100644 crates/cuddle/src/project.rs diff --git a/crates/cuddle/src/main.rs b/crates/cuddle/src/main.rs index 499e212..f96336d 100644 --- a/crates/cuddle/src/main.rs +++ b/crates/cuddle/src/main.rs @@ -1,3 +1,5 @@ +mod project; + use project::Project; #[tokio::main] @@ -21,77 +23,3 @@ impl Cuddle { Ok(Self { project }) } } - -mod project { - use std::env::current_dir; - - use serde::Deserialize; - - const CUDDLE_FILE_NAME: &str = "cuddle.toml"; - - pub struct Project { - config: Config, - } - - impl Project { - pub fn new(config: Config) -> Self { - Self { config } - } - - pub fn from_file(content: &str) -> anyhow::Result { - let config: Config = toml::from_str(&content)?; - - Ok(Self::new(config)) - } - - pub async fn from_current_path() -> anyhow::Result> { - let cur_dir = current_dir()?; - let cuddle_file = cur_dir.join(CUDDLE_FILE_NAME); - - if !cuddle_file.exists() { - // We may want to recursively search for the file (towards root) - return Ok(None); - } - - let cuddle_project_file = tokio::fs::read_to_string(cuddle_file).await?; - - Ok(Some(Self::from_file(&cuddle_project_file)?)) - } - } - - #[derive(Debug, Clone, Deserialize, PartialEq)] - pub struct Config { - project: ProjectConfig, - } - - #[derive(Debug, Clone, Deserialize, PartialEq)] - pub struct ProjectConfig { - name: String, - } - - #[cfg(test)] - mod tests { - use super::*; - - #[test] - fn test_can_parse_simple_file() -> anyhow::Result<()> { - let project = Project::from_file( - r##" -[project] -name = "simple_file" - "##, - )?; - - assert_eq!( - Config { - project: ProjectConfig { - name: "simple_file".into() - } - }, - project.config - ); - - Ok(()) - } - } -} diff --git a/crates/cuddle/src/project.rs b/crates/cuddle/src/project.rs new file mode 100644 index 0000000..f806108 --- /dev/null +++ b/crates/cuddle/src/project.rs @@ -0,0 +1,71 @@ +use std::env::current_dir; + +use serde::Deserialize; + +const CUDDLE_FILE_NAME: &str = "cuddle.toml"; + +pub struct Project { + config: Config, +} + +impl Project { + pub fn new(config: Config) -> Self { + Self { config } + } + + pub fn from_file(content: &str) -> anyhow::Result { + let config: Config = toml::from_str(&content)?; + + Ok(Self::new(config)) + } + + pub async fn from_current_path() -> anyhow::Result> { + let cur_dir = current_dir()?; + let cuddle_file = cur_dir.join(CUDDLE_FILE_NAME); + + if !cuddle_file.exists() { + // We may want to recursively search for the file (towards root) + return Ok(None); + } + + let cuddle_project_file = tokio::fs::read_to_string(cuddle_file).await?; + + Ok(Some(Self::from_file(&cuddle_project_file)?)) + } +} + +#[derive(Debug, Clone, Deserialize, PartialEq)] +pub struct Config { + project: ProjectConfig, +} + +#[derive(Debug, Clone, Deserialize, PartialEq)] +pub struct ProjectConfig { + name: String, +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_can_parse_simple_file() -> anyhow::Result<()> { + let project = Project::from_file( + r##" +[project] +name = "simple_file" + "##, + )?; + + assert_eq!( + Config { + project: ProjectConfig { + name: "simple_file".into() + } + }, + project.config + ); + + Ok(()) + } +}