[dependencies]
template_str = { git="https://github.com/CheeseGrinder/TemplateStr-Rust", tag="vX.X.X" }
use template_str::{TemplateStr, t_type::{VariableMap as VMap, TVal, FuncMap}, varmap, list_func, vecTval};let parser = TemplateStr::new(map, Some(vec));-
funcArray: is an array of functions passed to the constructor that can be called in the parsed text
let vec: FuncMap = list_func![test, test_type];
-
varMap: is a map of variables passed to the constructor that can be used in the parsed text
let map: VMap = varmap!{ "foo" => "bar", "str" => "Jame", "int" => 32, "float" => 4.2, "bool" => true, "Vec" => vecTval!["test", 42], "Map" => varmap!{ "value" => "Map in Map", }, "Map1" => varmap!{ "Map2" => varmap!{ "value" => "Map in Map in Map", }, }, };
let result = parser.parse(text)parse(text: String) -> Result<String, TmplError>: parse all (variable, function, condition and switch)parse_variable(text: String) -> Result<String, TmplError>: parse Variable ; ${variableName}parse_function(text: String) -> Result<String, TmplError>: parse Function and Custom Function ; @{functionName}parse_condition(text: String) -> Result<String, TmplError>: parse Condition ; #{value1 == value2; trueValue | falseValue}parse_switch(text: String) -> Result<String, TmplError>: parse Switch ; ?{var; value1::#0F0, value2::#00F, ..., _::#000}has_one(text: String) -> bool: check if there are one syntaxehas_variable(text: String) -> bool: check if there are any Variablehas_function(text: String) -> bool: check if there are any Functionhas_condition(text: String) -> bool: check if there are any Conditionhas_switch(text: String) -> bool: check if there are any Switch
-
Variable
The syntax of the Variables is like :
${variable}${Map.value}${MasterMap.SecondMap.value. ...}${variable[0]}
If the value does not exist an error is returned
//Example of parsing | is not code name = "Jame" "name is ${name}" parse() "name is Jame"
-
Function
The syntax of the Function is like :
@{function; parameter}@{function}
Here is a list of the basic functions available :
@{uppercase; variableName}@{uppercaseFirst; variableName}@{lowercase; variableName}@{swapcase; variableName}@{time}HH/mm/ss@{date}DD/MM/YYYY@{dateTime}DD/MM/YYYY HH/mm/ss
//Example of parsing | is not code name = "jame" "name is @{uppercase; name}" parse() "name is JAME" //================================= "what time is it ? it's @{time}" parse() "what time is it ? it's 15:30:29"
-
Custom Function
The syntax of Custom function is the same as the basic functions, they can have 0,1 or more parameters :
@{customFunction; param1 param2 variableName ...}@{customFunction}
The developer who adds his own function will have to document it
Syntaxe Typingcan be used at the parameter level of custom functionsFor developers :
- Parameters to be passed in a
list/vec/array - The custom function must necessarily return a
str/string
fn test(parameter: Vec<TVal>) -> String { // Your code return String; }
-
Condition
The syntax of the Condition is like :
#{value1 == value2; trueValue | falseValue}
comparator:
==!=<=*<*>=*>*
* for this comparator the type
stringandboolare modified :stringit's the number of characters that is compared ('text' = 4)boolit's the value in int that is compared (True = 1)
value1is compared withvalue2Syntaxe Typingcan be used atvalue1andvalue2level//Example of parsing | is not code name = "Jame" "Jame is equal to James ? #{name == 'James'; Yes | No}" parse() "Jame is equal to James ? No"
-
Switch
The syntax of the Switch is like :
?{variableName; value1::#0F0, value2::#00F, ..., _::#000}?{type/variableName; value1::#0F0, value2::#00F, ..., _::#000}
The value of
variableNameis compared with all thevalues*, if avalues*is equal to the value ofvariableNamethen the value after the::will be returned.
If novalues*matches, the value after_::is returnedyou can specify the type of
variableName, but don't useSyntaxe Typing.
If the type is specified then allvalues*will be typed with the same type.syntax to specify the type of
variableName:str/variableNameint/variableNamefloat/variableName
//Example of parsing | is not code name = "Jame" yearsOld = 36 "how old is Jame ? ?{name; Jame::42 years old, William::36 years old, _::I don't know}" parse() "how old is Jame ? 42 years old" //================================= "who at 36 years old ? ?{int/yearsOld; 42::Jame !, 36::William !, _::I don't know}" parse() "who at 42 years old ? William !"
Usable only in the custom function parameters and the two condition comparison values
| Format | Type | Return | Note |
|---|---|---|---|
| variableName | * |
value of variableName |
Is the key of the value in the dictionary pass to the constructor |
| b/True | Bool |
true | Type the string True as bool |
| i/123 | Int |
123 | Type the string 123 as type int |
| f/123.4 | Float |
123.4 | Type the string 123.4 as type float |
| "text" or 'text' or `text` | Str |
text | It just takes what's in quote, not to be interpreted as a variable name |
| ("test", i/56) | Vec |
[test 56] | Use typing for typed otherwise text will be used as variable name |
This function takes as parameters a Bool, Int and String
+ @{MyCustomFunction; b/True i/15 "foo"}
- @{MyCustomFunction; True 15 foo}
+ #{"test" == "test"; Yes | No}
+ #{"56" == i/56; Yes | No}
- #{foo == 56; Yes | No}
If you want another example you can look in the test file (tests/integration_test.rs)
- : Add exemple
- : Add test