Skip to content

Commit

Permalink
Merge pull request #25 from balena-io-modules/replace-error-chain
Browse files Browse the repository at this point in the history
Replace error chain
  • Loading branch information
zrzka committed Nov 16, 2018
2 parents c84f435 + 344e1bc commit 1933c39
Show file tree
Hide file tree
Showing 27 changed files with 622 additions and 179 deletions.
7 changes: 0 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,3 @@ install:

script:
- ci/test.sh

notifications:
email:
- robert@balena.io
- cyryl@balena.io
on_success: change
on_failure: always
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "balena-temen"
version = "0.0.9"
authors = ["Robert Vojta <robert@balena.io>"]
authors = ["Robert Vojta <robert@balena.io>", "Cyryl Płotnicki <cyryl@balena.io>"]
edition = "2018"
maintenance = { status = "actively-developed" }
license = "Apache-2.0"
Expand All @@ -15,7 +15,6 @@ travis-ci = { repository = "balena-io-modules/balena-temen", branch = "master" }
[dependencies]
approx = "0.3"
chrono = "0.4"
error-chain = "0.12"
lazy_static = "1"
pest = "2"
pest_derive = "2"
Expand Down
9 changes: 5 additions & 4 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
use std::{collections::HashMap, str::FromStr};

use crate::{
error::{bail, Error},
error::*,
parser::parse
};

