use std::fs::write;
-use std::io::Result;
+use std::io::{Error, ErrorKind::Other, Result};
use std::path::PathBuf;
use crate::template::{find, parse, TemplateContext};
pub fn archive(_: &PathBuf, template_directory: &PathBuf, target: &PathBuf, context: &TemplateContext) -> Result<()> {
match find(template_directory, FILENAME) {
Some(template) => {
- let parsed_template = parse(&template);
- let rendered_template = parsed_template.render(context);
+ let parsed_template = parse(&template)
+ .ok_or_else(|| Error::new(Other, "Unable to parse Gemini Archive template"))?;
+ let rendered_template = parsed_template.render(context)?;
let location = target.join(FILENAME);
write(location, rendered_template)?;
},
use std::fs::write;
-use std::io::Result;
+use std::io::{Error, ErrorKind::Other, Result};
use std::path::PathBuf;
use crate::template::{find, parse, TemplateContext};
pub fn archive(_: &PathBuf, template_directory: &PathBuf, target: &PathBuf, context: &TemplateContext) -> Result<()> {
match find(template_directory, FILENAME) {
Some(template) => {
- let parsed_template = parse(&template);
- let rendered_template = parsed_template.render(context);
+ let parsed_template = parse(&template)
+ .ok_or_else(|| Error::new(Other, "Unable to parse Gopher Archive template"))?;
+ let rendered_template = parsed_template.render(context)?;
let location = target.join(FILENAME);
write(location, rendered_template)?;
},
-use std::io::Result;
+use std::io::{Error, ErrorKind::Other, Result};
use crate::configuration::Configuration;
use crate::remote::add;
}
fn execute(&self, input: Option<&String>, configuration: &Configuration, _: &String) -> Result<()> {
- let input = input.expect("You must provide a location for the remote.");
+ let input = input
+ .ok_or_else(|| Error::new(Other, "You must provide a location for the remote."))?;
add(&configuration.config_directory, &configuration.remote_config, input)
}
-use std::io::Result;
+use std::io::{Error, ErrorKind::Other, Result};
use crate::configuration::Configuration;
use std::process::{Command, Stdio};
fn execute(&self, input: Option<&String>, configuration: &Configuration, _: &String) -> Result<()> {
- let input = input.expect("You must provide a location to publish the blog");
+ let input = input
+ .ok_or_else(|| Error::new(Other, "You must provide a location to publish the blog"))?;
Command::new(COMMAND)
.arg("--version")
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
- .expect("Publishing requires rsync");
+ .map_err(|_| Error::new(Other, "Publishing requires rsync"))?;
Command::new(COMMAND)
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
- .expect("Publishing requires rsync");
+ .map_err(|_| Error::new(Other, "Rsync failed to publish."))?;
return Ok(())
}
-use std::io::Result;
+use std::io::{Error, ErrorKind::Other, Result};
use crate::configuration::Configuration;
use std::process::{Command, Stdio};
fn execute(&self, input: Option<&String>, configuration: &Configuration, _: &String) -> Result<()> {
- let input = input.expect("You must provide a location to publish the archive");
+ let input = input
+ .ok_or_else(|| Error::new(Other, "You must provide a location to publish the archive"))?;
Command::new(COMMAND)
.arg("--version")
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
- .expect("Publishing requires rsync");
+ .map_err(|_| Error::new(Other, "Publishing requires rsync"))?;
Command::new(COMMAND)
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
- .expect("Publishing requires rsync");
+ .map_err(|_| Error::new(Other, "Rsync failed to publish."))?;
return Ok(())
}
}
fn execute(&self, _: Option<&String>, configuration: &Configuration, command: &String) -> Result<()> {
- if command == self.command() {
- return sync_down(&configuration.data_directory, &configuration.remote_config);
+ match sync_down(&configuration.data_directory, &configuration.remote_config) {
+ Ok(_) => {}
+ Err(e) => {
+ if command == self.command() {
+ return Err(e)
+ }
+ }
}
return Ok(())
}
vec![]
}
- fn execute(&self, _: Option<&String>, configuration: &Configuration, _: &String) -> Result<()> {
- sync_up(&configuration.data_directory, &configuration.remote_config)
+ fn execute(&self, _: Option<&String>, configuration: &Configuration, command: &String) -> Result<()> {
+ match sync_up(&configuration.data_directory, &configuration.remote_config) {
+ Ok(_) => {}
+ Err(e) => {
+ if command == self.command() {
+ return Err(e)
+ }
+ }
+ }
+ return Ok(())
}
fn after_dependencies(&self) -> Vec<Box<dyn super::Command>> {
}
fn execute(&self, input: Option<&String>, configuration: &Configuration, _: &String) -> Result<()> {
- let input = input.expect("You must provide a path to a post");
+ let input = input
+ .ok_or_else(|| Error::new(ErrorKind::InvalidInput, "You must provide a path to a post"))?;
let post_location = PathBuf::from(input);
if !post_location.exists() {
return Err(Error::new(ErrorKind::NotFound, "The path provided does not exist"));
use std::fs::write;
-use std::io::Result;
+use std::io::{Error, ErrorKind::Other, Result};
use std::path::PathBuf;
use crate::template::{find, parse, TemplateContext};
pub fn generate(_: &PathBuf, template_directory: &PathBuf, target: &PathBuf, context: &TemplateContext) -> Result<()> {
match find(template_directory, FILENAME) {
Some(template) => {
- let parsed_template = parse(&template);
- let rendered_template = parsed_template.render(context);
+ let parsed_template = parse(&template)
+ .ok_or_else(|| Error::new(Other, "Unable to parse HTML template"))?;
+ let rendered_template = parsed_template.render(context)?;
let location = target.join(FILENAME);
write(location, rendered_template)?;
},
use std::fs::write;
-use std::io::Result;
+use std::io::{Error, ErrorKind::Other, Result};
use std::path::PathBuf;
use crate::template::{find, parse, TemplateContext};
pub fn generate(_: &PathBuf, template_directory: &PathBuf, target: &PathBuf, context: &TemplateContext) -> Result<()> {
match find(template_directory, FILENAME) {
Some(template) => {
- let parsed_template = parse(&template);
- let rendered_template = parsed_template.render(context);
+ let parsed_template = parse(&template)
+ .ok_or_else(|| Error::new(Other, "Unable to parse RSS template"))?;
+ let rendered_template = parsed_template.render(context)?;
let location = target.join(FILENAME);
write(location, rendered_template)?;
},
use std::fs::write;
-use std::io::Result;
+use std::io::{Error, ErrorKind::Other, Result};
use std::path::PathBuf;
use crate::template::{find, parse, TemplateContext};
pub fn generate(_: &PathBuf, template_directory: &PathBuf, target: &PathBuf, context: &TemplateContext) -> Result<()> {
match find(template_directory, FILENAME) {
Some(template) => {
- let parsed_template = parse(&template);
- let rendered_template = parsed_template.render(context);
+ let parsed_template = parse(&template)
+ .ok_or_else(|| Error::new(Other, "Unable to parse TXT template"))?;
+ let rendered_template = parsed_template.render(context)?;
let location = target.join(FILENAME);
write(location, rendered_template)?;
},
use configuration::Configuration;
fn main() -> Result<()> {
+ let result = run();
+
+ if cfg!(debug_assertions) {
+ result
+ } else {
+ match result {
+ Ok(_) => Ok(()),
+ Err(e) => {
+ eprintln!("Error: {}", e);
+ std::process::exit(1);
+ }
+ }
+ }
+}
+
+fn run() -> Result<()> {
let configuration = Configuration::new();
let commands = available_commands();
let arguments: Vec<String> = args().collect();
-use std::io::Result;
+use std::io::{Error, ErrorKind::Other, Result};
use std::path::PathBuf;
-use std::process::Command;
+use std::process::{Command, Stdio};
use std::time::{SystemTime, UNIX_EPOCH};
pub struct Git;
fn sync_up(&self, remote: &str, directory: &PathBuf) -> Result<()> {
let timestamp = SystemTime::now().duration_since(UNIX_EPOCH)
- .expect("Invalid time")
+ .map_err(|_| Error::new(Other, "Invalid time"))?
.as_millis();
let commands = vec![
Command::new("sh")
.arg("-c")
.arg(&command)
+ .stdout(Stdio::null())
+ .stderr(Stdio::null())
.status()
- .expect("Failed while performing sync up with git");
+ .map_err(|_| Error::new(Other, "Failed while performing sync up with git"))?;
}
Ok(())
Command::new("sh")
.arg("-c")
.arg(&command)
+ .stdout(Stdio::null())
+ .stderr(Stdio::null())
.status()
- .expect("Failed while performing sync down with git");
+ .map_err(|_| Error::new(Other, "Failed while performing sync down with git"))?;
}
Ok(())
}
pub fn sync_up(data_directory: &PathBuf, remote_config: &PathBuf) -> Result<()> {
let remote_address = read_remote(remote_config)
- .expect("No remote is configured");
+ .ok_or_else(|| Error::new(Other, "No remote is configured"))?;
create_dir_all(data_directory)?;
let remotes = available_remotes();
for remote in remotes {
pub fn sync_down(data_directory: &PathBuf, remote_config: &PathBuf) -> Result<()> {
let remote_address = read_remote(remote_config)
- .expect("No remote is configured");
+ .ok_or_else(|| Error::new(Other, "No remote is configured"))?;
create_dir_all(data_directory)?;
let remotes = available_remotes();
for remote in remotes {
+use std::io::{Error, ErrorKind::Other, Result};
use std::collections::HashMap;
use std::fs::File;
use std::path::PathBuf;
}
impl ParsedTemplate {
- pub fn render(&self, context: &TemplateContext) -> String {
+ pub fn render(&self, context: &TemplateContext) -> Result<String> {
ParsedTemplate::render_tokens(&self.tokens, context)
}
- pub fn render_tokens(tokens: &Vec<Token>, context: &TemplateContext) -> String {
+ pub fn render_tokens(tokens: &Vec<Token>, context: &TemplateContext) -> Result<String> {
let mut rendered_template: String = String::new();
for token in tokens {
match token {
Token::Text(contents) => rendered_template.push_str(&contents),
Token::DisplayDirective { content } => {
- match TemplateContextGetter::get(context, &content) {
- Some(value) => rendered_template.push_str(&value.render()),
- None => panic!("{} is not a valid key", content)
- }
+ let value = TemplateContextGetter::get(context, &content)
+ .ok_or_else(|| Error::new(Other, format!("{} is not a valid key", content)))?;
+ rendered_template.push_str(&value.render());
},
Token::ConditionalDirective { condition, children} => {
let mut negator = false;
negator = true;
condition = condition[1..].to_string();
}
- match TemplateContextGetter::get(context, &condition) {
- Some(TemplateValue::Bool(value)) => {
+
+ let value = TemplateContextGetter::get(context, &condition)
+ .ok_or_else(|| Error::new(Other, format!("{} is not a valid key", condition)))?;
+
+ match value {
+ TemplateValue::Bool(value) => {
if negator ^ value {
- rendered_template.push_str(&ParsedTemplate::render_tokens(children, context))
+ rendered_template.push_str(&ParsedTemplate::render_tokens(children, context)?)
}
+ Ok(())
},
- _ => panic!("{} is not a boolean value", condition)
- }
+ _ => Err(Error::new(Other, format!("{} is not a boolean value", condition))),
+ }?;
},
Token::IteratorDirective { collection, member_label, children } => {
- match TemplateContextGetter::get(context, &collection) {
- Some(TemplateValue::Collection(collection)) => {
+ let value = TemplateContextGetter::get(context, &collection)
+ .ok_or_else(|| Error::new(Other, format!("{} is not a valid key", collection)))?;
+
+ match value {
+ TemplateValue::Collection(collection) => {
for member in collection {
let mut child_context = context.clone();
child_context.insert(
member_label.to_string(),
TemplateValue::Context(member)
);
- rendered_template.push_str(&ParsedTemplate::render_tokens(&children, &child_context))
+ rendered_template.push_str(&ParsedTemplate::render_tokens(&children, &child_context)?)
}
+ Ok(())
},
- _ => panic!("{} is not a collection", collection)
- }
+ _ => Err(Error::new(Other, format!("{} is not a collection", collection))),
+ }?;
}
}
}
- rendered_template
+ Ok(rendered_template)
}
}
-pub fn parse(template: &str) -> ParsedTemplate {
+pub fn parse(template: &str) -> Option<ParsedTemplate> {
let mut tokens = Vec::new();
- tokenize(template, &mut tokens);
- ParsedTemplate {
+ tokenize(template, &mut tokens).ok()?;
+ Some(ParsedTemplate {
tokens
- }
+ })
}
-fn tokenize(template: &str, tokens: &mut Vec<Token>) {
+fn tokenize(template: &str, tokens: &mut Vec<Token>) -> Result<()> {
let mut remaining_template = template;
while !remaining_template.is_empty() && remaining_template.contains("{{") {
let directive_start_index = remaining_template.find("{{")
- .expect("Was expecting at least one tag opener");
+ .ok_or_else(|| Error::new(Other, "Was expecting at least one tag opener"))?;
if directive_start_index > 0 {
let text = remaining_template[..directive_start_index].to_string();
tokens.push(Token::Text(text.to_string()));
remaining_template = &remaining_template[directive_start_index..];
let directive_end_index = remaining_template.find("}}")
- .expect("Was expecting }} after {{") + 2;
+ .ok_or_else(|| Error::new(Other, "Was expecting }} after {{"))? + 2;
let directive = &remaining_template[..directive_end_index];
remaining_template = &remaining_template[directive_end_index..];
let closing_block = remaining_template.find("{{?}}").unwrap();
let directive_block = &remaining_template[..closing_block];
remaining_template = &remaining_template[closing_block + 5..];
- tokenize(directive_block, &mut children);
+ tokenize(directive_block, &mut children)?;
tokens.push(Token::ConditionalDirective{
condition: content.to_string(),
children
let closing_block = remaining_template.find("{{~}}").unwrap();
let directive_block = &remaining_template[..closing_block];
remaining_template = &remaining_template[closing_block + 5..];
- tokenize(directive_block, &mut children);
+ tokenize(directive_block, &mut children)?;
if parts.len() == 2 {
tokens.push(Token::IteratorDirective {
collection: parts[0].trim().to_string(),
}
}
tokens.push(Token::Text(remaining_template.to_string()));
+ Ok(())
}
// File helpers.
{{= post.raw}}
------------------------------------
{{~}}
-{{? posts_count == 0}}
+{{? !has_posts }}
## This is a fresh blog!
There are no posts yet.