Skip to content
This repository has been archived by the owner on Feb 20, 2024. It is now read-only.

Commit

Permalink
clean up string handling a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
globin committed Feb 13, 2015
1 parent 726021b commit 4d54b71
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 35 deletions.
3 changes: 1 addition & 2 deletions src/bin/pumuckl.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![feature(slicing_syntax)]

extern crate puppetfile;
extern crate serialize;
extern crate semver;

use std::old_io::File;
Expand Down Expand Up @@ -45,7 +44,7 @@ fn main() {
}
).collect();

let mut modules_to_check = puppetfile.modules.iter().filter(
let modules_to_check = puppetfile.modules.iter().filter(
|m| m.user_name_pair().is_some() && m.version().is_some()
);

Expand Down
17 changes: 6 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
#![crate_name = "puppetfile"]
#![deny(missing_docs)]
#![feature(plugin, slicing_syntax)]
#![allow(unstabl)]

#[plugin] extern crate peg_syntax_ext;
#![plugin(peg_syntax_ext)]

extern crate hyper;
extern crate semver;
Expand All @@ -28,22 +27,20 @@ mod test;

/// This represents a Puppetfile
#[derive(PartialEq, Clone, Debug)]
#[experimental]
pub struct Puppetfile {
/// The forge URL
pub forge: String,
/// All Modules contained in the Puppetfile
pub modules: Vec<Module>
}

#[experimental]
impl Puppetfile {
/// Try parsing the contents of a Puppetfile into a Puppetfile struct
pub fn parse(contents: &str) -> Result<Puppetfile, String> {
grammar::parse(contents)
}
}
impl fmt::String for Puppetfile {
impl fmt::Display for Puppetfile {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let res = write!(f, "forge '{}'\n\n", self.forge);
self.modules.iter().fold(res, |prev_res, module| { prev_res.and(write!(f, "\n{}\n", module)) })
Expand All @@ -53,7 +50,6 @@ impl fmt::String for Puppetfile {

/// The representation of a puppet module
#[derive(PartialEq, Clone, Debug)]
#[experimental]
pub struct Module {
/// Name of the module
pub name: String,
Expand Down Expand Up @@ -148,10 +144,9 @@ impl Error for PuppetfileError {
}
}

#[experimental]
impl Module {
/// The current version of the module returned from the forge API
pub fn forge_version(&self, forge_url: &String) -> Result<semver::Version, PuppetfileError> {
pub fn forge_version(&self, forge_url: &str) -> Result<semver::Version, PuppetfileError> {
let url = try!(self.version_url(forge_url));
let mut response = try!(Client::new().get(&url[]).send());
let response_string = try!(response.read_to_string());
Expand All @@ -162,7 +157,7 @@ impl Module {
}

/// Builds the URL for the forge API for fetching the version
pub fn version_url(&self, forge_url: &String) -> Result<String, PuppetfileError> {
pub fn version_url(&self, forge_url: &str) -> Result<String, PuppetfileError> {
let stripped_url = match forge_url[].ends_with("/") {
true => &forge_url[..forge_url.len() - 1],
_ => &forge_url[]
Expand Down Expand Up @@ -196,7 +191,7 @@ impl Module {
None
}
}
impl fmt::String for Module {
impl fmt::Display for Module {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let res = write!(f, "mod '{}'", self.name);
self.info.iter().fold(res, |prev_res, mod_info| {
Expand Down Expand Up @@ -227,7 +222,7 @@ impl ModuleInfo {
}
}

impl fmt::String for ModuleInfo {
impl fmt::Display for ModuleInfo {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
ModuleInfo::Version(ref v) => write!(f, "{}", v),
Expand Down
44 changes: 22 additions & 22 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fn empty_file() {
assert!(puppetfile.is_ok());
let parsed = puppetfile.unwrap();
assert_eq!(
String::from_str("https://forge.puppetlabs.com"),
"https://forge.puppetlabs.com",
parsed.forge
);
assert_eq!(
Expand All @@ -26,11 +26,11 @@ mod 'mayflower/php'

let parsed = puppetfile.unwrap();
assert_eq!(
String::from_str("https://forge.puppetlabs.com"),
"https://forge.puppetlabs.com",
parsed.forge
);
assert_eq!(
Module { name: String::from_str("mayflower/php"), info: vec![] },
Module { name: "mayflower/php".to_string(), info: vec![] },
parsed.modules[0]
);
}
Expand All @@ -45,12 +45,12 @@ mod 'mayflower/php', '1.0.1'

let parsed = puppetfile.unwrap();
assert_eq!(
String::from_str("https://forge.puppetlabs.com"),
"https://forge.puppetlabs.com",
parsed.forge
);
assert_eq!(
Module {
name: String::from_str("mayflower/php"),
name: "mayflower/php".to_string(),
info: vec![ModuleInfo::Version(VersionReq::parse("= 1.0.1").unwrap())]
},
parsed.modules[0]
Expand All @@ -68,15 +68,15 @@ mod 'mayflower/php',

let parsed = puppetfile.unwrap();
assert_eq!(
String::from_str("https://forge.puppetlabs.com"),
"https://forge.puppetlabs.com",
parsed.forge
);
assert_eq!(
Module {
name: String::from_str("mayflower/php"),
name: "mayflower/php".to_string(),
info: vec![
ModuleInfo::Info(String::from_str("git"),
String::from_str("git://github.com/Mayflower/puppet-php.git"))
ModuleInfo::Info("git".to_string(),
"git://github.com/Mayflower/puppet-php.git".to_string())
]
},
parsed.modules[0]
Expand All @@ -89,57 +89,57 @@ fn format() {
assert_eq!(String::from_str("= 1.0.0"), format!("{}", version));

let mod_info = ModuleInfo::Info(
String::from_str("git"),
String::from_str("git://github.com/Mayflower/puppet-php.git")
"git".to_string(),
"git://github.com/Mayflower/puppet-php.git".to_string()
);
assert_eq!(
String::from_str(":git => 'git://github.com/Mayflower/puppet-php.git'"),
":git => 'git://github.com/Mayflower/puppet-php.git'",
format!("{}", mod_info)
);

let module = Module {
name: String::from_str("mayflower/php"),
name: "mayflower/php".to_string(),
info: vec![version, mod_info]
};
assert_eq!(
String::from_str("mod 'mayflower/php', '= 1.0.0',
:git => 'git://github.com/Mayflower/puppet-php.git'"),
"mod 'mayflower/php', '= 1.0.0',
:git => 'git://github.com/Mayflower/puppet-php.git'",
format!("{}", module)
);

let puppetfile = Puppetfile {
forge: String::from_str("https://forge.puppetlabs.com"),
forge: "https://forge.puppetlabs.com".to_string(),
modules: vec![module]
};
assert_eq!(
String::from_str("forge 'https://forge.puppetlabs.com'
"forge 'https://forge.puppetlabs.com'
mod 'mayflower/php', '= 1.0.0',
:git => 'git://github.com/Mayflower/puppet-php.git'
"),
",
format!("{}", puppetfile)
);
}

#[test]
fn version_url() {
let module = Module { name: String::from_str("mayflower/php"), info: vec![] };
let module = Module { name: "mayflower/php".to_string(), info: vec![] };
assert_eq!(
Ok("https://forge.puppetlabs.com/users/mayflower/modules/php/releases/find.json".to_string()),
module.version_url(&"https://forge.puppetlabs.com/".to_string())
module.version_url("https://forge.puppetlabs.com/")
)
}

#[test]
fn user_name_pair() {
let module = Module { name: String::from_str("mayflower/php"), info: vec![] };
let module = Module { name: "mayflower/php".to_string(), info: vec![] };
assert_eq!(module.user_name_pair(), Some(("mayflower", "php")))
}

#[test]
fn forge_version() {
let module = Module { name: String::from_str("puppetlabs/nginx"), info: vec![] };
let module = Module { name: "puppetlabs/nginx".to_string(), info: vec![] };
assert_eq!(
module.forge_version(&"https://forge.puppetlabs.com/".to_string()).unwrap(),
semver::Version::parse("99.99.99").unwrap()
Expand Down

0 comments on commit 4d54b71

Please sign in to comment.