-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add support for enums * Emit C-style enums * Explain why enum is isize * Run cargo fmt
- Loading branch information
Showing
17 changed files
with
465 additions
and
81 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use super::utils::get_doc_lines; | ||
use super::Method; | ||
|
||
/// A fieldless enum declaration in an FFI module. | ||
#[derive(Clone, Serialize, Deserialize, Debug)] | ||
pub struct Enum { | ||
pub name: String, | ||
pub doc_lines: String, | ||
/// A list of variants of the enum. (name, discriminant, doc_lines) | ||
pub variants: Vec<(String, isize, String)>, | ||
pub methods: Vec<Method>, | ||
} | ||
|
||
impl From<&syn::ItemEnum> for Enum { | ||
/// Extract an [`Enum`] metadata value from an AST node. | ||
fn from(strct: &syn::ItemEnum) -> Enum { | ||
let mut last_discriminant = -1; | ||
Enum { | ||
name: strct.ident.to_string(), | ||
doc_lines: get_doc_lines(&strct.attrs), | ||
variants: strct | ||
.variants | ||
.iter() | ||
.map(|v| { | ||
let new_discriminant = v | ||
.discriminant | ||
.as_ref() | ||
.map(|d| { | ||
if let syn::Expr::Lit(syn::ExprLit { | ||
attrs: _, | ||
lit: syn::Lit::Int(lit_int), | ||
}) = &d.1 | ||
{ | ||
lit_int.base10_parse::<isize>().unwrap() | ||
} else { | ||
panic!("Expected a discriminant to be a constant integer"); | ||
} | ||
}) | ||
.unwrap_or_else(|| last_discriminant + 1); | ||
|
||
last_discriminant = new_discriminant; | ||
|
||
( | ||
v.ident.to_string(), | ||
new_discriminant, | ||
get_doc_lines(&v.attrs), | ||
) | ||
}) | ||
.collect(), | ||
methods: vec![], | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use insta::{self, Settings}; | ||
|
||
use quote::quote; | ||
use syn; | ||
|
||
use super::Enum; | ||
|
||
#[test] | ||
fn simple_enum() { | ||
let mut settings = Settings::new(); | ||
settings.set_sort_maps(true); | ||
|
||
settings.bind(|| { | ||
insta::assert_yaml_snapshot!(Enum::from( | ||
&syn::parse2(quote! { | ||
/// Some docs. | ||
enum MyLocalEnum { | ||
Abc, | ||
/// Some more docs. | ||
Def | ||
} | ||
}) | ||
.unwrap() | ||
)); | ||
}); | ||
} | ||
} |
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
16 changes: 16 additions & 0 deletions
16
core/src/ast/snapshots/diplomat_core__ast__enums__tests__simple_enum.snap
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,16 @@ | ||
--- | ||
source: core/src/ast/enums.rs | ||
expression: "Enum::from(&syn::parse2(quote! {\n /// Some docs.\n enum MyLocalEnum\n {\n Abc, /// Some more docs.\n Def\n }\n }).unwrap())" | ||
|
||
--- | ||
name: MyLocalEnum | ||
doc_lines: Some docs. | ||
variants: | ||
- - Abc | ||
- 0 | ||
- "" | ||
- - Def | ||
- 1 | ||
- Some more docs. | ||
methods: [] | ||
|
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
Oops, something went wrong.