Crates.io | guidon |
lib.rs | guidon |
version | 0.4.1 |
source | src |
created_at | 2020-04-22 11:47:42.138681 |
updated_at | 2021-05-01 08:37:19.076051 |
description | Library to initialize project from templates |
homepage | https://gitlab.com/rs-guidon/guidon |
repository | https://gitlab.com/rs-guidon/guidon |
max_upload_size | |
id | 232887 |
size | 61,295 |
guidon performs templating based on handlebars templating system.
Files to be handles needs to have an .hbs
extension. Folders and files names can be templatized too : {{folder/subfolder}}
The entry point is the Guidon structure.
let mut guidon = Guidon::new(PathBuf::from("path/to/template/dir"));
let mut vars = BTreeMap::new();
vars.insert("key1".to_string(), "value1".to_string());
guidon.variables(vars);
guidon.apply_template("path/to/destination").unwrap();
With this initialization:
path/to/template/dir/template
path/to/destination
guidon implements a TryNew trait to initialize from a dir or a git repo
let mut guidon = Guidon::try_new("path/to/template/dir").unwrap();
With this initialization:
path/to/template/dir/variables.toml
path/to/template/dir/template
let mut guidon = Guidon::try_new("path/to/template/dir/my_vars.toml").unwrap();
With this initialization:
path/to/template/dir/my_vars.toml
path/to/template/dir
Guidon can be provided with specific template file and directory to parse.
let custom = CustomTemplate {
dir_path: Path::new("path/to/template/dir"),
template_path: Path::new("path/to/my_vars.toml")
};
let mut guidon = Guidon::try_new(custom);
let git = GitOptions::builder()
.repo("url/to/repo")
.build()
.unwrap();
let mut guidon = Guidon::try_new(git);
With this initialization
tmp/dir/template.toml
tmp/dir/template
The git repo url could be in the form :
http[s]://[user[:password]]@uri/repo[.git]
where user and password must be part of the urlgit@uri:repo.git
. The key used must be loaded in an ssh-agent instanceThe configuration file is structured as follows :
# Key value pairs for template substitution
[variables]
test1 = "test 1"
test2 = "test 2"
replace
. It's simply replace a string by another in the value.
Tell me: {{replace my_var everybody world}}
with my_var="Hello everybody !
will render as
Tell me: Hello world !
.append
. Simply append a string to the value.
Tell me: {{append my_var "and everybody !"}}
with my_var="Hello world"
will render as
Tell me: Hello world and everybody !
.prepend
. Prepend a string to the value.
Tell me: {{prepend my_var "Sure, "}}
with my_var="hello world"
will render as
Tell me: Sure, hello world
.up
: Uppercase the valuelow
: Lowercase the valuedefault
: default to given value if input is null or empty
Tell me: {{default my_var "Oops"}}
with my_var
not defined
will render as
Tell me: Oops
encrypt
: Encrypt the data. The key must be provideddecrypt
: Decrypt the dataencrypt and decrypt helpers are provide with the
crypto
feature.
guidon offers the possiblity to provide two callbacks :
These callbacks could be use for user interaction, or default behaviour.
In this example, the callback will add " cb"
to every value.
let cb = |h: &mut BTreeMap<String, String>| {
h.iter_mut().for_each(|(_, v)| *v +=" cb");
};
let mut guidon = Guidon::new(PathBuf::from("template/path"));
guidon.set_variables_callback(cb);
In this example, the callback will replace any missing value by the key concatenated with "-cb"
// this callback will add `-cb` to the key as a value
let cb = |h: String| {
let mut s = h.clone();
s.push_str("-cb");
s
};
let mut guidon = Guidon::new(PathBuf::from("template/path"));
guidon.set_render_callback(cb);
Filnames and directory can also be templatized.
Super-{{my_var}}-content.txt
will render as Super-boring-content.txt
given my_var="boring"
.
If the content of the file is templatized, we have Super-{{my_var}}-content.txt.hbs
.//!
guidon uses the log facade.
This project is licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in guidon by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
Generated with cargo readme