Skip to content

Commit

Permalink
feat: support typed class constant
Browse files Browse the repository at this point in the history
Signed-off-by: azjezz <azjezz@protonmail.com>
  • Loading branch information
azjezz committed Aug 12, 2024
1 parent 81cc662 commit 24208bd
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 15 deletions.
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "php_codegen"
version = "0.1.0"
version = "0.2.0"
edition = "2021"
description = "Generate PHP code from Rust using a fluent API 🐘 🦀"
readme = "README.md"
Expand All @@ -15,3 +15,6 @@ keywords = ["php", "codegen", "code-generation", "php-rust-tools"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

[dev-dependencies]
pretty_assertions = "1.4.0"
8 changes: 2 additions & 6 deletions src/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,7 @@ impl Generator for Body {
if self.semicolon_for_empty {
code.push(';');
} else {
code.push_str(" {");
code.push('\n');
code.push_str(&indentation.indent("// empty body", level + 1));
code.push('\n');
code.push_str(&indentation.indent("}", level));
code.push_str(" {}");
}

code.push('\n');
Expand All @@ -87,7 +83,7 @@ impl<T: ToString> From<Vec<T>> for Body {
let body = body.clone();

body.iter()
.map(|line| indentation.indent(&line.to_string(), level))
.map(|line| indentation.indent(line.to_string(), level))
.collect::<Vec<String>>()
.join("\n")
})),
Expand Down
29 changes: 24 additions & 5 deletions src/constant.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::attribute::AttributeGroup;
use crate::comment::Document;
use crate::data_type::DataType;
use crate::literal::Value;
use crate::modifiers::Modifier;
use crate::modifiers::VisibilityModifier;
Expand Down Expand Up @@ -85,6 +86,7 @@ pub struct ClassConstant {
pub attributes: Vec<AttributeGroup>,
pub visibility: Option<VisibilityModifier>,
pub modifiers: Vec<Modifier>,
pub data_type: Option<DataType>,
pub name: String,
pub value: Value,
}
Expand All @@ -96,6 +98,7 @@ impl ClassConstant {
attributes: vec![],
visibility: None,
modifiers: vec![],
data_type: None,
name: name.to_string(),
value: Value::Null,
}
Expand Down Expand Up @@ -143,6 +146,12 @@ impl ClassConstant {
self
}

pub fn typed<T: Into<DataType>>(mut self, data_type: T) -> Self {
self.data_type = Some(data_type.into());

self
}

pub fn valued<T: Into<Value>>(mut self, value: T) -> Self {
self.value = value.into();

Expand Down Expand Up @@ -172,11 +181,20 @@ impl Generator for ClassConstant {
code.push_str(&format!("{} ", modifier.generate(indentation, level)));
}

code.push_str(&format!(
"const {} = {};\n",
self.name,
self.value.generate(indentation, level)
));
if let Some(data_type) = &self.data_type {
code.push_str(&format!(
"const {} {} = {};\n",
data_type.generate(indentation, level),
self.name,
self.value.generate(indentation, level)
));
} else {
code.push_str(&format!(
"const {} = {};\n",
self.name,
self.value.generate(indentation, level)
));
}

code
}
Expand Down Expand Up @@ -205,6 +223,7 @@ impl<T: ToString, Tv: Into<Value>> From<(T, Tv)> for ClassConstant {
attributes: vec![],
visibility: None,
modifiers: vec![],
data_type: None,
name: name.to_string(),
value: value.into(),
}
Expand Down
6 changes: 3 additions & 3 deletions tests/complete.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ function hello(
return 'Hello ' . $firstname . ' ' . $lastname . '!';
}

function nothing(): void {
// empty body
}
function nothing(): void {}

function format(
string $template,
Expand Down Expand Up @@ -91,6 +89,8 @@ abstract class Example extends Foo\Bar\Baz implements Foo\Bar\BazInterface

public const D = false;

public const bool E = false;

private string $foo;

protected string $bar;
Expand Down
8 changes: 8 additions & 0 deletions tests/complete.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use pretty_assertions::assert_eq;

use php_codegen::attribute::AttributeGroup;
use php_codegen::class::Class;
use php_codegen::comment::Document;
Expand Down Expand Up @@ -129,6 +131,12 @@ fn test_code_generation() {
.constant(ClassConstant::new("B").valued(()).protected())
.constant(ClassConstant::new("C").valued(1).private())
.constant(ClassConstant::new("D").valued(false).public())
.constant(
ClassConstant::new("E")
.typed(DataType::Boolean)
.valued(false)
.public(),
)
.property(Property::new("foo").typed(DataType::String).private())
.property(Property::new("bar").typed(DataType::String).protected())
.property(
Expand Down

0 comments on commit 24208bd

Please sign in to comment.