Skip to content

Commit

Permalink
feat: Support configuration with Cargo.toml using package.metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
attakei committed Apr 4, 2024
1 parent 49bd0b4 commit c1a5fb3
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use serde::{Deserialize, Serialize};
use std::path::PathBuf;

mod age_toml;
mod cargo_toml;

pub const DEFAULT_FILENAME: &'static str = ".age.toml";

Expand Down Expand Up @@ -33,17 +34,20 @@ pub trait ParseAvaliable {
#[derive(Debug)]
pub enum ConfigDocument {
AgeToml(age_toml::Property),
CargoToml(cargo_toml::Property),
}

impl ConfigDocument {
pub fn filename(&self) -> String {
match self {
Self::AgeToml(_) => age_toml::FILENAME.to_string(),
Self::CargoToml(_) => cargo_toml::FILENAME.to_string(),
}
}
pub fn update_version(&mut self, version: &Version) -> Result<()> {
match self {
Self::AgeToml(props) => props.update_version(version),
Self::CargoToml(props) => props.update_version(version),
}
}
}
Expand All @@ -64,5 +68,20 @@ pub fn resolve_config(root: &PathBuf) -> Result<(ConfigDocument, Config)> {
if _age_toml.is_ok() {
return _age_toml;
}
let _cargo_toml = '_cargo_toml: {
let doc = cargo_toml::Property::new(root);
if doc.is_err() {
break '_cargo_toml Err(doc.unwrap_err());
}
let doc = doc.unwrap();
let config = doc.get_config();
if config.is_err() {
break '_cargo_toml Err(config.unwrap_err());
}
Ok((ConfigDocument::CargoToml(doc), config.unwrap()))
};
if _cargo_toml.is_ok() {
return _cargo_toml;
}
Err(anyhow!("Valid configuration file is not exists."))
}
61 changes: 61 additions & 0 deletions src/config/cargo_toml.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use std::fs::{read_to_string, File};
use std::io::prelude::*;
use std::path::PathBuf;

use anyhow::{anyhow, Result};
use toml_edit::{value, DocumentMut};

use super::{Config, ParseAvaliable};

pub const FILENAME: &'static str = "Cargo.toml";

#[derive(Debug)]
pub struct Property {
pub filepath: PathBuf,
pub doc: DocumentMut,
}

impl ParseAvaliable for Property {
fn new(root: &PathBuf) -> Result<Self> {
let filepath = root.join(FILENAME);
if !filepath.exists() {
return Err(anyhow!("Configuration file is not found."));
}
let source = read_to_string(&filepath);
if source.is_err() {
return Err(anyhow!("Configuration file cannot access."));
}
let doc = source.unwrap().parse::<DocumentMut>();
if doc.is_err() {
return Err(anyhow!("Configuration is not valid TOML."));
}

Ok(Property {
filepath,
doc: doc.unwrap(),
})
}

fn get_config(&self) -> Result<Config> {
let mut item = self.doc.as_item();
for k in ["package", "metadata", "age"] {
let child = item.get(k);
if child.is_none() || !child.unwrap().is_table() {
return Err(anyhow!("It does not have valid values."));
}
item = child.unwrap();
}
let config = toml::from_str::<Config>(item.to_string().as_str());
if config.is_err() {
return Err(anyhow!(config.unwrap_err()));
}
Ok(config.unwrap())
}

fn update_version(&mut self, version: &semver::Version) -> Result<()> {
self.doc["package"]["metadata"]["age"]["current_version"] = value(version.to_string());
let mut out = File::create(&self.filepath)?;
let _ = out.write(self.doc.to_string().as_bytes());
Ok(())
}
}
12 changes: 12 additions & 0 deletions tests/return-0/no-target-cargo-toml/after/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "dummy"
version = "0.1.0"
edition = "2021"

[package.metadata.age]
current_version = "0.2.0"
files = []

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

[dependencies]
12 changes: 12 additions & 0 deletions tests/return-0/no-target-cargo-toml/before/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "dummy"
version = "0.1.0"
edition = "2021"

[package.metadata.age]
current_version = "0.1.0"
files = []

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

[dependencies]

0 comments on commit c1a5fb3

Please sign in to comment.