Skip to content
This repository was archived by the owner on Nov 5, 2024. It is now read-only.

Commit 5d5a5ed

Browse files
committed
added escape_cpp_keyword filter
1 parent 9874034 commit 5d5a5ed

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ By default the following filters are available:
103103
+ `| camelcase_to_snakecase` - converts `FooBar` string to `foo_bar`
104104
+ `| snakecase_to_camelcase` - converts `foo_bar` string to `FooBar`
105105
+ `| escape_c_keyword` - appends `_` to a string if it's a C keyword
106+
+ `| escape_cpp_keyword` - appends `_` to a string if it's a C++ keyword
106107
+ `| escape_rust_keyword` - appends `_` to a string if it's a Rust keyword
107108
+ `| render_comment: "//", 4` - renders array of strings (like `node.comment`) to a string where each line is prefixed with "//" and has 4 spaces padding (except for the first line, it has no padding)
108109

src/filters/escape_cpp_keyword.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use liquid_core::Result;
2+
use liquid_core::Runtime;
3+
use liquid_core::{Display_filter, Filter, FilterReflection, ParseFilter};
4+
use liquid_core::{Value, ValueView};
5+
6+
#[derive(Clone, ParseFilter, FilterReflection)]
7+
#[filter(
8+
name = "escape_cpp_keyword",
9+
description = "Escape C++ keyword by adding trailing _",
10+
parsed(EscapeCppKeywordFilter)
11+
)]
12+
pub struct EscapeCppKeyword;
13+
14+
#[derive(Debug, Default, Display_filter)]
15+
#[name = "escape_cpp_keyword"]
16+
struct EscapeCppKeywordFilter;
17+
18+
impl Filter for EscapeCppKeywordFilter {
19+
fn evaluate(&self, input: &dyn ValueView, _runtime: &dyn Runtime) -> Result<Value> {
20+
let name = input.to_kstr();
21+
let output = crate::helpers::escape_cpp_keyword(&name);
22+
Ok(Value::scalar(output))
23+
}
24+
}

src/filters/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ pub use escape_rust_keyword::EscapeRustKeyword;
1313
mod escape_c_keyword;
1414
pub use escape_c_keyword::EscapeCKeyword;
1515

16+
mod escape_cpp_keyword;
17+
pub use escape_cpp_keyword::EscapeCppKeyword;
18+
1619
pub fn invalid_input<S>(cause: S) -> liquid_core::Error
1720
where
1821
S: Into<liquid_core::model::KString>,
@@ -27,5 +30,6 @@ pub(crate) fn all() -> Vec<Box<dyn liquid_core::parser::ParseFilter>> {
2730
crate::filters::SnakecaseToCamelcase.into(),
2831
crate::filters::EscapeRustKeyword.into(),
2932
crate::filters::EscapeCKeyword.into(),
33+
crate::filters::EscapeCppKeyword.into(),
3034
]
3135
}

src/helpers.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,18 @@ fn test_escape_c_keyword() {
7676
assert_eq!(escape_c_keyword("foo"), "foo");
7777
assert_eq!(escape_c_keyword("default"), "default_");
7878
}
79+
80+
pub fn escape_cpp_keyword(s: &str) -> String {
81+
match s {
82+
"default" | "operator" | "else" | "const" | "and" | "break" | "case" | "class"
83+
| "false" | "float" | "for" | "if" | "int" | "or" | "return" | "true" | "while"
84+
| "ERANGE" => format!("{}_", s),
85+
_ => s.to_string(),
86+
}
87+
}
88+
89+
#[test]
90+
fn test_escape_cpp_keyword() {
91+
assert_eq!(escape_cpp_keyword("foo"), "foo");
92+
assert_eq!(escape_cpp_keyword("default"), "default_");
93+
}

0 commit comments

Comments
 (0)