Skip to content

feature: interfaces #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion examples/simple.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,3 @@ function format(
array_filter($args, static fn ($arg) => $arg !== null)
));
}

14 changes: 14 additions & 0 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::fmt::Display;
use crate::class::Class;
use crate::constant::Constant;
use crate::function::Function;
use crate::interface::Interface;
use crate::literal::Value;
use crate::Generator;
use crate::Indentation;
Expand All @@ -17,6 +18,7 @@ pub struct File {
pub functions: Vec<Function>,
pub constants: Vec<Constant>,
pub classes: Vec<Class>,
pub interfaces: Vec<Interface>,
}

impl File {
Expand All @@ -30,6 +32,7 @@ impl File {
constants: vec![],
functions: vec![],
classes: vec![],
interfaces: vec![],
}
}

Expand Down Expand Up @@ -80,6 +83,12 @@ impl File {

self
}

pub fn interface(mut self, interface: Interface) -> Self {
self.interfaces.push(interface.into());

self
}
}

impl Generator for File {
Expand Down Expand Up @@ -150,6 +159,11 @@ impl Generator for File {
code.push_str("\n");
}

for interface in &self.interfaces {
code.push_str(&interface.generate(indentation, level));
code.push_str("\n");
}

code
}
}
Expand Down
83 changes: 83 additions & 0 deletions src/interface.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
use crate::{comment::Document, attribute::AttributeGroup, Generator, Indentation, method::Method};

#[derive(Debug)]
pub struct Interface {
pub documentation: Option<Document>,
pub attributes: Vec<AttributeGroup>,
pub name: String,
pub extends: Option<String>,
pub methods: Vec<Method>,
}

impl Interface {
pub fn new<T: ToString>(name: T) -> Self {
Self {
documentation: None,
attributes: vec![],
name: name.to_string(),
extends: None,
methods: vec![],
}
}

pub fn document(mut self, documentation: Document) -> Self {
self.documentation = Some(documentation);

self
}

pub fn attributes(mut self, attributes: AttributeGroup) -> Self {
self.attributes.push(attributes);

self
}

pub fn extend<T: ToString>(mut self, extends: T) -> Self {
self.extends = Some(extends.to_string());

self
}

pub fn method(mut self, method: Method) -> Self {
self.methods.push(method.public());

self
}
}

impl Generator for Interface {
fn generate(&self, indentation: Indentation, level: usize) -> String {
let mut code = String::new();

if let Some(documentation) = &self.documentation {
code.push_str(&documentation.generate(indentation, level));
}

for attribute in &self.attributes {
code.push_str(&attribute.generate(indentation, level));
}

code.push_str(&format!("interface {}", self.name));

if let Some(extends) = &self.extends {
code.push_str(&format!(" extends {}", extends));
}

code.push_str("\n{\n");

if !self.methods.is_empty() {
code.push_str(
&self
.methods
.iter()
.map(|method| method.generate(indentation, level + 1))
.collect::<Vec<String>>()
.join("\n"),
);
}

code.push_str("}\n");

code
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub mod method;
pub mod modifiers;
pub mod parameter;
pub mod property;
pub mod interface;

#[derive(Debug, PartialEq, PartialOrd, Clone, Copy)]
pub enum Indentation {
Expand Down