Skip to content
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

provide API to list supported language versions #490

Merged
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
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"));
});