aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatias Linares <matiaslina@openmailbox.org>2015-11-27 03:50:07 -0300
committerMatias Linares <matiaslina@openmailbox.org>2015-11-27 03:50:07 -0300
commit901d03b3a8ac0b7d8533fd9a8d43238d665d9cb8 (patch)
tree469acd8d8110afa2a9cf89ab79142583159828ff
parente99ad3688971fde57cf34d4a9f5dd89256c7ab78 (diff)
downloaddotwm-901d03b3a8ac0b7d8533fd9a8d43238d665d9cb8.tar.gz
Little implemented the socket parser.
Just two functions for the parsing of the socket. nothing special.
-rw-r--r--Cargo.lock6
-rw-r--r--Cargo.toml1
-rw-r--r--src/main.rs2
-rw-r--r--src/socket/parser.rs36
4 files changed, 43 insertions, 2 deletions
diff --git a/Cargo.lock b/Cargo.lock
index eabeb47..7b7fe67 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -3,6 +3,7 @@ name = "dotwm"
version = "0.1.0"
dependencies = [
"libc 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
+ "nom 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"unix_socket 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"x11 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
@@ -23,6 +24,11 @@ version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
+name = "nom"
+version = "1.0.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
name = "pkg-config"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/Cargo.toml b/Cargo.toml
index 66e2b19..b479ece 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -6,6 +6,7 @@ authors = ["Matias Linares <matiaslina@openmailbox.org>"]
[dependencies]
libc = "0.2.*"
unix_socket = "*"
+nom = "~1.0.0"
[dependencies.x11]
version = "*"
diff --git a/src/main.rs b/src/main.rs
index 27c1a46..21133ef 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -3,6 +3,8 @@
extern crate libc;
extern crate x11;
extern crate unix_socket;
+#[macro_use]
+extern crate nom;
pub mod safe_x11;
pub mod command;
diff --git a/src/socket/parser.rs b/src/socket/parser.rs
index 3cfc15d..e8dd9ec 100644
--- a/src/socket/parser.rs
+++ b/src/socket/parser.rs
@@ -11,7 +11,28 @@ use x11::keysym;
use command::*;
use dotwm::DotWM;
-fn str_to_modifier<'a>(s: &'a str) -> Result<u32, ()> {
+use nom::{IResult,alpha,multispace};
+use nom::IResult::*;
+use std::str;
+
+named!(token<&str>,
+ map_res!(
+ alpha, str::from_utf8));
+
+fn slice2str(input: Vec<&[u8]>) -> Vec<&str> {
+ let res: Vec<&str> = input.iter()
+ .map(|x| str::from_utf8(x).unwrap())
+ .collect();
+ res
+}
+
+named!(args<Vec<&str> >,
+ map!(
+ separated_list!(
+ multispace, alpha),
+ slice2str));
+
+fn modifier<'a>(s: &'a str) -> Result<u32, ()> {
match s {
"Mod1Mask" => Ok(xlib::Mod4Mask),
"Mod2Mask" => Ok(xlib::Mod4Mask),
@@ -53,7 +74,7 @@ pub fn parse<'a>(dotwm: &mut DotWM, bindings: &mut BindingHash, input: &'a str)
Some(cmd) => {
match cmd {
&"add-binding" => {
- let modifier = str_to_modifier(args[1]).unwrap();
+ let modifier = modifier(args[1]).unwrap();
let key = str_to_key(args[2]).unwrap();
let func = str_to_func(args[3]).unwrap();
let arguments = &args[4..];
@@ -68,3 +89,14 @@ pub fn parse<'a>(dotwm: &mut DotWM, bindings: &mut BindingHash, input: &'a str)
}
"ok"
}
+
+#[test]
+fn parse_test() {
+ assert_eq!(token(&b"exec"[..]), IResult::Done(&b""[..], "exec"));
+}
+
+#[test]
+fn parse_args() {
+ assert_eq!(args(&b"hola chau"[..]),
+ IResult::Done(&b""[..], vec!["hola", "chau"]));
+}