Skip to content
Snippets Groups Projects
Commit 02186451 authored by AdminXVII's avatar AdminXVII
Browse files

Use the std-native functions instead of hand-rolled ones

parent 1d02419d
No related branches found
No related tags found
No related merge requests found
use super::split_pattern;
use crate::parser::lexers::assignments::{KeyBuf, KeyIterator, TypeError}; use crate::parser::lexers::assignments::{KeyBuf, KeyIterator, TypeError};
use err_derive::Error; use err_derive::Error;
...@@ -14,7 +13,8 @@ pub enum FunctionParseError { ...@@ -14,7 +13,8 @@ pub enum FunctionParseError {
/// converted into a tuple consisting of a `KeyIterator` iterator, which will collect type /// converted into a tuple consisting of a `KeyIterator` iterator, which will collect type
/// information, and an optional description of the function. /// information, and an optional description of the function.
pub fn parse_function(arg: &str) -> (KeyIterator<'_>, Option<&str>) { pub fn parse_function(arg: &str) -> (KeyIterator<'_>, Option<&str>) {
let (args, description) = split_pattern(arg, "--"); let mut parts = arg.splitn(2, "--");
let (args, description) = (parts.next().unwrap().trim(), parts.next().map(str::trim));
(KeyIterator::new(args), description) (KeyIterator::new(args), description)
} }
......
...@@ -103,41 +103,3 @@ pub fn parse_and_validate<'b>( ...@@ -103,41 +103,3 @@ pub fn parse_and_validate<'b>(
StatementVariant::Default(statement) => parse(statement, builtins), StatementVariant::Default(statement) => parse(statement, builtins),
} }
} }
/// Splits a string into two, based on a given pattern. We know that the first string will always
/// exist, but if the pattern is not found, or no string follows the pattern, then the second
/// string will not exist. Useful for splitting the function expression by the "--" pattern.
fn split_pattern<'a>(arg: &'a str, pattern: &str) -> (&'a str, Option<&'a str>) {
match arg.find(pattern) {
Some(pos) => {
let args = &arg[..pos].trim();
let comment = &arg[pos + pattern.len()..].trim();
if comment.is_empty() {
(args, None)
} else {
(args, Some(comment))
}
}
None => (arg, None),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn statement_pattern_splitting() {
let (args, description) = split_pattern("a:int b:bool -- a comment", "--");
assert_eq!(args, "a:int b:bool");
assert_eq!(description, Some("a comment"));
let (args, description) = split_pattern("a --", "--");
assert_eq!(args, "a");
assert_eq!(description, None);
let (args, description) = split_pattern("a", "--");
assert_eq!(args, "a");
assert_eq!(description, None);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment