Skip to content
Open
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
7 changes: 7 additions & 0 deletions Cargo.lock

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

21 changes: 21 additions & 0 deletions libninja/tests/basic/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use ln_core::{OutputConfig, PackageConfig};

const BASIC: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/spec/basic.yaml");
const RECURLY: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/spec/recurly.yaml");
const KEYWORDS: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/spec/keywords.yaml");

const EXAMPLE: &str = include_str!("link_create_token.rs");

Expand Down Expand Up @@ -74,3 +75,23 @@ pub fn test_build_full_library_recurly() -> Result<()> {
};
generate_library(spec, opts)
}

#[test]
pub fn test_keywords_schema() -> Result<()> {
let yaml = File::open(KEYWORDS).unwrap();
let temp = tempfile::tempdir()?;

let spec: OpenAPI = serde_yaml::from_reader(yaml).unwrap();
let opts = OutputConfig {
dest_path: temp.path().to_path_buf(),
build_examples: false,
package_name: "keywords".to_string(),
service_name: "Keywords".to_string(),
language: Language::Rust,
config: Default::default(),
github_repo: Some("libninjacom/test".to_string()),
version: None,
derive: vec![],
};
generate_library(spec, opts)
}
48 changes: 48 additions & 0 deletions libninja/tests/spec/keywords.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
openapi: 3.0.1
servers:
- url: "{scheme}://example.com/keywords"
variables:
scheme:
description: "This OpenAPI schema names fields after Rust keywords."
enum:
- "https"
- "http"
default: "https"
info:
description: >-
This api uses Rust keywords in dangerous places.
version: 1.0.0
title: Rust keyword test
paths:
/:
get:
operationId: fetch-keywords
summary: Fetch keywords
responses:
"200":
description: Returns a struct with fields named after rust keywords.
content:
application/json:
schema:
$ref: "#/components/schemas/dangerNoodle"
components:
schemas:
dangerNoodle:
type: object
properties:
async:
type: boolean
enum:
type: string
final:
type: boolean
match:
type: integer
mut:
type: boolean
self:
type: string
type:
type: boolean
use:
type: boolean
1 change: 1 addition & 0 deletions mir_rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ syn = "2.0.48"
convert_case = "0.6.0"
regex = "1.10.3"
prettyplease = "0.2.16"
check_keyword = "0.2.0"
22 changes: 10 additions & 12 deletions mir_rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
use check_keyword::CheckKeyword;
use convert_case::{Case, Casing};
use proc_macro2::TokenStream;
use quote::quote;
use regex::{Captures, Regex};

use mir::{Doc, Ident, Literal, ParamKey, Visibility};

mod file;
mod class;
mod import;
mod function;
mod r#enum;
mod file;
mod function;
mod import;

/// Use this for codegen structs: Function, Class, etc.
pub trait ToRustCode {
Expand Down Expand Up @@ -40,7 +41,6 @@ impl ToRustCode for Visibility {
}
}


impl ToRustCode for Option<Doc> {
fn to_rust_code(self) -> TokenStream {
match self {
Expand Down Expand Up @@ -72,7 +72,6 @@ impl ToRustCode for ParamKey {
}
}


pub trait ToRustIdent {
fn to_rust_struct(&self) -> Ident;
fn to_rust_ident(&self) -> Ident;
Expand Down Expand Up @@ -132,7 +131,7 @@ fn sanitize(s: impl AsRef<str>) -> String {
c
})
.into();
if is_restricted(&s) {
if s.is_keyword() {
s += "_"
}
if s.chars().next().unwrap().is_numeric() {
Expand All @@ -147,17 +146,13 @@ fn sanitize_struct(s: impl AsRef<str>) -> Ident {
let original = s;
let s = rewrite_names(s);
let mut s = s.to_case(Case::Pascal);
if is_restricted(&s) {
if s.is_keyword() {
s += "Struct"
}
assert_valid_ident(&s, &original);
Ident(s)
}

pub fn is_restricted(s: &str) -> bool {
["type", "use", "ref", "self", "match", "final"].contains(&s)
}

fn assert_valid_ident(s: &str, original: &str) {
if s.chars().next().map(|c| c.is_numeric()).unwrap_or_default() {
panic!("Numeric identifier: {}", original)
Expand All @@ -177,7 +172,10 @@ mod tests {
#[test]
fn test_filename() {
let s = "SdAddress.contractor1099";
assert_eq!(String::from(s).to_rust_ident().0, "sd_address_contractor1099");
assert_eq!(
String::from(s).to_rust_ident().0,
"sd_address_contractor1099"
);
assert_eq!(sanitize_filename(s), "sd_address_contractor1099");
}
}
Expand Down