Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 24208bd

Browse files
committed
feat: support typed class constant
Signed-off-by: azjezz <azjezz@protonmail.com>
1 parent 81cc662 commit 24208bd

File tree

5 files changed

+41
-15
lines changed

5 files changed

+41
-15
lines changed

‎Cargo.toml‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "php_codegen"
3-
version = "0.1.0"
3+
version = "0.2.0"
44
edition = "2021"
55
description = "Generate PHP code from Rust using a fluent API 🐘 🦀"
66
readme = "README.md"
@@ -15,3 +15,6 @@ keywords = ["php", "codegen", "code-generation", "php-rust-tools"]
1515
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1616

1717
[dependencies]
18+
19+
[dev-dependencies]
20+
pretty_assertions = "1.4.0"

‎src/body.rs‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,7 @@ impl Generator for Body {
6060
if self.semicolon_for_empty {
6161
code.push(';');
6262
} else {
63-
code.push_str(" {");
64-
code.push('\n');
65-
code.push_str(&indentation.indent("// empty body", level + 1));
66-
code.push('\n');
67-
code.push_str(&indentation.indent("}", level));
63+
code.push_str(" {}");
6864
}
6965

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

8985
body.iter()
90-
.map(|line| indentation.indent(&line.to_string(), level))
86+
.map(|line| indentation.indent(line.to_string(), level))
9187
.collect::<Vec<String>>()
9288
.join("\n")
9389
})),

‎src/constant.rs‎

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::attribute::AttributeGroup;
22
use crate::comment::Document;
3+
use crate::data_type::DataType;
34
use crate::literal::Value;
45
use crate::modifiers::Modifier;
56
use crate::modifiers::VisibilityModifier;
@@ -85,6 +86,7 @@ pub struct ClassConstant {
8586
pub attributes: Vec<AttributeGroup>,
8687
pub visibility: Option<VisibilityModifier>,
8788
pub modifiers: Vec<Modifier>,
89+
pub data_type: Option<DataType>,
8890
pub name: String,
8991
pub value: Value,
9092
}
@@ -96,6 +98,7 @@ impl ClassConstant {
9698
attributes: vec![],
9799
visibility: None,
98100
modifiers: vec![],
101+
data_type: None,
99102
name: name.to_string(),
100103
value: Value::Null,
101104
}
@@ -143,6 +146,12 @@ impl ClassConstant {
143146
self
144147
}
145148

149+
pub fn typed<T: Into<DataType>>(mut self, data_type: T) -> Self {
150+
self.data_type = Some(data_type.into());
151+
152+
self
153+
}
154+
146155
pub fn valued<T: Into<Value>>(mut self, value: T) -> Self {
147156
self.value = value.into();
148157

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

175-
code.push_str(&format!(
176-
"const {} = {};\n",
177-
self.name,
178-
self.value.generate(indentation, level)
179-
));
184+
if let Some(data_type) = &self.data_type {
185+
code.push_str(&format!(
186+
"const {} {} = {};\n",
187+
data_type.generate(indentation, level),
188+
self.name,
189+
self.value.generate(indentation, level)
190+
));
191+
} else {
192+
code.push_str(&format!(
193+
"const {} = {};\n",
194+
self.name,
195+
self.value.generate(indentation, level)
196+
));
197+
}
180198

181199
code
182200
}
@@ -205,6 +223,7 @@ impl<T: ToString, Tv: Into<Value>> From<(T, Tv)> for ClassConstant {
205223
attributes: vec![],
206224
visibility: None,
207225
modifiers: vec![],
226+
data_type: None,
208227
name: name.to_string(),
209228
value: value.into(),
210229
}

‎tests/complete.php‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,7 @@ function hello(
4949
return 'Hello ' . $firstname . '' . $lastname . '!';
5050
}
5151

52-
function nothing(): void {
53-
// empty body
54-
}
52+
function nothing(): void {}
5553

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

9290
public const D = false;
9391

92+
public const bool E = false;
93+
9494
private string $foo;
9595

9696
protected string $bar;

‎tests/complete.rs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use pretty_assertions::assert_eq;
2+
13
use php_codegen::attribute::AttributeGroup;
24
use php_codegen::class::Class;
35
use php_codegen::comment::Document;
@@ -129,6 +131,12 @@ fn test_code_generation() {
129131
.constant(ClassConstant::new("B").valued(()).protected())
130132
.constant(ClassConstant::new("C").valued(1).private())
131133
.constant(ClassConstant::new("D").valued(false).public())
134+
.constant(
135+
ClassConstant::new("E")
136+
.typed(DataType::Boolean)
137+
.valued(false)
138+
.public(),
139+
)
132140
.property(Property::new("foo").typed(DataType::String).private())
133141
.property(Property::new("bar").typed(DataType::String).protected())
134142
.property(

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /