-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support configuration with Cargo.toml using package.metadata
- Loading branch information
Showing
4 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |