Skip to content

Commit

Permalink
provide API to list supported language versions (#490)
Browse files Browse the repository at this point in the history
Closes #363
  • Loading branch information
OmarTawfik committed Jun 2, 2023
1 parent 15c34a7 commit ea8e7e7
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 26 deletions.
5 changes: 5 additions & 0 deletions .changeset/chilled-ways-worry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"changelog": minor
---

provide API to list supported language versions
9 changes: 7 additions & 2 deletions crates/codegen/syntax/src/rust_lib_code_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,10 @@ impl CodeGenerator {
InvalidProductionVersion(ProductionKind),
}}
{versions_array}
impl Language {{
pub fn new(version: Version) -> Result<Self, Error> {{
{versions_array}
if VERSIONS.contains(&version.to_string().as_str()) {{
Ok(Self {{
{version_flag_initializers},
Expand All @@ -86,6 +87,10 @@ impl CodeGenerator {
&self.version
}}
pub fn supported_versions() -> Vec<Version> {{
return VERSIONS.iter().map(|v| Version::parse(v).unwrap()).collect();
}}
pub fn parse(&self, production_kind: ProductionKind, input: &str) -> Result<ParseOutput, Error> {{
let output = match production_kind {{
{scanner_invocations},
Expand All @@ -107,7 +112,7 @@ impl CodeGenerator {
language_title = &language.title,
versions_array = {
let versions = language.versions.iter().map(|v| v.to_string());
quote! { static VERSIONS: &'static [&'static str] = &[ #(#versions),* ]; }
quote! { const VERSIONS: &'static [&'static str] = &[ #(#versions),* ]; }
},
version_flag_initializers = self.version_flag_initializers(),
scanner_invocations = self.scanner_invocations(),
Expand Down
10 changes: 8 additions & 2 deletions crates/codegen/syntax/src/typescript_lib_code_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,12 @@ impl CodeGenerator {
}}
}}
{versions_array}
#[napi]
impl Language {{
#[napi(constructor)]
pub fn new(version: String) -> Result<Self, napi::Error> {{
{versions_array}
let version = Version::parse(&version).map_err(|_| Error::InvalidSemanticVersion(version))?;
if VERSIONS.contains(&version.to_string().as_str()) {{
Ok(Self {{
Expand All @@ -101,6 +102,11 @@ impl CodeGenerator {
self.version.to_string()
}}
#[napi]
pub fn supported_versions() -> Vec<String> {{
return VERSIONS.iter().map(|v| v.to_string()).collect();
}}
#[napi]
pub fn parse(&self, production_kind: ProductionKind, input: String) -> Result<ParseOutput, napi::Error> {{
let input = input.as_str();
Expand All @@ -121,7 +127,7 @@ impl CodeGenerator {
version_flag_declarations = self.version_flag_declarations(),
versions_array = {
let versions = language.versions.iter().map(|v| v.to_string());
quote! { static VERSIONS: &'static [&'static str] = &[ #(#versions),* ]; }
quote! { const VERSIONS: &'static [&'static str] = &[ #(#versions),* ]; }
},
version_flag_initializers = self.version_flag_initializers(),
language_title = &language.title,
Expand Down
29 changes: 18 additions & 11 deletions crates/solidity/outputs/cargo/crate/src/generated/language.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/solidity/outputs/cargo/tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
mod cst_output;
#[cfg(test)]
mod errors;
#[cfg(test)]
mod versions;
11 changes: 11 additions & 0 deletions crates/solidity/outputs/cargo/tests/src/versions/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use semver::Version;
use slang_solidity::language::Language;

#[test]
fn list_supported_versions() {
let versions = Language::supported_versions();

assert_eq!(false, versions.is_empty());
assert_eq!(false, versions.contains(&Version::parse("0.0.0").unwrap()));
assert_eq!(true, versions.contains(&Version::parse("0.4.11").unwrap()));
}
27 changes: 16 additions & 11 deletions crates/solidity/outputs/npm/crate/src/generated/language.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions crates/solidity/outputs/npm/tests/src/versions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import test from "ava";
import { Language } from "@nomicfoundation/slang/language";

test("list supported versions", (t) => {
const versions = Language.supportedVersions();

t.true(versions.length > 0);
t.true(versions.includes("0.4.11"));
t.false(versions.includes("0.0.0"));
});

0 comments on commit ea8e7e7

Please sign in to comment.