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

Generate string enum types. #169

Merged
merged 5 commits into from
Feb 21, 2023
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ dist
**/.vs
**/.mypy_cache
packages/rust/**/target/
packages/rust/**/Cargo.lock
41 changes: 41 additions & 0 deletions generator-plugins/rust/rust_commons.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

from typing import Dict, List

import generator.model as model


def generate_custom_enum() -> Dict[str, List[str]]:
return {
"CustomStringEnum": [
"/// This type allows extending any string enum to support custom values.",
"#[derive(Serialize, Deserialize, PartialEq, Debug)]",
"#[serde(untagged)]",
"pub enum CustomStringEnum<T> {",
" /// The value is one of the known enum values.",
" Known(T),",
" /// The value is custom.",
" Custom(String),",
"}",
"",
],
"CustomIntEnum": [
"/// This type allows extending any integer enum to support custom values.",
"#[derive(Serialize, Deserialize, PartialEq, Debug)]",
"#[serde(untagged)]",
"pub enum CustomIntEnum<T> {",
" /// The value is one of the known enum values.",
" Known(T),",
" /// The value is custom.",
" Custom(i64),",
"}",
"",
],
}


def generate_commons(model: model.LSPModel) -> Dict[str, List[str]]:
return {
**generate_custom_enum(),
}
52 changes: 45 additions & 7 deletions generator-plugins/rust/rust_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,69 @@

import generator.model as model

from .rust_lang_utils import to_upper_camel_case
from .rust_lang_utils import lines_to_doc_comments, to_upper_camel_case


def generate_int_enum(enum: model.Enum) -> List[str]:
is_int = all(isinstance(item.value, int) for item in enum.values)
if not is_int:
raise Exception("Enum is not an integer enum")

return [
doc = enum.documentation.splitlines(keepends=False) if enum.documentation else []
lines = lines_to_doc_comments(doc) + [
f"#[derive(Serialize_repr, Deserialize_repr, PartialEq, Debug)]",
f"#[repr(i64)]",
f"pub enum {enum.name} " "{",
*[
f" {to_upper_camel_case(item.name)} = {item.value},"
for item in enum.values
],
"}",
]

indent = " " * 4
for item in enum.values:
doc = (
item.documentation.splitlines(keepends=False) if item.documentation else []
)
lines += [f"{indent}{line}" for line in lines_to_doc_comments(doc)]
lines += [f"{indent}{to_upper_camel_case(item.name)} = {item.value},", ""]

lines += ["}"]

return lines


def generate_string_enum(enum: model.Enum) -> List[str]:
is_string = all(isinstance(item.value, str) for item in enum.values)
if not is_string:
raise Exception("Enum is not a string enum")

doc = enum.documentation.splitlines(keepends=False) if enum.documentation else []
lines = lines_to_doc_comments(doc) + [
f"#[derive(Serialize, Deserialize, PartialEq, Debug)]",
f"pub enum {enum.name} " "{",
]

indent = " " * 4
for item in enum.values:
doc = (
item.documentation.splitlines(keepends=False) if item.documentation else []
)
lines += [f"{indent}{line}" for line in lines_to_doc_comments(doc)]
lines += [
f'{indent}#[serde(rename = "{item.value}")]',
f"{indent}{to_upper_camel_case(item.name)},",
"",
]

lines += ["}"]

return lines


def generate_enum(enum: model.Enum) -> List[str]:
is_int = all(isinstance(item.value, int) for item in enum.values)
lines = []
if is_int:
lines += generate_int_enum(enum)
else:
lines += generate_string_enum(enum)
return lines


Expand Down
13 changes: 9 additions & 4 deletions generator-plugins/rust/rust_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import generator.model as model

from .rust_commons import generate_commons
from .rust_enum import generate_enums
from .rust_file_header import license_header
from .rust_lang_utils import lines_to_comments
Expand All @@ -30,11 +31,15 @@ def generate_package_code(spec: model.LSPModel) -> List[str]:

def generate_lib_rs(spec: model.LSPModel) -> List[str]:
lines = lines_to_comments(license_header())
lines += ["use serde_repr::*;", ""]
lines += ["use serde_repr::*;", "use serde::{Serialize, Deserialize};", ""]

enums = generate_enums(spec.enumerations)
for enum_name in enums:
lines += enums[enum_name]
types = {
**generate_commons(spec),
**generate_enums(spec.enumerations),
}

for name in types:
lines += types[name]
lines += [""]

return "\n".join(lines)
12 changes: 6 additions & 6 deletions generator/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ importlib-metadata==6.0.0 \
--hash=sha256:7efb448ec9a5e313a57655d35aa54cd3e01b7e1fbcf72dce1bf06119420f5bad \
--hash=sha256:e354bedeb60efa6affdcc8ae121b73544a7aa74156d047311948f6d711cd378d
# via jsonschema
importlib-resources==5.10.2 \
--hash=sha256:7d543798b0beca10b6a01ac7cafda9f822c54db9e8376a6bf57e0cbd74d486b6 \
--hash=sha256:e4a96c8cc0339647ff9a5e0550d9f276fc5a01ffa276012b58ec108cfd7b8484
importlib-resources==5.12.0 \
--hash=sha256:4be82589bf5c1d7999aedf2a45159d10cb3ca4f19b2271f8792bc8e6da7b22f6 \
--hash=sha256:7b1deeebbf351c7578e09bf2f63fa2ce8b5ffec296e0d349139d43cca061a81a
# via
# -r ./generator/requirements.in
# jsonschema
Expand Down Expand Up @@ -61,9 +61,9 @@ typing-extensions==4.5.0 \
# via
# importlib-metadata
# jsonschema
zipp==3.13.0 \
--hash=sha256:23f70e964bc11a34cef175bc90ba2914e1e4545ea1e3e2f67c079671883f9cb6 \
--hash=sha256:e8b2a36ea17df80ffe9e2c4fda3f693c3dad6df1697d3cd3af232db680950b0b
zipp==3.14.0 \
--hash=sha256:188834565033387710d046e3fe96acfc9b5e86cbca7f39ff69cf21a4128198b7 \
--hash=sha256:9e5421e176ef5ab4c0ad896624e87a7b2f07aca746c9b2aa305952800cb8eecb
# via
# importlib-metadata
# importlib-resources
154 changes: 0 additions & 154 deletions packages/rust/lsprotocol/Cargo.lock

This file was deleted.

2 changes: 1 addition & 1 deletion packages/rust/lsprotocol/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ repository = "https://github.com/microsoft/lsprotocol"
readme = "README.md"

[dependencies]
serde = "1.0.152"
serde = {version ="1.0.152", features = ["derive"]}
serde_json = "1.0.93"
serde_repr = "0.1.10"
url = "2.3.1"
Loading