Skip to content

Commit

Permalink
Rust scaffolding utils
Browse files Browse the repository at this point in the history
  • Loading branch information
guillemcordoba committed May 20, 2024
1 parent ff06375 commit 8b14b15
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 3 deletions.
57 changes: 54 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions crates/rust_scaffolding_utils/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[package]
name = "rust_scaffolding_utils"
version = "0.1.0"
edition = "2021"

[lib]
name = "rust_scaffolding_utils"
path = "src/lib.rs"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = "1.0.81"
ignore = "0.4"
toml = "0.8"
serde = "1"
clap = {version = "4.5.4", features = ["derive"]}
regex = "1.10.4"
thiserror = "1.0.58"
file_tree_utils = { path = "../file_tree_utils" }
build-fs-tree = "0.4"
dialoguer = "0.11"
colored = "2.1.0"
91 changes: 91 additions & 0 deletions crates/rust_scaffolding_utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
use std::path::PathBuf;

use dialoguer::{theme::ColorfulTheme, Select};
use file_tree_utils::{
file_content, file_exists, find_files_by_name, map_file, FileTree, FileTreeError,
};
use thiserror::Error;

#[derive(Error, Debug)]
pub enum RustScaffoldingUtilsError {
#[error(transparent)]
FileTreeError(#[from] FileTreeError),

#[error(transparent)]
DialoguerError(#[from] dialoguer::Error),

#[error("TOML deserialization error: {0}")]
TomlDeError(#[from] toml::de::Error),

#[error("TOML serialization error: {0}")]
TomlSerError(#[from] toml::ser::Error),

#[error("Malformed Cargo.toml {0}: {1}")]
MalformedCargoTomlError(PathBuf, String),
}

pub fn add_member_to_workspace(
cargo_toml: &(PathBuf, String),
new_workspace_member: String,
) -> Result<String, RustScaffoldingUtilsError> {
let mut workspace_cargo_toml: toml::Value = toml::from_str(cargo_toml.1.as_str())?;

let workspace_table = workspace_cargo_toml
.as_table_mut()
.ok_or(RustScaffoldingUtilsError::MalformedCargoTomlError(
cargo_toml.0.clone(),
String::from("file does not conform to toml"),
))?
.get_mut("workspace")
.ok_or(RustScaffoldingUtilsError::MalformedCargoTomlError(
cargo_toml.0.clone(),
String::from("no workspace table found in workspace root"),
))?
.as_table_mut()
.ok_or(RustScaffoldingUtilsError::MalformedCargoTomlError(
cargo_toml.0.clone(),
String::from("workspace key is not a table"),
))?;

let members = workspace_table
.get_mut("members")
.ok_or(RustScaffoldingUtilsError::MalformedCargoTomlError(
cargo_toml.0.clone(),
String::from("should have a members field in the workspace table"),
))?
.as_array_mut()
.ok_or(RustScaffoldingUtilsError::MalformedCargoTomlError(
cargo_toml.0.clone(),
String::from("the members field in the workspace table should be an array"),
))?;

members.push(toml::Value::String(new_workspace_member));

let cargo_toml_str = toml::to_string(&workspace_cargo_toml)?;

Ok(cargo_toml_str)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn add_member_to_workspace_test() {
let cargo_toml = r#"[workspace]
members = ["tauri-plugin-holochain", "crates/*"]
resolver = "2"
"#;
assert_eq!(
add_member_to_workspace(
&(PathBuf::from("/"), cargo_toml.to_string()),
String::from("src-tauri")
)
.unwrap(),
r#"[workspace]
members = ["tauri-plugin-holochain", "crates/*", "src-tauri"]
resolver = "2"
"#
);
}
}
23 changes: 23 additions & 0 deletions crates/templates_scaffolding_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,26 @@ pub fn register_case_helpers<'a>(mut h: Handlebars<'a>) -> Handlebars<'a> {

h
}

#[cfg(test)]
mod tests {
use handlebars::{no_escape, Context, Handlebars};
use serde_json::json;

#[test]
fn test_render_with_quotes_quotes() {
let mut h = Handlebars::new();
h.register_escape_fn(no_escape);

let code = r#""With quotes""#;
let value = json!({"previous_file_content": code});
println!("value, {value:?}");
let context = Context::from(value);
let template = r#"{{previous_file_content}}"#;

assert_eq!(
h.render_template_with_context(template, &context).unwrap(),
code
);
}
}

0 comments on commit 8b14b15

Please sign in to comment.