Skip to content

Commit

Permalink
rust 2018 edition and some version changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Steve Hoffman committed Jun 16, 2020
1 parent 92cc4ad commit a0448a4
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 27 deletions.
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
name = "limpet"
version = "0.2.2"
authors = ["Steve Hoffman <[email protected]>"]
edition = '2018'

[dependencies]
rustc-serialize = "0.3.19"
rustc-serialize = "0.3.24"
docopt = "0.6.83"
liquid = "0.20.0"
walkdir = "0.1.8"
liquid = "0.20.1"
walkdir = "2.3.1"
4 changes: 2 additions & 2 deletions package.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
VERSION=`grep '^version' Cargo.toml | cut -d '"' -f 2`
echo "Packaging version ${VERSION}..."
cargo clean
docker run --rm -v `pwd`:/source -ti schickling/rust cargo build --release
zip -j limpet_${VERSION}_linux_amd64.zip target/release/limpet
docker run --rm -v `pwd`:/volume -ti clux/muslrust cargo build --release
zip -j limpet_${VERSION}_linux_amd64.zip target/x86_64-unknown-linux-musl/release/limpet

76 changes: 54 additions & 22 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
extern crate docopt;
extern crate rustc_serialize;
extern crate liquid;
extern crate walkdir;

use docopt::Docopt;
use liquid::Template;
use std::env;
use std::fs::File;
use std::io::Read;
use std::io::Write;
use std::path::PathBuf;
use std::process::exit;
use docopt::Docopt;
use liquid::Template;
use walkdir::WalkDir;

const VERSION: &'static str = env!("CARGO_PKG_VERSION");
Expand All @@ -35,10 +30,14 @@ struct Args {
flag_recursive: bool,
arg_dir: String,
// flag_help: bool, Don't need this - it's automatically processed by docopt
flag_version: bool
flag_version: bool,
}

fn process_single_file(source: &PathBuf, destination: &PathBuf, context: &mut liquid::Object) -> Result<(), String> {
fn process_single_file(
source: &PathBuf,
destination: &PathBuf,
context: &mut liquid::Object,
) -> Result<(), String> {
// Read in full template as string
match File::open(source.as_path()) {
Ok(mut f) => {
Expand All @@ -48,36 +47,55 @@ fn process_single_file(source: &PathBuf, destination: &PathBuf, context: &mut li
return Err(format!("Source file {:?} can't be a directory. Did you mean to use the --recursive option?", &source));
}

// TODO: save permissions to set file with later?
// let permissions = metadata.permissions();

// TODO: save permissions to set file with later?
// let permissions = metadata.permissions();
} else {
return Err(format!("Problem reading source file metadata <{:?}>: Could be permissions.", &source));
return Err(format!(
"Problem reading source file metadata <{:?}>: Could be permissions.",
&source
));
}

// Read template and process
let mut contents: Vec<u8> = Vec::new();
let _ = f.read_to_end(&mut contents).expect("Couldn't read whole file");
let _ = f
.read_to_end(&mut contents)
.expect("Couldn't read whole file");
// println!("Read {} bytes", size);
let template_string = String::from_utf8(contents).unwrap();
let template: Template = liquid::ParserBuilder::with_stdlib().build().unwrap().parse(&template_string).unwrap();
let template: Template = liquid::ParserBuilder::with_stdlib()
.build()
.unwrap()
.parse(&template_string)
.unwrap();
if let Ok(output_string) = template.render(context) {
// println!("rendered file is {:?}", &output_string);
match File::create(&destination) {
Ok(mut output_file) => {
if let Err(e) = output_file.write_all(&output_string.into_bytes()) {
return Err(format!("Error writing file {}: {}", destination.to_str().unwrap(), &e));
return Err(format!(
"Error writing file {}: {}",
destination.to_str().unwrap(),
&e
));
}
// I don't think we need to flush/close in rust.
// output_file.flush();
return Ok(());
}
Err(e) => {
return Err(format!("Destination file <{}>: {}", destination.to_str().unwrap(), &e));
return Err(format!(
"Destination file <{}>: {}",
destination.to_str().unwrap(),
&e
));
}
}
} else {
return Err(format!("Unknown error while rendering the template for {}", source.to_str().unwrap()));
return Err(format!(
"Unknown error while rendering the template for {}",
source.to_str().unwrap()
));
}
}
Err(e) => {
Expand All @@ -87,7 +105,9 @@ fn process_single_file(source: &PathBuf, destination: &PathBuf, context: &mut li
}

fn main() {
let args:Args = Docopt::new(USAGE).and_then(|d| d.decode()).unwrap_or_else(|e| e.exit());
let args: Args = Docopt::new(USAGE)
.and_then(|d| d.decode())
.unwrap_or_else(|e| e.exit());

// Build a data context for the liquid templates
let mut data = liquid::Object::new();
Expand Down Expand Up @@ -118,12 +138,24 @@ fn main() {
if let Some(file_stem) = p.file_stem() {
dest.push(file_stem);
// println!("Processing file {} into {}", p.display(), dest.display());
match process_single_file(&p.to_path_buf(), &dest, &mut data) {
match process_single_file(
&p.to_path_buf(),
&dest,
&mut data,
) {
Ok(_) => {
println!("Processed: {} => {}", p.to_path_buf().display(), dest.display());
println!(
"Processed: {} => {}",
p.to_path_buf().display(),
dest.display()
);
}
Err(e) => {
println!("ERROR processing file {}: {:?}", p.display(), e);
println!(
"ERROR processing file {}: {:?}",
p.display(),
e
);
}
}
}
Expand Down

0 comments on commit a0448a4

Please sign in to comment.