Skip to content

Commit

Permalink
move file & enum to their own files
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtbuilds committed Mar 24, 2024
1 parent 9e45e9f commit 7bf9423
Show file tree
Hide file tree
Showing 12 changed files with 219 additions and 125 deletions.
30 changes: 14 additions & 16 deletions mir/src/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct Class<T> {
/// Use `class_methods` in Rust.
pub class_methods: Vec<Function<T>>,
pub static_methods: Vec<Function<T>>,
pub public: bool,
pub vis: Visibility,

pub lifetimes: Vec<String>,
pub decorators: Vec<T>,
Expand All @@ -36,20 +36,18 @@ pub struct Field<T> {

impl Debug for Class<String> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let public = self.public;
write!(f, "Class {{ name: {name:?}, \
doc: {doc:?}, \
instance_fields: todo!, \
static_fields: todo!, \
constructors: todo!, \
class_methods: todo!, \
static_methods: todo!, \
public: {public}, \
lifetimes: todo!, \
superclasses: todo! }}",
name = self.name,
doc = self.doc,
)
f.debug_struct("Class")
.field("name", &self.name)
.field("doc", &self.doc)
.field("instance_fields", &self.instance_fields)
.field("static_fields", &self.static_fields)
.field("constructors", &self.constructors)
.field("class_methods", &self.class_methods)
.field("static_methods", &self.static_methods)
.field("vis", &self.vis)
.field("lifetimes", &self.lifetimes)
.field("superclasses", &self.superclasses)
.finish()
}
}

Expand All @@ -64,7 +62,7 @@ impl<T> Default for Class<T> {
constructors: vec![],
class_methods: vec![],
static_methods: vec![],
public: false,
vis: Visibility::Private,
lifetimes: vec![],
decorators: vec![],
superclasses: vec![],
Expand Down
19 changes: 19 additions & 0 deletions mir/src/enum.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use std::marker::PhantomData;
use crate::{Doc, Function, Ident, Visibility};

#[derive(Debug, Default)]
pub struct Enum<T> {
pub name: Ident,
pub doc: Option<Doc>,
pub variants: Vec<Variant>,
pub vis: Visibility,
/// Attributes in Rust
pub decorators: Vec<T>,
pub methods: Vec<Function<T>>,
}

#[derive(Debug)]
pub struct Variant {
pub name: Ident,
pub doc: Option<Doc>,
}
29 changes: 29 additions & 0 deletions mir/src/file.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use crate::{Class, Doc, Enum, Function, Import};

pub struct File<T> {
pub imports: Vec<Import>,
pub doc: Option<Doc>,
/// Code that is before function and class declarations
pub declaration: Option<T>,
pub classes: Vec<Class<T>>,
pub enums: Vec<Enum<T>>,
pub functions: Vec<Function<T>>,
/// Code that follows after the function and class declarations
pub code: Option<T>,
pub package: Option<String>,
}

impl<T> Default for File<T> where T: Default {
fn default() -> Self {
Self {
doc: None,
declaration: None,
classes: vec![],
enums: vec![],
functions: vec![],
code: None,
imports: vec![],
package: None,
}
}
}
20 changes: 9 additions & 11 deletions mir/src/function.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::fmt::{Debug, Formatter};

use crate::{Doc, Ident};
use crate::{Doc, Ident, Visibility};

/// Localized
pub enum ArgIdent {
Expand Down Expand Up @@ -163,7 +163,7 @@ pub struct Function<T> {
pub body: T,
pub doc: Option<Doc>,
pub async_: bool,
pub public: bool,
pub vis: Visibility,
/// #[...] in Rust
/// @... in Python
pub annotations: Vec<String>,
Expand All @@ -175,14 +175,12 @@ impl<T> Debug for Function<T>
T: Debug,
{
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "Function {{ name: {name:?}, args: debug print not impl, ret: {ret:?}, body: {body:?}, doc: {doc:?}, async_: {async_}, public: {public}, annotations: debug print not impl }}",
name = self.name,
ret = self.ret,
body = self.body,
doc = self.doc,
async_ = self.async_,
public = self.public
)
f.debug_struct("Function")
.field("name", &self.name)
.field("ret", &self.ret)
.field("args", &"..")
.field("body", &"..")
.finish()
}
}

Expand All @@ -198,7 +196,7 @@ impl<T> Default for Function<T>
body: T::default(),
doc: None,
async_: false,
public: false,
vis: Default::default(),
annotations: vec![],
generic: vec![],
}
Expand Down
32 changes: 31 additions & 1 deletion mir/src/ident.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
use std::fmt::Formatter;

