Skip to content

Commit

Permalink
fix: make clippy not angry with rust codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
unimonkiez committed Nov 12, 2023
1 parent ecac983 commit 8ca1704
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 47 deletions.
90 changes: 54 additions & 36 deletions cli/src/languages/rust/__snapshots__/codegen-rust.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`python codegen cache can generate also with model 1`] = `
"extern crate memorix_client_redis;
exports[`rust codegen cache can generate also with model 1`] = `
"#![allow(dead_code)]
extern crate memorix_client_redis;
#[derive(Clone, memorix_client_redis::Serialize, memorix_client_redis::Deserialize)]
pub struct User {
Expand Down Expand Up @@ -58,8 +59,9 @@ impl Memorix {
}"
`;
exports[`python codegen cache can generate with inline object type 1`] = `
"extern crate memorix_client_redis;
exports[`rust codegen cache can generate with inline object type 1`] = `
"#![allow(dead_code)]
extern crate memorix_client_redis;
#[derive(Clone, memorix_client_redis::Serialize, memorix_client_redis::Deserialize)]
pub struct CacheUserPayload {
Expand Down Expand Up @@ -111,8 +113,9 @@ impl Memorix {
}"
`;
exports[`python codegen cache can generate with inline types 1`] = `
"extern crate memorix_client_redis;
exports[`rust codegen cache can generate with inline types 1`] = `
"#![allow(dead_code)]
extern crate memorix_client_redis;
#[derive(Clone)]
#[allow(non_snake_case)]
Expand Down Expand Up @@ -156,8 +159,9 @@ impl Memorix {
}"
`;
exports[`python codegen cache can generate with inline types and convert to CamelCase 1`] = `
"extern crate memorix_client_redis;
exports[`rust codegen cache can generate with inline types and convert to CamelCase 1`] = `
"#![allow(dead_code)]
extern crate memorix_client_redis;
#[derive(Clone, memorix_client_redis::Serialize, memorix_client_redis::Deserialize)]
pub struct CacheUserByNumberPayload {
Expand Down Expand Up @@ -208,8 +212,9 @@ impl Memorix {
}"
`;
exports[`python codegen cache can generate with no key 1`] = `
"extern crate memorix_client_redis;
exports[`rust codegen cache can generate with no key 1`] = `
"#![allow(dead_code)]
extern crate memorix_client_redis;
#[derive(Clone, memorix_client_redis::Serialize, memorix_client_redis::Deserialize)]
pub struct CacheUserPayload {
Expand Down Expand Up @@ -261,8 +266,9 @@ impl Memorix {
}"
`;
exports[`python codegen cache can have options 1`] = `
"extern crate memorix_client_redis;
exports[`rust codegen cache can have options 1`] = `
"#![allow(dead_code)]
extern crate memorix_client_redis;
#[derive(Clone)]
#[allow(non_snake_case)]
Expand Down Expand Up @@ -306,8 +312,9 @@ impl Memorix {
}"
`;
exports[`python codegen config can be null 1`] = `
"extern crate memorix_client_redis;
exports[`rust codegen config can be null 1`] = `
"#![allow(dead_code)]
extern crate memorix_client_redis;
#[derive(Clone)]
#[allow(non_snake_case)]
Expand Down Expand Up @@ -338,8 +345,9 @@ impl Memorix {
}"
`;
exports[`python codegen config can generate 1`] = `
"extern crate memorix_client_redis;
exports[`rust codegen config can generate 1`] = `
"#![allow(dead_code)]
extern crate memorix_client_redis;
#[derive(Clone)]
#[allow(non_snake_case)]
Expand Down Expand Up @@ -376,8 +384,9 @@ impl Memorix {
}"
`;
exports[`python codegen enum can generate 1`] = `
"extern crate memorix_client_redis;
exports[`rust codegen enum can generate 1`] = `
"#![allow(dead_code)]
extern crate memorix_client_redis;
#[allow(non_camel_case_types, clippy::upper_case_acronyms)]
#[derive(Clone, memorix_client_redis::Serialize, memorix_client_redis::Deserialize, PartialEq, std::fmt::Debug)]
Expand All @@ -388,8 +397,9 @@ pub enum Animals {
}"
`;
exports[`python codegen model can generate from 2 models 1`] = `
"extern crate memorix_client_redis;
exports[`rust codegen model can generate from 2 models 1`] = `
"#![allow(dead_code)]
extern crate memorix_client_redis;
#[derive(Clone, memorix_client_redis::Serialize, memorix_client_redis::Deserialize)]
pub struct User1 {
Expand All @@ -405,8 +415,9 @@ pub struct User2 {
}"
`;
exports[`python codegen model can generate from model 1`] = `
"extern crate memorix_client_redis;
exports[`rust codegen model can generate from model 1`] = `
"#![allow(dead_code)]
extern crate memorix_client_redis;
#[derive(Clone, memorix_client_redis::Serialize, memorix_client_redis::Deserialize)]
pub struct User {
Expand All @@ -417,8 +428,9 @@ pub struct User {
}"
`;
exports[`python codegen model can generate from model with array 1`] = `
"extern crate memorix_client_redis;
exports[`rust codegen model can generate from model with array 1`] = `
"#![allow(dead_code)]
extern crate memorix_client_redis;
#[derive(Clone, memorix_client_redis::Serialize, memorix_client_redis::Deserialize)]
pub struct UserChildren {
Expand All @@ -437,8 +449,9 @@ pub struct User {
}"
`;
exports[`python codegen model can generate from model within model 1`] = `
"extern crate memorix_client_redis;
exports[`rust codegen model can generate from model within model 1`] = `
"#![allow(dead_code)]
extern crate memorix_client_redis;
#[derive(Clone, memorix_client_redis::Serialize, memorix_client_redis::Deserialize)]
pub struct UserPapa {
Expand All @@ -454,8 +467,9 @@ pub struct User {
}"
`;
exports[`python codegen model can generate from model within model within a model 1`] = `
"extern crate memorix_client_redis;
exports[`rust codegen model can generate from model within model within a model 1`] = `
"#![allow(dead_code)]
extern crate memorix_client_redis;
#[derive(Clone, memorix_client_redis::Serialize, memorix_client_redis::Deserialize)]
pub struct UserPapaMama {
Expand All @@ -480,8 +494,9 @@ pub struct User {
}"
`;
exports[`python codegen namespace can generate 1`] = `
"extern crate memorix_client_redis;
exports[`rust codegen namespace can generate 1`] = `
"#![allow(dead_code)]
extern crate memorix_client_redis;
#[derive(Clone, memorix_client_redis::Serialize, memorix_client_redis::Deserialize)]
pub struct UserCacheBioPayload {
Expand Down Expand Up @@ -619,8 +634,9 @@ impl Memorix {
}"
`;
exports[`python codegen pubsub can generate with inline types 1`] = `
"extern crate memorix_client_redis;
exports[`rust codegen pubsub can generate with inline types 1`] = `
"#![allow(dead_code)]
extern crate memorix_client_redis;
#[derive(Clone)]
#[allow(non_snake_case)]
Expand Down Expand Up @@ -664,8 +680,9 @@ impl Memorix {
}"
`;
exports[`python codegen task can generate with inline types 1`] = `
"extern crate memorix_client_redis;
exports[`rust codegen task can generate with inline types 1`] = `
"#![allow(dead_code)]
extern crate memorix_client_redis;
#[derive(Clone)]
#[allow(non_snake_case)]
Expand Down Expand Up @@ -709,8 +726,9 @@ impl Memorix {
}"
`;
exports[`python codegen task can have options 1`] = `
"extern crate memorix_client_redis;
exports[`rust codegen task can have options 1`] = `
"#![allow(dead_code)]
extern crate memorix_client_redis;
#[derive(Clone)]
#[allow(non_snake_case)]
Expand Down
2 changes: 1 addition & 1 deletion cli/src/languages/rust/codegen-rust.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const codegenRust: (schema: string, moreConfig?: string) => Promise<string> = (
codegen({ schemaFilePath: "schema.memorix" }).catch(rej);
});

describe("python codegen", () => {
describe("rust codegen", () => {
describe("model", () => {
it("can generate from model", async () => {
expect(
Expand Down
21 changes: 11 additions & 10 deletions cli/src/languages/rust/rust.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,30 +70,30 @@ const valueToCode: (value: ValueType, isType: boolean) => string = (
value,
isType
) => {
let valuePython;
let valueRust;
if (value.type === ValueTypes.simple) {
if (value.name === "string") {
valuePython = "String";
valueRust = "String";
} else if (value.name === "boolean") {
valuePython = "bool";
valueRust = "bool";
} else if (value.name === "float") {
valuePython = "f32";
valueRust = "f32";
} else if (value.name === "int") {
valuePython = "i32";
valueRust = "i32";
} else if (isType) {
valuePython = `${value.name}`;
valueRust = `${value.name}`;
} else {
valuePython = `${value.name}`;
valueRust = `${value.name}`;
}
} else if (value.type === ValueTypes.array) {
valuePython = `Vec<${valueToCode(value.value, isType)}>`;
valueRust = `Vec<${valueToCode(value.value, isType)}>`;
} else {
throw new Error(
"Shouldn't get here, all inline objects became models, maybe forgot 'flatBlocks()?'"
);
}

return `${value.isOptional ? "Option<" : ""}${valuePython}${
return `${value.isOptional ? "Option<" : ""}${valueRust}${
value.isOptional ? ">" : ""
}`;
};
Expand Down Expand Up @@ -425,7 +425,8 @@ export const codegen: (namespaces: Namespace) => string = (
const importCode = ([] as string[])
.concat([`extern crate memorix_client_redis;`])
.join("\n");
return `${importCode}
return `#![allow(dead_code)]
${importCode}
${code}`;
};

0 comments on commit 8ca1704

Please sign in to comment.