-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from sdttttt/develop
Feature: grc configuration file parsing.
- Loading branch information
Showing
16 changed files
with
346 additions
and
92 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
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 |
---|---|---|
@@ -1,4 +1,3 @@ | ||
{ | ||
"editor.fontSize": 17, | ||
"editor.fontFamily": "Consolas,Fira Code, Cascadia Code, 'Courier New', monospace" | ||
"editor.fontFamily": "Consolas, 'Courier New', monospace" | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "grc" | ||
version = "0.8.1" | ||
version = "0.9.0-rc1" | ||
authors = ["sdttttt <[email protected]>"] | ||
description = "Similar to git-cz, gcr will help you to provide a better Git experience." | ||
edition = "2018" | ||
|
@@ -10,8 +10,13 @@ keywords = ["git-cz", "git", "cli"] | |
categories = ["command-line-utilities"] | ||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[profile.release] | ||
lto = "fat" | ||
|
||
[dependencies] | ||
clap = "2.33.3" | ||
console = "0.13.0" | ||
git2 = "0.13" | ||
dialoguer = "0.6.2" | ||
toml = "0.5.7" | ||
serde = { version = "1.0.82", features = ["derive"] } |
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 @@ | ||
type = [ "123" ] |
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 @@ | ||
type = [ "version: version is change." ] |
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 |
---|---|---|
|
@@ -11,7 +11,7 @@ build-dev: fmt | |
|
||
.PHONY: run | ||
run: fmt | ||
$(c) run -a . | ||
$(c) run | ||
|
||
.PHONY: test | ||
test: fmt | ||
|
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,2 @@ | ||
use_small_heuristics = "Max" | ||
merge_derives = false |
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,102 @@ | ||
use std::fs; | ||
use std::io::{Error, ErrorKind}; | ||
|
||
use serde::Deserialize; | ||
|
||
use crate::metadata::GRC_CONFIG_FILE_NAME; | ||
|
||
#[derive(Deserialize)] | ||
pub struct Extensions { | ||
#[serde(rename = "type")] | ||
typ: Vec<String>, | ||
} | ||
|
||
impl Extensions { | ||
pub fn from_agreement() -> Result<Self, Error> { | ||
let file_str = Self::read_config_file(GRC_CONFIG_FILE_NAME)?; | ||
Ok(Self::deserialize(file_str)?) | ||
} | ||
|
||
pub fn from(filename: &str) -> Result<Self, Error> { | ||
let file_str = Self::read_config_file(filename)?; | ||
Ok(Self::deserialize(file_str)?) | ||
} | ||
|
||
pub fn types(&self) -> &Vec<String> { | ||
&self.typ | ||
} | ||
|
||
fn deserialize(file_str: String) -> Result<Self, Error> { | ||
if file_str.len() == 0 || file_str == "" { | ||
return Ok(Self { typ: vec![] }); | ||
} | ||
|
||
let config = toml::from_str::<Extensions>(file_str.as_str())?; | ||
Ok(config) | ||
} | ||
|
||
fn read_config_file(filename: &str) -> Result<String, Error> { | ||
match fs::read_to_string(filename) { | ||
Ok(content) => Ok(content), | ||
Err(e) => { | ||
if e.kind() == ErrorKind::NotFound { | ||
Ok(String::new()) | ||
} else { | ||
Err(e) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
|
||
use super::*; | ||
|
||
const GRC_TEST_CONFIG_FILE_NAME: &str = "grc.test.toml"; | ||
|
||
const GRC_TOML_CONTENT: &str = r#"type = [ "version: version is change." ]"#; | ||
const GRC_TOML_TYPE: &str = "version: version is change."; | ||
|
||
const GRC_TEST_TOML_CONTENT: &str = r#"type = [ "123" ]"#; | ||
const GRC_TEST_TOML_TYPE: &str = "123"; | ||
|
||
#[test] | ||
fn it_read_config_file() { | ||
let file_str = Extensions::read_config_file(GRC_TEST_CONFIG_FILE_NAME).unwrap(); | ||
assert_eq!(file_str.as_str(), GRC_TEST_TOML_CONTENT); | ||
|
||
let file_str2 = Extensions::read_config_file("nullfile").unwrap(); | ||
assert_eq!(file_str2.len(), 0); | ||
assert_eq!(file_str2.as_str(), ""); | ||
|
||
let config = Extensions::read_config_file(GRC_CONFIG_FILE_NAME).unwrap(); | ||
assert_eq!(config.as_str(), GRC_TOML_CONTENT); | ||
} | ||
|
||
#[test] | ||
fn it_deserialize() { | ||
let file_str = String::from(GRC_TOML_CONTENT); | ||
let result = Extensions::deserialize(file_str).unwrap(); | ||
|
||
let types = result.typ; | ||
assert_eq!(types[0], GRC_TOML_TYPE); | ||
} | ||
|
||
#[test] | ||
fn it_from_agreement() { | ||
let config = Extensions::from_agreement().unwrap(); | ||
let types = config.typ; | ||
|
||
assert_eq!(types[0], GRC_TOML_TYPE); | ||
} | ||
|
||
#[test] | ||
fn it_from() { | ||
let config = Extensions::from(GRC_TEST_CONFIG_FILE_NAME).unwrap(); | ||
let types = config.typ; | ||
|
||
assert_eq!(types[0], GRC_TEST_TOML_TYPE); | ||
} | ||
} |
Oops, something went wrong.