Expand Down Expand Up @@ -365,18 +365,19 @@ impl Expression {
/// Converts self into [`Identifier`]
///
/// [`Identifier`]: struct.Identifier.html
pub fn into_identifier(self) -> Result<Identifier, Error> {
pub fn into_identifier(self) -> Result<Identifier> {
match self.value {
ExpressionValue::Identifier(identifier) => Ok(identifier),
_ => bail!("expression is not an identifier"),
_ => Err(Error::with_message("expression does not contain an identifier")
.context("expression", format!("{:?}", self))),
}
}
}

impl FromStr for Expression {
type Err = Error;

fn from_str(s: &str) -> Result<Expression, Self::Err> {
fn from_str(s: &str) -> Result<Expression> {
parse(s)
}
}
30 changes: 21 additions & 9 deletions src/builtin/filter/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,29 @@ use chrono::{DateTime, NaiveDateTime, Utc};
use serde_json::Value;

use crate::context::Context;
use crate::error::Result;

fn format_timestamp(filter: &str, input: &Value, args: &HashMap<String, Value>, default: &str) -> Result<Value> {
let ts = input
.as_i64()
.ok_or_else(|| format!("`{}` accepts integer only", filter))?;
use crate::error::*;

fn format_timestamp(
filter: &'static str,
input: &Value,
args: &HashMap<String, Value>,
default: &str,
) -> Result<Value> {
let ts = input.as_i64().ok_or_else(|| {
Error::with_message("invalid input type")
.context("filter", filter)
.context("expected", "i64")
.context("input", input.to_string())
})?;

let format = match args.get("format") {
Some(x) => x
.as_str()
.ok_or_else(|| format!("`{}` format must be a string", filter))?,
Some(x) => x.as_str().ok_or_else(|| {
Error::with_message("invalid argument type")
.context("filter", filter)
.context("argument name", "format")
.context("argument value", x.to_string())
.context("expected", "string")
})?,
None => default,
};

Expand Down
9 changes: 7 additions & 2 deletions src/builtin/filter/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@ use std::collections::HashMap;
use serde_json::Value;

use crate::context::Context;
use crate::error::Result;
use crate::error::*;

pub(crate) fn lower(input: &Value, _args: &HashMap<String, Value>, _context: &mut Context) -> Result<Value> {
let s = input.as_str().ok_or_else(|| "`lower` filter accepts string only")?;
let s = input.as_str().ok_or_else(|| {
Error::with_message("invalid input type")
.context("filter", "lower")
.context("expected", "string")
.context("input", input.to_string())
})?;
Ok(Value::String(s.to_lowercase()))
}

Expand Down
13 changes: 10 additions & 3 deletions src/builtin/filter/slugify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,25 @@ use serde_json::Value;
use slug;

use crate::context::Context;
use crate::error::Result;
use crate::error::*;

pub(crate) fn slugify(input: &Value, _args: &HashMap<String, Value>, _context: &mut Context) -> Result<Value> {
let s = input.as_str().ok_or_else(|| "`slugify` filter accepts string only")?;
let s = input.as_str().ok_or_else(|| {
Error::with_message("invalid input type")
.context("filter", "slugify")
.context("expected", "string")
.context("input", input.to_string())
})?;

if s.is_empty() {
return Ok(input.clone());
}

let result = slug::slugify(s);
if result.is_empty() {
return Err(format!("unable to slugify `{}`", s).into());
return Err(Error::with_message("empty result, unable to slugify input")
.context("filter", "slugify")
.context("input", input.to_string()));
}

Ok(Value::String(result))
Expand Down
17 changes: 9 additions & 8 deletions src/builtin/filter/trim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ use std::collections::HashMap;
use serde_json::Value;

use crate::context::Context;
use crate::error::Result;
use crate::error::*;

pub(crate) fn trim(input: &Value, _args: &HashMap<String, Value>, _context: &mut Context) -> Result<Value> {
Ok(Value::String(
input
.as_str()
.ok_or_else(|| "`trim` filter accepts string only")?
.trim()
.to_string(),
))
let s = input.as_str().ok_or_else(|| {
Error::with_message("invalid input type")
.context("filter", "trim")
.context("expected", "string")
.context("input", input.to_string())
})?;

Ok(Value::String(s.trim().to_string()))
}

#[cfg(test)]
Expand Down
9 changes: 7 additions & 2 deletions src/builtin/filter/upper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@ use std::collections::HashMap;
use serde_json::Value;

use crate::context::Context;
use crate::error::Result;
use crate::error::*;

pub(crate) fn upper(input: &Value, _args: &HashMap<String, Value>, _context: &mut Context) -> Result<Value> {
let s = input.as_str().ok_or_else(|| "`upper` filter accepts string only")?;
let s = input.as_str().ok_or_else(|| {
Error::with_message("invalid input type")
.context("filter", "trim")
.context("expected", "string")
.context("input", input.to_string())
})?;
Ok(Value::String(s.to_uppercase()))
}

Expand Down
32 changes: 20 additions & 12 deletions src/builtin/function/now.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,28 @@ use chrono::{DateTime, Local, Utc};
use serde_json::{Number, Value};

use crate::context::Context;
use crate::error::Result;
use crate::error::*;

fn now_with_cached(cached: DateTime<Utc>, args: &HashMap<String, Value>) -> Result<Value> {
let timestamp = args
.get("timestamp")
.unwrap_or_else(|| &Value::Bool(false))
.as_bool()
.ok_or_else(|| "timestamp must be a boolean value")?;

let utc = args
.get("utc")
.unwrap_or_else(|| &Value::Bool(false))
.as_bool()
.ok_or_else(|| "utc must be a boolean value")?;
let timestamp = args.get("timestamp").unwrap_or_else(|| &Value::Bool(false));

let timestamp = timestamp.as_bool().ok_or_else(|| {
Error::with_message("invalid argument type")
.context("function", "now")
.context("argument name", "timestamp")
.context("argument value", timestamp.to_string())
.context("expected", "boolean")
})?;

let utc = args.get("utc").unwrap_or_else(|| &Value::Bool(false));

let utc = utc.as_bool().ok_or_else(|| {
Error::with_message("invalid argument type")
.context("function", "now")
.context("argument name", "utc")
.context("argument value", utc.to_string())
.context("expected", "boolean")
})?;

match (utc, timestamp) {
(true, true) => Ok(Value::Number(Number::from(cached.timestamp()))),
Expand Down
Loading

0 comments on commit 1933c39

Please sign in to comment.