Skip to content

Commit

Permalink
Merge branch 'master' of github.com:foundpatterns/torchbear
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnaz87 committed Dec 3, 2018
2 parents 5837af0 + 9b2c4a1 commit 923a581
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 5 deletions.
18 changes: 17 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "torchbear"
description = "network application framework"
version = "0.8.3"
version = "0.8.4"
authors = ["Mitchell Tannenbaum <[email protected]>"]
repository = "https://github.com/foundpatterns/torchbear"
readme = "Readme.md"
Expand Down Expand Up @@ -54,6 +54,7 @@ regex = "1"
tantivy = "0.7"
mime_guess = "1.8.6"
scl = "0.0.1"
heck = "0.3.0"

[dev-dependencies]
tempfile = "3"
Expand Down
1 change: 1 addition & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Note: You don't need to learn Rust to use Torchbear.
* [Tantivy](https://github.com/tantivy-search/tantivy) schema building, document adding/updating/deleting, and searching
* [regex](https://github.com/rust-lang/regex) matching and replacing
* [MIME](https://github.com/abonander/mime_guess) type guessing
* [Heck](https://github.com/withoutboats/heck) case conversions

## Installation

Expand Down
106 changes: 106 additions & 0 deletions src/bindings/heck.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
use rlua::prelude::*;
use heck::*;

pub fn init(lua: &Lua) -> Result<(), LuaError> {

let module = lua.create_table()?;

module.set("to_camel_case", lua.create_function(|_, text: String| {
Ok(text.to_camel_case())
})?)?;

module.set("to_kebab_case", lua.create_function(|_, text: String| {
Ok(text.to_kebab_case())
})?)?;

module.set("to_mixed_case", lua.create_function(|_, text: String| {
Ok(text.to_mixed_case())
})?)?;

module.set("to_shouty_snake_case", lua.create_function(|_, text: String| {
Ok(text.to_shouty_snake_case())
})?)?;

module.set("to_snake_case", lua.create_function(|_, text: String| {
Ok(text.to_snake_case())
})?)?;

module.set("to_title_case", lua.create_function(|_, text: String| {
Ok(text.to_title_case())
})?)?;

lua.globals().set("heck", module)?;

Ok(())
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn lua_heck_cambel () {
let lua = Lua::new();
init(&lua).unwrap();

lua.exec::<_, ()>(r#"
local val = "We are not in the least afraid of ruins"
assert(heck.to_camel_case(val) == "WeAreNotInTheLeastAfraidOfRuins")
"#, None).unwrap();
}

#[test]
fn lua_heck_kebab () {
let lua = Lua::new();
init(&lua).unwrap();

lua.exec::<_, ()>(r#"
local val = "We are going to inherit the earth"
assert(heck.to_kebab_case(val) == "we-are-going-to-inherit-the-earth")
"#, None).unwrap();
}

#[test]
fn lua_heck_mixed () {
let lua = Lua::new();
init(&lua).unwrap();

lua.exec::<_, ()>(r#"
local val = "It is we who built these palaces and cities"
assert(heck.to_mixed_case(val) == "itIsWeWhoBuiltThesePalacesAndCities")
"#, None).unwrap();
}

#[test]
fn lua_heck_shouty() {
let lua = Lua::new();
init(&lua).unwrap();

lua.exec::<_, ()>(r#"
local val = "That world is growing in this minute"
assert(heck.to_shouty_snake_case(val) == "THAT_WORLD_IS_GROWING_IN_THIS_MINUTE")
"#, None).unwrap();
}

#[test]
fn lua_heck_snake () {
let lua = Lua::new();
init(&lua).unwrap();

lua.exec::<_, ()>(r#"
local val = "We carry a new world here, in our hearts"
assert(heck.to_snake_case(val) == "we_carry_a_new_world_here_in_our_hearts")
"#, None).unwrap();
}

#[test]
fn lua_heck_title () {
let lua = Lua::new();
init(&lua).unwrap();

lua.exec::<_, ()>(r#"
local val = "We have always lived in slums and holes in the wall"
assert(heck.to_title_case(val) == "We Have Always Lived In Slums And Holes In The Wall")
"#, None).unwrap();
}
}
2 changes: 2 additions & 0 deletions src/bindings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ pub mod git;
pub mod regex;
pub mod mime;
pub mod scl;
pub mod heck;

// Panics if not included (?)
//#[cfg(feature = "log_bindings")]
pub mod log;
Expand Down
10 changes: 7 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,15 @@ extern crate git2;
extern crate regex;
extern crate openssl;
extern crate mime_guess;
extern crate heck;

#[cfg(feature = "tantivy_bindings")]
extern crate tantivy;
extern crate scl;

pub mod bindings;
pub mod logger;

use actix::prelude::*;
use actix_lua::LuaActorBuilder;
use actix_web::{server as actix_server, App};
Expand All @@ -47,8 +51,6 @@ use std::io;
use openssl::ssl::{SslAcceptor, SslFiletype, SslMethod};
use serde_json::Value;

pub mod bindings;
pub mod logger;

pub struct AppState {
pub lua: ::actix::Addr<::actix_lua::LuaActor>
Expand All @@ -75,7 +77,8 @@ fn create_vm(init_path: &str, settings: Value) -> Result<Lua, LuaError> {
bindings::tantivy::init(&lua)?;
bindings::mime::init(&lua)?;
bindings::scl::init(&lua)?;

bindings::heck::init(&lua)?;

// torchbear crashes if there's no log binding
//if cfg!(feature = "log_bindings") {
bindings::log::init(&lua)?;
Expand All @@ -86,6 +89,7 @@ fn create_vm(init_path: &str, settings: Value) -> Result<Lua, LuaError> {
let tb_table = lua.create_table()?;
tb_table.set("settings", rlua_serde::to_value(&lua, settings).map_err(LuaError::external)?)?;
tb_table.set("init_filename", init_path)?;
tb_table.set("version", env!("CARGO_PKG_VERSION"))?;
lua.globals().set("torchbear", tb_table)?;
}

Expand Down

0 comments on commit 923a581

Please sign in to comment.