Skip to content

Commit 2433479

Browse files
authored
feat: Basic plotnik-macros crate (#45)
1 parent a1401eb commit 2433479

9 files changed

Lines changed: 577 additions & 393 deletions

File tree

Cargo.lock

Lines changed: 54 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
resolver = "2"
44

5-
members = ["crates/plotnik-cli", "crates/plotnik-lib", "crates/plotnik-langs"]
5+
members = ["crates/plotnik-cli", "crates/plotnik-lib", "crates/plotnik-langs", "crates/plotnik-macros"]

crates/plotnik-cli/src/commands/debug/source.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ pub fn resolve_lang(
2525
lang: &Option<String>,
2626
_source_text: &Option<String>,
2727
source_file: &Option<PathBuf>,
28-
) -> Lang {
28+
) -> &'static Lang {
2929
if let Some(name) = lang {
30-
return Lang::from_name(name).unwrap_or_else(|| {
30+
return plotnik_langs::from_name(name).unwrap_or_else(|| {
3131
eprintln!("error: unknown language: {}", name);
3232
std::process::exit(1);
3333
});
@@ -37,7 +37,7 @@ pub fn resolve_lang(
3737
&& path.as_os_str() != "-"
3838
&& let Some(ext) = path.extension().and_then(|e| e.to_str())
3939
{
40-
return Lang::from_extension(ext).unwrap_or_else(|| {
40+
return plotnik_langs::from_ext(ext).unwrap_or_else(|| {
4141
eprintln!(
4242
"error: cannot infer language from extension '.{}', use --lang",
4343
ext
@@ -50,10 +50,10 @@ pub fn resolve_lang(
5050
std::process::exit(1);
5151
}
5252

53-
pub fn parse_tree(source: &str, lang: Lang) -> tree_sitter::Tree {
53+
pub fn parse_tree(source: &str, lang: &Lang) -> tree_sitter::Tree {
5454
let mut parser = tree_sitter::Parser::new();
5555
parser
56-
.set_language(&lang.language())
56+
.set_language(&lang.ts_lang)
5757
.expect("failed to set language");
5858
parser.parse(source, None).expect("failed to parse source")
5959
}
Lines changed: 58 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
1-
use plotnik_langs::Lang;
2-
31
pub fn run() {
4-
let langs = Lang::all();
2+
let langs = plotnik_langs::all();
53
println!("Supported languages ({}):", langs.len());
64
for lang in langs {
7-
println!(" {}", lang.name());
5+
println!(" {}", lang.name);
86
}
97
}
108

119
#[cfg(test)]
1210
mod tests {
13-
use plotnik_langs::Lang;
14-
15-
fn smoke_test(lang: Lang, source: &str, expected_root: &str) {
11+
fn smoke_test(lang: &plotnik_langs::Lang, source: &str, expected_root: &str) {
1612
let mut parser = tree_sitter::Parser::new();
17-
parser.set_language(&lang.language()).unwrap();
13+
parser.set_language(&lang.ts_lang).unwrap();
1814
let tree = parser.parse(source, None).unwrap();
1915
let root = tree.root_node();
2016
assert_eq!(root.kind(), expected_root);
@@ -24,56 +20,68 @@ mod tests {
2420
#[test]
2521
#[cfg(feature = "bash")]
2622
fn smoke_parse_bash() {
27-
smoke_test(Lang::Bash, "echo hello", "program");
23+
smoke_test(plotnik_langs::bash(), "echo hello", "program");
2824
}
2925

3026
#[test]
3127
#[cfg(feature = "c")]
3228
fn smoke_parse_c() {
33-
smoke_test(Lang::C, "int main() { return 0; }", "translation_unit");
29+
smoke_test(
30+
plotnik_langs::c(),
31+
"int main() { return 0; }",
32+
"translation_unit",
33+
);
3434
}
3535

3636
#[test]
3737
#[cfg(feature = "cpp")]
3838
fn smoke_parse_cpp() {
39-
smoke_test(Lang::Cpp, "int main() { return 0; }", "translation_unit");
39+
smoke_test(
40+
plotnik_langs::cpp(),
41+
"int main() { return 0; }",
42+
"translation_unit",
43+
);
4044
}
4145

4246
#[test]
4347
#[cfg(feature = "csharp")]
4448
fn smoke_parse_csharp() {
45-
smoke_test(Lang::CSharp, "class Foo { }", "compilation_unit");
49+
smoke_test(plotnik_langs::csharp(), "class Foo { }", "compilation_unit");
4650
}
4751

4852
#[test]
4953
#[cfg(feature = "css")]
5054
fn smoke_parse_css() {
51-
smoke_test(Lang::Css, "body { color: red; }", "stylesheet");
55+
smoke_test(plotnik_langs::css(), "body { color: red; }", "stylesheet");
5256
}
5357

5458
#[test]
5559
#[cfg(feature = "elixir")]
5660
fn smoke_parse_elixir() {
57-
smoke_test(Lang::Elixir, "defmodule Foo do end", "source");
61+
smoke_test(plotnik_langs::elixir(), "defmodule Foo do end", "source");
5862
}
5963

6064
#[test]
6165
#[cfg(feature = "go")]
6266
fn smoke_parse_go() {
63-
smoke_test(Lang::Go, "package main", "source_file");
67+
smoke_test(plotnik_langs::go(), "package main", "source_file");
6468
}
6569

6670
#[test]
6771
#[cfg(feature = "haskell")]
6872
fn smoke_parse_haskell() {
69-
smoke_test(Lang::Haskell, "main = putStrLn \"hello\"", "haskell");
73+
smoke_test(
74+
plotnik_langs::haskell(),
75+
"main = putStrLn \"hello\"",
76+
"haskell",
77+
);
7078
}
7179

7280
#[test]
7381
#[cfg(feature = "hcl")]
7482
fn smoke_parse_hcl() {
7583
smoke_test(
76-
Lang::Hcl,
84+
plotnik_langs::hcl(),
7785
"resource \"aws_instance\" \"x\" {}",
7886
"config_file",
7987
);
@@ -82,20 +90,20 @@ mod tests {
8290
#[test]
8391
#[cfg(feature = "html")]
8492
fn smoke_parse_html() {
85-
smoke_test(Lang::Html, "<html></html>", "document");
93+
smoke_test(plotnik_langs::html(), "<html></html>", "document");
8694
}
8795

8896
#[test]
8997
#[cfg(feature = "java")]
9098
fn smoke_parse_java() {
91-
smoke_test(Lang::Java, "class Foo {}", "program");
99+
smoke_test(plotnik_langs::java(), "class Foo {}", "program");
92100
}
93101

94102
#[test]
95103
#[cfg(feature = "javascript")]
96104
fn smoke_parse_javascript() {
97105
smoke_test(
98-
Lang::JavaScript,
106+
plotnik_langs::javascript(),
99107
"function hello() { return 42; }",
100108
"program",
101109
);
@@ -104,99 +112,110 @@ mod tests {
104112
#[test]
105113
#[cfg(feature = "json")]
106114
fn smoke_parse_json() {
107-
smoke_test(Lang::Json, r#"{"key": "value"}"#, "document");
115+
smoke_test(plotnik_langs::json(), r#"{"key": "value"}"#, "document");
108116
}
109117

110118
#[test]
111119
#[cfg(feature = "kotlin")]
112120
fn smoke_parse_kotlin() {
113-
smoke_test(Lang::Kotlin, "fun main() {}", "source_file");
121+
smoke_test(plotnik_langs::kotlin(), "fun main() {}", "source_file");
114122
}
115123

116124
#[test]
117125
#[cfg(feature = "lua")]
118126
fn smoke_parse_lua() {
119-
smoke_test(Lang::Lua, "print('hello')", "chunk");
127+
smoke_test(plotnik_langs::lua(), "print('hello')", "chunk");
120128
}
121129

122130
#[test]
123131
#[cfg(feature = "nix")]
124132
fn smoke_parse_nix() {
125-
smoke_test(Lang::Nix, "{ x = 1; }", "source_code");
133+
smoke_test(plotnik_langs::nix(), "{ x = 1; }", "source_code");
126134
}
127135

128136
#[test]
129137
#[cfg(feature = "php")]
130138
fn smoke_parse_php() {
131-
smoke_test(Lang::Php, "<?php echo 1;", "program");
139+
smoke_test(plotnik_langs::php(), "<?php echo 1;", "program");
132140
}
133141

134142
#[test]
135143
#[cfg(feature = "python")]
136144
fn smoke_parse_python() {
137-
smoke_test(Lang::Python, "def hello():\n return 42", "module");
145+
smoke_test(
146+
plotnik_langs::python(),
147+
"def hello():\n return 42",
148+
"module",
149+
);
138150
}
139151

140152
#[test]
141153
#[cfg(feature = "ruby")]
142154
fn smoke_parse_ruby() {
143-
smoke_test(Lang::Ruby, "def hello; end", "program");
155+
smoke_test(plotnik_langs::ruby(), "def hello; end", "program");
144156
}
145157

146158
#[test]
147159
#[cfg(feature = "rust")]
148160
fn smoke_parse_rust() {
149-
smoke_test(Lang::Rust, "fn main() {}", "source_file");
161+
smoke_test(plotnik_langs::rust(), "fn main() {}", "source_file");
150162
}
151163

152164
#[test]
153165
#[cfg(feature = "scala")]
154166
fn smoke_parse_scala() {
155-
smoke_test(Lang::Scala, "object Main {}", "compilation_unit");
167+
smoke_test(plotnik_langs::scala(), "object Main {}", "compilation_unit");
156168
}
157169

158170
#[test]
159171
#[cfg(feature = "solidity")]
160172
fn smoke_parse_solidity() {
161-
smoke_test(Lang::Solidity, "contract Foo {}", "source_file");
173+
smoke_test(plotnik_langs::solidity(), "contract Foo {}", "source_file");
162174
}
163175

164176
#[test]
165177
#[cfg(feature = "swift")]
166178
fn smoke_parse_swift() {
167-
smoke_test(Lang::Swift, "func main() {}", "source_file");
179+
smoke_test(plotnik_langs::swift(), "func main() {}", "source_file");
168180
}
169181

170182
#[test]
171183
#[cfg(feature = "typescript")]
172184
fn smoke_parse_typescript() {
173-
smoke_test(Lang::TypeScript, "const x: number = 42;", "program");
185+
smoke_test(
186+
plotnik_langs::typescript(),
187+
"const x: number = 42;",
188+
"program",
189+
);
174190
}
175191

176192
#[test]
177193
#[cfg(feature = "typescript")]
178194
fn smoke_parse_tsx() {
179-
smoke_test(Lang::Tsx, "const x = <div />;", "program");
195+
smoke_test(plotnik_langs::tsx(), "const x = <div />;", "program");
180196
}
181197

182198
#[test]
183199
#[cfg(feature = "yaml")]
184200
fn smoke_parse_yaml() {
185-
smoke_test(Lang::Yaml, "key: value", "stream");
201+
smoke_test(plotnik_langs::yaml(), "key: value", "stream");
186202
}
187203

188204
#[test]
189205
#[cfg(feature = "javascript")]
190206
fn lang_from_name() {
191-
assert_eq!(Lang::from_name("js"), Some(Lang::JavaScript));
192-
assert_eq!(Lang::from_name("JavaScript"), Some(Lang::JavaScript));
193-
assert_eq!(Lang::from_name("unknown"), None);
207+
assert_eq!(plotnik_langs::from_name("js").unwrap().name, "javascript");
208+
assert_eq!(
209+
plotnik_langs::from_name("JavaScript").unwrap().name,
210+
"javascript"
211+
);
212+
assert!(plotnik_langs::from_name("unknown").is_none());
194213
}
195214

196215
#[test]
197216
#[cfg(feature = "javascript")]
198217
fn lang_from_extension() {
199-
assert_eq!(Lang::from_extension("js"), Some(Lang::JavaScript));
200-
assert_eq!(Lang::from_extension("mjs"), Some(Lang::JavaScript));
218+
assert_eq!(plotnik_langs::from_ext("js").unwrap().name, "javascript");
219+
assert_eq!(plotnik_langs::from_ext("mjs").unwrap().name, "javascript");
201220
}
202221
}

0 commit comments

Comments
 (0)