Struct libtmplgen::types::TmplBuilder
source · pub struct TmplBuilder {
pub pkg_name: String,
pub pkg_type: Option<PkgType>,
pub pkg_info: Option<PkgInfo>,
pub deps: Option<Vec<String>>,
}
Fields§
§pkg_name: String
§pkg_type: Option<PkgType>
§pkg_info: Option<PkgInfo>
§deps: Option<Vec<String>>
Implementations§
source§impl TmplBuilder
impl TmplBuilder
sourcepub fn new(pkg_name: &str) -> TmplBuilder
pub fn new(pkg_name: &str) -> TmplBuilder
Initializes a new TmplBuilder with nothing but pkg_name set.
sourcepub fn from_pkg_info(pkg_info: PkgInfo) -> TmplBuilder
pub fn from_pkg_info(pkg_info: PkgInfo) -> TmplBuilder
Initializes a new TmplBuilder from a PkgInfo. Useful for testing or as a shortcut
sourcepub fn get_type(&mut self) -> Result<&mut TmplBuilder, Error>
pub fn get_type(&mut self) -> Result<&mut TmplBuilder, Error>
Gets the PkgType of the package of the TmplBuilder that’s passed into the function
sourcepub fn set_type(&mut self, pkg_type: PkgType) -> &mut TmplBuilder
pub fn set_type(&mut self, pkg_type: PkgType) -> &mut TmplBuilder
Sets the PkgType of the package of the TmplBuilder that’s passed into the function
sourcepub fn get_info(&mut self) -> Result<&mut TmplBuilder, Error>
pub fn get_info(&mut self) -> Result<&mut TmplBuilder, Error>
Gets the PkgInfo of the package of the TmplBuilder that’s passed into the function
sourcepub fn set_info(&mut self, pkg_info: PkgInfo) -> &mut TmplBuilder
pub fn set_info(&mut self, pkg_info: PkgInfo) -> &mut TmplBuilder
Sets the PkgInfo of the package of the TmplBuilder that’s passed into the function
sourcepub fn get_deps(&mut self) -> Result<&mut TmplBuilder, Error>
pub fn get_deps(&mut self) -> Result<&mut TmplBuilder, Error>
Gets the dependencies of the TmplBuilder that’s passed into the function
sourcepub fn is_built_in(&self) -> Result<bool, Error>
pub fn is_built_in(&self) -> Result<bool, Error>
Checks if a Gem or PerlDist is built into Ruby/Perl.
sourcepub fn update(
&self,
old_template: &Template,
update_all: bool
) -> Result<Template, Error>
pub fn update(
&self,
old_template: &Template,
update_all: bool
) -> Result<Template, Error>
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() };
// 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();
sourcepub fn generate(&self) -> Result<Template, Error>
pub fn generate(&self) -> Result<Template, Error>
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
let template = TmplBuilder::from_pkg_info(pkg_info_crate).set_type(PkgType::Crate).generate().unwrap();
// Write the [Template](crate::types::Template) to `./template`
let mut file = File::create("./template").unwrap();
file.write_all(template.inner.as_bytes()).unwrap();
Errors
- Errors out if any of the underlying functions fails