pub struct TmplBuilder {
    pub pkg_name: String,
    pub pkg_type: Option<PkgType>,
    pub pkg_info: Option<PkgInfo>,
    pub deps: Option<Vec<String>>,
}
Expand description

The TemplateBuilder struct, which is used to build a Template

Fields§

§pkg_name: String§pkg_type: Option<PkgType>§pkg_info: Option<PkgInfo>§deps: Option<Vec<String>>

Implementations§

Initializes a new TmplBuilder with nothing but pkg_name set.

Initializes a new TmplBuilder from a PkgInfo. Useful for testing or as a shortcut

Gets the PkgType of the package of the TmplBuilder that’s passed into the method

Errors
  • If a package with the name of (self.pkg_name)[crate::TmplBuilder.pkg_name] can be found on multiple platforms (e.g. on both (crates.io)[https://crates.io] and (rubygems.org)[https://rubygems.org])
  • If the package can’t be found on any of the platforms

Sets the PkgType of the package of the TmplBuilder that’s passed into the method

Gets the PkgInfo of the package of the TmplBuilder that’s passed into the method

Errors
  • If you try to call this method without setting/getting pkg_type first via either (self.get_type)[crate::tmplwriter::TmplBuilder::get_type] or (self.set_type)[crate::tmplwriter::TmplBuilder::set_type]

Sets the PkgInfo of the package of the TmplBuilder that’s passed into the method

Gets the dependencies of the TmplBuilder that’s passed into the method

Errors:
  • If metacpan.org/rubygems.org can’t be reached
  • If the perldist/gem can’t be found on metacpan.org/rubygems.org

Checks if a Gem or PerlDist is built into Ruby/Perl.

Errors
  • If you try to call this method without setting/getting pkg_type first via either (self.get_type)[crate::tmplwriter::TmplBuilder::get_type] or (self.set_type)[crate::tmplwriter::TmplBuilder::set_type]

Helper method to get a Vec<Template> of all dependencies a package has. Also includes recursive dependencies.

Arguments

Takes the optional argument ‘tmpl_path’, which is used to check if a template exists already and as such not unnecessarily return a Template struct for a template we don’t need (because it exists already)

Errors:
  • If you try to call this method without actually getting the deps via get_deps first
  • If something went wrong while generating Templates for all dependencies of self.pkg_name

Updates a Template

Example
use libtmplgen::{PkgInfo, Template, TmplBuilder};
use std::fs::File;
use std::io::prelude::*;

// Do note that we only manually create a PkgInfo here to make the example easier to understand
let pkg_info_crate = PkgInfo {
       pkg_name: "tmplgen".to_string(),
       version: "0.6.0".to_string(),
       description: "Void Linux template generator for language-specific package managers"
           .to_string(),
       homepage: "https://github.com/Cogitri/tmplgen".to_string(),
       license: vec!["GPL-3.0-or-later".to_string()],
       dependencies: None,
       sha: "afc403bf69ad4da168938961b0f02da86ef29d655967cfcbacc8201e1327aff4".to_string(),
       download_url: Some(
           "https://static.crates.io/crates/tmplgen/tmplgen-${version}.crate".to_string(),
       ),
};

let mut old_template = Template { inner: String::new(), name: "tmplgen".to_string() };
// Open whatever file you want to below.
let mut file = File::open("src/lib/tests/template_test_crate.in").unwrap();
file.read_to_string(&mut old_template.inner).unwrap();

// This will return a [Template](crate::types::Template), which is updated to 0.6.0 (as specified by pkg_info_crate above)
// Use TmplBuilder::new("tmplgen").get_info() to manually get the info we manually set in pkg_info_crate!
// Won't update `homepage`, `distfiles` and `short_desc`, set the second argument to `true` for that
let template_updated = TmplBuilder::from_pkg_info(pkg_info_crate).get_type().unwrap().update(&old_template, false).unwrap();

// Write the [Template](crate::types::Template) to `./template`
let mut file = File::create("./template").unwrap();
file.write_all(template_updated.inner.as_bytes()).unwrap();
Errors
  • If you try to call this method without setting/getting pkg_info first via either (self.get_info)[crate::tmplwriter::TmplBuilder::get_info] or (self.set_type)[crate::tmplwriter::TmplBuilder::set_info]

Generates a new Template

Example
use libtmplgen::{PkgInfo, PkgType, TmplBuilder};
use std::fs::File;
use std::io::prelude::*;

// Do note that we only manually create a PkgInfo here to make the example easier to understand
let pkg_info_crate = PkgInfo {
       pkg_name: "tmplgen".to_string(),
       version: "0.6.0".to_string(),
       description: "Void Linux template generator for language-specific package managers"
           .to_string(),
       homepage: "https://github.com/Cogitri/tmplgen".to_string(),
       license: vec!["GPL-3.0-or-later".to_string()],
       dependencies: None,
       sha: "afc403bf69ad4da168938961b0f02da86ef29d655967cfcbacc8201e1327aff4".to_string(),
       download_url: Some(
           "https://static.crates.io/crates/tmplgen/tmplgen-${version}.crate".to_string(),
       ),
};

// Use TmplBuilder::new("tmplgen").get_type.generate() to do this automatically instead of
// setting PkgInfo and PkgType manually
// You generally want "prefix" to be true, unless you want to generate a template without
// the language- prefix.
let template = TmplBuilder::from_pkg_info(pkg_info_crate).set_type(PkgType::Crate).generate(true).unwrap();

// Write the [Template](crate::types::Template) to `./template`
let mut file = File::create("./template").unwrap();
file.write_all(template.inner.as_bytes()).unwrap();
  • If you try to call this method without setting/getting pkg_info first via either (self.get_info)[crate::tmplwriter::TmplBuilder::get_info] or (self.set_info)[crate::tmplwriter::TmplBuilder::set_info]

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.