use quote::TokenStreamExt;

/// Localized string
#[derive(Debug, Clone, Eq, PartialEq, Hash, Default)]
pub struct Ident(pub String);

impl Ident {
pub fn validate(&self) {
if self.0.contains(',') {
panic!("Ident cannot contain a comma: {}", self.0);
}
}
}

impl Ident {
pub fn new(s: &'static str) -> Self {
Ident(s.into())
Expand All @@ -20,4 +30,24 @@ impl PartialEq<str> for Ident {
fn eq(&self, other: &str) -> bool {
self.0 == *other
}
}
}

impl PartialEq<&str> for Ident {
fn eq(&self, other: &&str) -> bool {
self.0 == *other
}
}

impl quote::ToTokens for Ident {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
tokens.append(proc_macro2::Ident::new(&self.0, proc_macro2::Span::call_site()))
}
}

impl From<Ident> for proc_macro2::TokenStream {
fn from(val: Ident) -> Self {
let mut tok = proc_macro2::TokenStream::new();
tok.append(proc_macro2::Ident::new(&val.0, proc_macro2::Span::call_site()));
tok
}
}
64 changes: 61 additions & 3 deletions mir/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,48 @@ pub struct Import {
pub feature: Option<String>
}


impl Import {
pub fn package(path: &str) -> Self {
Self {
path: path.to_string(),
imports: vec![],
alias: None,
vis: Visibility::Private,
feature: None,
}
}

pub fn new(path: &str, imports: impl IntoIterator<Item=impl Into<ImportItem>>) -> Self {
Self {
path: path.to_string(),
imports: imports
.into_iter()
.map(|s| s.into())
.collect(),
alias: None,
vis: Visibility::Private,
feature: None,
}
}

pub fn alias(path: &str, alias: &str) -> Self {
Self {
path: path.to_string(),
imports: Vec::new(),
alias: Some(alias.to_string()),
vis: Visibility::Private,
feature: None,
}
}

pub fn public(mut self) -> Self {
self.vis = Visibility::Public;
self
}
}


pub struct ImportItem {
/// This might not conform to standard ident rules for the language, so its a string, not an ident.
pub name: String,
Expand All @@ -23,23 +65,39 @@ impl ImportItem {
pub fn alias(name: &str, alias: &str) -> Self {
Self { name: name.to_string(), alias: Some(alias.to_string()) }
}

pub fn validate(&self) -> Result<(), String> {
if self.name.is_empty() {
return Err("ImportItem name cannot be empty".to_string());
}
if self.name.chars().all(|c| c.is_digit(10)) {
return Err("ImportItem name cannot be all digits".to_string());
}
Ok(())
}
}

impl From<&String> for ImportItem {
fn from(s: &String) -> Self {
Self { name: s.clone(), alias: None }
let r = Self { name: s.clone(), alias: None };
r.validate().unwrap();
r
}
}

impl From<String> for ImportItem {
fn from(s: String) -> Self {
Self { name: s, alias: None }
let r = Self { name: s, alias: None };
r.validate().unwrap();
r
}
}

impl From<&str> for ImportItem {
fn from(s: &str) -> Self {
Self { name: s.to_string(), alias: None }
let r = Self { name: s.to_string(), alias: None };
r.validate().unwrap();
r
}
}

Expand Down
Loading

0 comments on commit 7bf9423

Please sign in to comment.