-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add `types_output` Co-authored-by: pashleyy <[email protected]> * Update docs/config/options.md Co-authored-by: Sasial <[email protected]> * Set default Co-authored-by: Sasial <[email protected]> * Remove clone Co-authored-by: Sasial <[email protected]> * Fix formatting * Fix formatting * Fix formatting * Add TypeScript output * Remove formatting change * Fix output * Make type optional * Add Selene lints * Refactoring * Lune tests * Add newline to Selene tests * Fix Lune tests * Wrap tagged enums in parentheses * Formatting * Formatting --------- Co-authored-by: pashleyy <[email protected]> Co-authored-by: Sasial <[email protected]>
- Loading branch information
1 parent
2632e9e
commit 608c066
Showing
26 changed files
with
319 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
use crate::config::{Config, TyDecl}; | ||
|
||
use super::Output; | ||
|
||
struct TypesOutput<'src> { | ||
config: &'src Config<'src>, | ||
tabs: u32, | ||
buf: String, | ||
} | ||
|
||
impl Output for TypesOutput<'_> { | ||
fn push(&mut self, s: &str) { | ||
self.buf.push_str(s); | ||
} | ||
|
||
fn indent(&mut self) { | ||
self.tabs += 1; | ||
} | ||
|
||
fn dedent(&mut self) { | ||
self.tabs -= 1; | ||
} | ||
|
||
fn push_indent(&mut self) { | ||
for _ in 0..self.tabs { | ||
self.push("\t"); | ||
} | ||
} | ||
} | ||
|
||
impl<'a> TypesOutput<'a> { | ||
pub fn new(config: &'a Config) -> Self { | ||
Self { | ||
config, | ||
tabs: 0, | ||
buf: String::new(), | ||
} | ||
} | ||
|
||
fn push_tydecl(&mut self, tydecl: &TyDecl) { | ||
let name = &tydecl.name; | ||
let ty = &tydecl.ty; | ||
|
||
self.push_indent(); | ||
self.push(&format!("export type {name} = ")); | ||
self.push_ty(ty); | ||
self.push("\n"); | ||
} | ||
|
||
fn push_tydecls(&mut self) { | ||
for tydecl in self.config.tydecls.iter() { | ||
self.push_tydecl(tydecl); | ||
} | ||
} | ||
|
||
pub fn output(mut self) -> String { | ||
self.push_line(&format!( | ||
"-- Types generated by Zap v{} (https://github.com/red-blox/zap)", | ||
env!("CARGO_PKG_VERSION") | ||
)); | ||
|
||
self.push_tydecls(); | ||
self.push_line("return nil"); | ||
|
||
self.buf | ||
} | ||
} | ||
|
||
pub fn code(config: &Config) -> String { | ||
TypesOutput::new(config).output() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
use crate::config::{Config, TyDecl}; | ||
|
||
use super::{ConfigProvider, Output}; | ||
|
||
struct TypesOutput<'src> { | ||
config: &'src Config<'src>, | ||
tabs: u32, | ||
buf: String, | ||
} | ||
|
||
impl Output for TypesOutput<'_> { | ||
fn push(&mut self, s: &str) { | ||
self.buf.push_str(s); | ||
} | ||
|
||
fn indent(&mut self) { | ||
self.tabs += 1; | ||
} | ||
|
||
fn dedent(&mut self) { | ||
self.tabs -= 1; | ||
} | ||
|
||
fn push_indent(&mut self) { | ||
for _ in 0..self.tabs { | ||
self.push("\t"); | ||
} | ||
} | ||
} | ||
|
||
impl ConfigProvider for TypesOutput<'_> { | ||
fn get_config(&self) -> &Config { | ||
self.config | ||
} | ||
} | ||
|
||
impl<'a> TypesOutput<'a> { | ||
pub fn new(config: &'a Config) -> Self { | ||
Self { | ||
config, | ||
tabs: 0, | ||
buf: String::new(), | ||
} | ||
} | ||
|
||
fn push_tydecl(&mut self, tydecl: &TyDecl) { | ||
let name = &tydecl.name; | ||
let ty = &tydecl.ty; | ||
|
||
self.push_indent(); | ||
self.push(&format!("export type {name} = ")); | ||
self.push_ty(ty); | ||
self.push("\n"); | ||
} | ||
|
||
fn push_tydecls(&mut self) { | ||
for tydecl in self.config.tydecls.iter() { | ||
self.push_tydecl(tydecl); | ||
} | ||
} | ||
|
||
pub fn output(mut self) -> String { | ||
self.push_line(&format!( | ||
"// Types generated by Zap v{} (https://github.com/red-blox/zap)", | ||
env!("CARGO_PKG_VERSION") | ||
)); | ||
|
||
self.push_tydecls(); | ||
|
||
self.buf | ||
} | ||
} | ||
|
||
pub fn code(config: &Config) -> Option<String> { | ||
Some(TypesOutput::new(config).output()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
source: zap/tests/selene/mod.rs | ||
expression: types_diagnostics | ||
input_file: zap/tests/files/function.zap | ||
--- | ||
[] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
source: zap/tests/selene/mod.rs | ||
expression: types_diagnostics | ||
input_file: zap/tests/files/function_mutiple_rets.zap | ||
--- | ||
[] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
source: zap/tests/selene/mod.rs | ||
expression: types_diagnostics | ||
input_file: zap/tests/files/function_one_unnamed_parameter.zap | ||
--- | ||
[] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
source: zap/tests/selene/mod.rs | ||
expression: types_diagnostics | ||
input_file: zap/tests/files/function_two_unnamed_parameters.zap | ||
--- | ||
[] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
source: zap/tests/selene/mod.rs | ||
expression: types_diagnostics | ||
input_file: zap/tests/files/many_assorted.zap | ||
--- | ||
[] |
Oops, something went wrong.