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
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)