diff --git a/Cargo.toml b/Cargo.toml index 4f35267..26adae4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" @@ -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" diff --git a/src/body.rs b/src/body.rs index ad2e288..9f4a78e 100644 --- a/src/body.rs +++ b/src/body.rs @@ -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'); @@ -87,7 +83,7 @@ impl From> 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::>() .join("\n") })), diff --git a/src/constant.rs b/src/constant.rs index 73ebc5e..655aafa 100644 --- a/src/constant.rs +++ b/src/constant.rs @@ -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; @@ -85,6 +86,7 @@ pub struct ClassConstant { pub attributes: Vec, pub visibility: Option, pub modifiers: Vec, + pub data_type: Option, pub name: String, pub value: Value, } @@ -96,6 +98,7 @@ impl ClassConstant { attributes: vec![], visibility: None, modifiers: vec![], + data_type: None, name: name.to_string(), value: Value::Null, } @@ -143,6 +146,12 @@ impl ClassConstant { self } + pub fn typed>(mut self, data_type: T) -> Self { + self.data_type = Some(data_type.into()); + + self + } + pub fn valued>(mut self, value: T) -> Self { self.value = value.into(); @@ -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 } @@ -205,6 +223,7 @@ impl> From<(T, Tv)> for ClassConstant { attributes: vec![], visibility: None, modifiers: vec![], + data_type: None, name: name.to_string(), value: value.into(), } diff --git a/tests/complete.php b/tests/complete.php index 509fed6..8f92caa 100644 --- a/tests/complete.php +++ b/tests/complete.php @@ -49,9 +49,7 @@ function hello( return 'Hello ' . $firstname . ' ' . $lastname . '!'; } -function nothing(): void { - // empty body -} +function nothing(): void {} function format( string $template, @@ -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; diff --git a/tests/complete.rs b/tests/complete.rs index 9f88ee4..47a5df6 100644 --- a/tests/complete.rs +++ b/tests/complete.rs @@ -1,3 +1,5 @@ +use pretty_assertions::assert_eq; + use php_codegen::attribute::AttributeGroup; use php_codegen::class::Class; use php_codegen::comment::Document; @@ -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(