Skip to content

Commit

Permalink
feat(parser): add support for variables in variable declaration (#107)
Browse files Browse the repository at this point in the history
Added support for using variables in variable declaration values.

```
@hostname=localhost
@PORT=5000
@host={{hostname}}:{{port}}
GET {{host}}/users
```
  • Loading branch information
hougesen authored Jan 3, 2024
1 parent 51aa115 commit f8d8e10
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 10 deletions.
2 changes: 1 addition & 1 deletion hitt-parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ fn tokenize(
// move forward once since we don't care about the '@'
chrs.next();

if let Some((name, value)) = parse_variable_declaration(&mut chrs) {
if let Some((name, value)) = parse_variable_declaration(&mut chrs, &vars)? {
vars.insert(name, value);
continue;
}
Expand Down
89 changes: 80 additions & 9 deletions hitt-parser/src/variables/mod.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,64 @@
use crate::error::RequestParseError;

#[inline]
pub fn parse_variable_declaration(
chars: &mut core::iter::Enumerate<core::str::Chars>,
) -> Option<(String, String)> {
vars: &std::collections::HashMap<String, String>,
) -> Result<Option<(String, String)>, RequestParseError> {
let mut declaration = String::new();

let mut value = String::new();

let mut is_declaration = true;

for (_, ch) in chars {
while let Some((_, ch)) = chars.next() {
if ch == '=' && is_declaration {
is_declaration = false;
} else if is_declaration {
declaration.push(ch);
} else {
if ch == '{' {
// FIXME: remove cloning of enumerator
if let Some((var, jumps)) = parse_variable(&mut chars.clone()) {
if let Some(variable_value) = vars.get(&var) {
value.push_str(variable_value);

for _ in 0..jumps {
chars.next();
}

continue;
}

return Err(RequestParseError::VariableNotFound(var));
}
}

value.push(ch);
}
}

if is_declaration {
return None;
return Ok(None);
}

Some((declaration.trim().to_owned(), value.trim().to_owned()))
Ok(Some((
declaration.trim().to_owned(),
value.trim().to_owned(),
)))
}

#[cfg(test)]
mod test_parse_variable_declarations {
use crate::to_enum_chars;
use once_cell::sync::Lazy;

use crate::{error::RequestParseError, to_enum_chars};

use super::parse_variable_declaration;

static EMPTY_VARS: Lazy<std::collections::HashMap<String, String>> =
Lazy::new(std::collections::HashMap::new);

#[test]
fn it_should_parse_variable_declarations() {
for i in i8::MIN..i8::MAX {
Expand All @@ -40,7 +68,8 @@ mod test_parse_variable_declarations {
// NOTE: we do not start with a '@' here since it is expected to already be removed
let input = format!("{input_declaration}={input_value}");

let (key, value) = parse_variable_declaration(&mut to_enum_chars(&input))
let (key, value) = parse_variable_declaration(&mut to_enum_chars(&input), &EMPTY_VARS)
.expect("it to not return an error")
.expect("it to return a variable declaration");

assert_eq!(input_declaration, key);
Expand All @@ -63,7 +92,8 @@ mod test_parse_variable_declarations {
"{input_declaration}{extra_spaces}={extra_spaces}{input_value}{extra_spaces}"
);

let (key, value) = parse_variable_declaration(&mut to_enum_chars(&input))
let (key, value) = parse_variable_declaration(&mut to_enum_chars(&input), &EMPTY_VARS)
.expect("it to not return an error")
.expect("it to return a variable declaration");

assert_eq!(input_declaration, key);
Expand All @@ -72,10 +102,51 @@ mod test_parse_variable_declarations {
}

#[test]
fn it_should_include_an_equal_sign() {
fn it_must_include_an_equal_sign() {
let input = "mads hougesen";

assert_eq!(None, parse_variable_declaration(&mut to_enum_chars(input)));
let result = parse_variable_declaration(&mut to_enum_chars(input), &EMPTY_VARS)
.expect("it to not return an error");

assert_eq!(None, result);
}

#[test]
fn it_should_support_variables() {
let input = "host={{ hostname }}:{{ port }}";

{
let vars = std::collections::HashMap::from([
("hostname".to_owned(), "localhost".to_owned()),
("port".to_owned(), "5000".to_owned()),
]);

let (name, value) = parse_variable_declaration(&mut to_enum_chars(input), &vars)
.expect("it to not return an error")
.expect("it to be some");

assert_eq!(name, "host");
assert_eq!(value, "localhost:5000");
};

{
let result = parse_variable_declaration(&mut to_enum_chars(input), &EMPTY_VARS)
.expect_err("it should return RequestParseError::VariableNotFound");

assert!(
matches!(result, RequestParseError::VariableNotFound(var) if var == "hostname")
);
};

{
let vars =
std::collections::HashMap::from([("hostname".to_owned(), "localhost".to_owned())]);

let result = parse_variable_declaration(&mut to_enum_chars(input), &vars)
.expect_err("it should return RequestParseError::VariableNotFound");

assert!(matches!(result, RequestParseError::VariableNotFound(var) if var == "port"));
};
}
}

Expand Down

0 comments on commit f8d8e10

Please sign in to comment.