Also move some associated methods to the 'Config' module

And refactor the 'new' command implementation.
This commit is contained in:
KtorZ 2023-01-14 22:09:27 +01:00
parent 0771ab24bd
commit d4f905b1db
No known key found for this signature in database
GPG Key ID: 33173CB6F77F4277
2 changed files with 41 additions and 28 deletions

View File

@ -1,13 +1,18 @@
use crate::{package_name::PackageName, Error};
use aiken_lang::ast::Span;
use miette::NamedSource;
use serde::{de::Visitor, Deserialize, Serialize};
use std::{fmt::Display, fs, path::PathBuf};
use serde::{Deserialize, Serialize};
use std::{
fmt::Display,
fs, io,
path::{Path, PathBuf},
};
#[derive(Deserialize)]
#[derive(Deserialize, Serialize)]
pub struct Config {
pub name: PackageName,
pub version: String,
pub license: String,
#[serde(default)]
pub description: String,
pub repository: Option<Repository>,
@ -15,7 +20,7 @@ pub struct Config {
pub dependencies: Vec<Dependency>,
}
#[derive(Deserialize)]
#[derive(Deserialize, Serialize)]
pub struct Repository {
pub user: String,
pub project: String,
@ -48,6 +53,34 @@ impl Display for Platform {
}
impl Config {
pub fn default(name: &PackageName) -> Self {
Config {
name: name.clone(),
version: "0.0.0".to_string(),
license: "Apache-2.0".to_string(),
description: format!("Aiken contracts for project '{name}'"),
repository: Some(Repository {
user: name.owner.clone(),
project: name.repo.clone(),
platform: Platform::Github,
}),
dependencies: vec![Dependency {
name: PackageName {
owner: "aiken-lang".to_string(),
repo: "stdlib".to_string(),
},
version: "main".to_string(),
source: Platform::Github,
}],
}
}
pub fn save(&self, dir: &Path) -> Result<(), io::Error> {
let aiken_toml_path = dir.join("aiken.toml");
let aiken_toml = toml::to_string_pretty(self).unwrap();
fs::write(aiken_toml_path, aiken_toml)
}
pub fn load(dir: PathBuf) -> Result<Config, Error> {
let config_path = dir.join("aiken.toml");
let raw_config = fs::read_to_string(&config_path)

View File

@ -43,10 +43,12 @@ fn create_project(args: Args, package_name: &PackageName) -> miette::Result<()>
create_validators_folder(&root)?;
}
aiken_toml(&root, package_name)?;
readme(&root, &package_name.repo)?;
Config::default(package_name)
.save(&root)
.into_diagnostic()?;
gitignore(&root)?;
Ok(())
@ -84,28 +86,6 @@ fn create_validators_folder(root: &Path) -> miette::Result<()> {
Ok(())
}
fn aiken_toml(root: &Path, package_name: &PackageName) -> miette::Result<()> {
let aiken_toml_path = root.join("aiken.toml");
fs::write(
aiken_toml_path,
formatdoc! {
r#"
name = "{name}"
version = "0.0.0"
licences = ["Apache-2.0"]
description = "Aiken contracts for project '{name}'"
dependencies = [
{{ name = "aiken-lang/stdlib", version = "main", source = "github" }},
]
"#,
name = package_name.to_string(),
},
)
.into_diagnostic()
}
fn readme(root: &Path, project_name: &str) -> miette::Result<()> {
fs::write(
root.join("README.md"),