[][src]Struct libtmplgen::types::TmplBuilder

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

The TemplateBuilder struct, which is used to build a Template

Fields

pkg_name: Stringpkg_type: Option<PkgType>pkg_info: Option<PkgInfo>

Methods

impl TmplBuilder[src]

pub fn new(pkg_name: &str) -> Self[src]

Initializes a new TmplBuilder with nothing but pkg_name set.

pub fn from_pkg_info(pkg_info: PkgInfo) -> Self[src]

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

pub fn get_type(&mut self) -> Result<&mut Self, Error>[src]

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

pub fn set_type(&mut self, pkg_type: PkgType) -> &mut Self[src]

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

pub fn get_info(&mut self) -> Result<&mut Self, Error>[src]

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

Errors

pub fn set_info(&mut self, pkg_info: PkgInfo) -> &mut Self[src]

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

pub fn is_built_in(&self) -> Result<bool, Error>[src]

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

Errors

pub fn gen_deps(&self, tmpl_path: Option<&str>) -> Result<Vec<Template>, Error>[src]

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

pub fn update(
    &self,
    old_template: &Template,
    update_all: bool
) -> Result<Template, Error>
[src]

Updates a Template

Example

use libtmplgen::*;
use std::fs::File;
use std::io::prelude::*;

fn update_template() -> Result<(), Error> {
    // 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: Some("Void Linux template generator for language-specific package managers"
               .to_string()),
           homepage: "https://github.com/Cogitri/tmplgen".to_string(),
           license: Some(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")?;
    file.read_to_string(&mut old_template.inner)?;

    // 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()?
        .update(&old_template, false)?;

    // Write the [Template](crate::types::Template) to `./template`
    let mut file = File::create("./template")?;
    file.write_all(template_updated.inner.as_bytes())?;

    Ok(())
}

Errors

pub fn generate(&self, prefix: bool) -> Result<Template, Error>[src]

Generates a new Template

Example

use libtmplgen::*;
use std::fs::File;
use std::io::prelude::*;

fn write_template() -> Result<(), Error> {
    // 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: Some("Void Linux template generator for language-specific package managers"
               .to_string()),
           homepage: "https://github.com/Cogitri/tmplgen".to_string(),
           license: Some(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)?;

    // Write the [Template](crate::types::Template) to `./template`
    let mut file = File::create("./template")?;
    file.write_all(template.inner.as_bytes())?;

    Ok(())
}

Auto Trait Implementations

impl Send for TmplBuilder

impl Sync for TmplBuilder

Blanket Implementations

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T> From for T[src]

impl<T, U> TryFrom for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T> BorrowMut for T where
    T: ?Sized
[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Erased for T

impl<T> Same for T

type Output = T

Should always be Self