-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add template formatting for hook (#83)
- Loading branch information
Showing
7 changed files
with
175 additions
and
88 deletions.
There are no files selected for viewing
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
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
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,76 +1,95 @@ | ||
use material_colors::color::Argb; | ||
use regex::{Captures, Regex}; | ||
use upon::Engine; | ||
use upon::{Engine, Syntax, Template, Value}; | ||
|
||
use super::color::{format_hex, rgb_from_argb}; | ||
// use regex::{Captures, Regex}; | ||
// use material_colors::color::Argb; | ||
// use super::color::{format_hex, rgb_from_argb}; | ||
|
||
pub enum Variables { | ||
Invalid, | ||
ComparedColor, | ||
SourceImage, | ||
SourceColor, | ||
} | ||
// pub enum Variables { | ||
// Invalid, | ||
// ComparedColor, | ||
// SourceImage, | ||
// SourceColor, | ||
// } | ||
|
||
impl Variables { | ||
fn from(mut input: &str) -> Self { | ||
if input.starts_with("{") && input.ends_with("}") { | ||
input = input.remove_first_char().remove_last_char(); | ||
} | ||
// impl Variables { | ||
// fn from(mut input: &str) -> Self { | ||
// if input.starts_with("{") && input.ends_with("}") { | ||
// input = input.remove_first_char().remove_last_char(); | ||
// } | ||
|
||
match input { | ||
"compared_color" => Variables::ComparedColor, | ||
"source_image" => Variables::SourceImage, | ||
"source_color" => Variables::SourceColor, | ||
_ => { | ||
error!("Invalid variable: {{{}}}", input); | ||
Variables::Invalid | ||
} | ||
} | ||
} | ||
} | ||
// match input { | ||
// "compared_color" => Variables::ComparedColor, | ||
// "source_image" => Variables::SourceImage, | ||
// "source_color" => Variables::SourceColor, | ||
// _ => { | ||
// error!("Invalid variable: {{{}}}", input); | ||
// Variables::Invalid | ||
// } | ||
// } | ||
// } | ||
// } | ||
|
||
trait StrExt { | ||
fn remove_last_char(&self) -> &str; | ||
fn remove_first_char(&self) -> &str; | ||
} | ||
// trait StrExt { | ||
// fn remove_last_char(&self) -> &str; | ||
// fn remove_first_char(&self) -> &str; | ||
// } | ||
|
||
impl StrExt for str { | ||
fn remove_last_char(&self) -> &str { | ||
match self.char_indices().next_back() { | ||
Some((i, _)) => &self[..i], | ||
None => self, | ||
} | ||
} | ||
fn remove_first_char(&self) -> &str { | ||
self.chars() | ||
.next() | ||
.map(|c| &self[c.len_utf8()..]) | ||
.unwrap_or("") | ||
} | ||
} | ||
// impl StrExt for str { | ||
// fn remove_last_char(&self) -> &str { | ||
// match self.char_indices().next_back() { | ||
// Some((i, _)) => &self[..i], | ||
// None => self, | ||
// } | ||
// } | ||
// fn remove_first_char(&self) -> &str { | ||
// self.chars() | ||
// .next() | ||
// .map(|c| &self[c.len_utf8()..]) | ||
// .unwrap_or("") | ||
// } | ||
// } | ||
|
||
// pub fn replace_hook_keywords( | ||
// input: &str, | ||
// default_value: &String, | ||
// src_img: Option<&String>, | ||
// compared_color: Option<&String>, | ||
// source_color: &Argb, | ||
// ) -> String { | ||
// let re = Regex::new(r"\{.*?\}").unwrap(); | ||
|
||
// let source_formatted = format_hex(&rgb_from_argb(*source_color)); | ||
|
||
pub fn replace_hook_keywords( | ||
input: &str, | ||
default_value: &String, | ||
src_img: Option<&String>, | ||
compared_color: Option<&String>, | ||
source_color: &Argb, | ||
) -> String { | ||
let re = Regex::new(r"\{.*?\}").unwrap(); | ||
// let result = re.replace_all(input, |cap: &Captures| { | ||
// match Variables::from(&cap[0]) { | ||
// Variables::Invalid => &cap[0], | ||
// Variables::ComparedColor => compared_color.unwrap_or(default_value), | ||
// Variables::SourceImage => src_img.unwrap_or(default_value), | ||
// Variables::SourceColor => &source_formatted, | ||
// } | ||
// .to_string() | ||
// }); | ||
|
||
let source_formatted = format_hex(&rgb_from_argb(*source_color)); | ||
// return result.to_string(); | ||
// } | ||
|
||
let result = re.replace_all(input, |cap: &Captures| { | ||
match Variables::from(&cap[0]) { | ||
Variables::Invalid => &cap[0], | ||
Variables::ComparedColor => compared_color.unwrap_or(default_value), | ||
Variables::SourceImage => src_img.unwrap_or(default_value), | ||
Variables::SourceColor => &source_formatted, | ||
pub fn format_hook_text(render_data: &mut Value, compared_color: Option<&String>, template: Template<'_>) -> String { | ||
let syntax = Syntax::builder().expr("{{", "}}").block("<*", "*>").build(); | ||
let mut engine = Engine::with_syntax(syntax); | ||
match render_data { | ||
Value::Map(ref mut map) => { | ||
if compared_color.is_some() { | ||
map.insert("compared_color".to_string(), Value::from(compared_color.unwrap().as_str())); | ||
} | ||
}, | ||
_ => { | ||
println!("not") | ||
} | ||
.to_string() | ||
}); | ||
} | ||
|
||
return result.to_string(); | ||
} | ||
let data = template | ||
.render(&engine,&render_data) | ||
.to_string().unwrap(); | ||
|
||
pub fn format_hook_text(mut engine: Engine) {} | ||
return data | ||
} |