Skip to content

Commit

Permalink
[tutasdk] service calls
Browse files Browse the repository at this point in the history
* CompressedString is still not supported!
* RustGenerator produces service files which are expanded into service
  trait implementations by a declarative macro
* EntityFacade gained the ability to encrypt entities
* limited CreateSession ( without offline login and 2FA ) for testing
  purposes

Co-authored-by: sug <[email protected]>
Co-authored-by: jhm <[email protected]>
Co-authored-by: paw-hub <[email protected]>
Co-authored-by: ivk <[email protected]>
Co-authored-by: nig <[email protected]>
  • Loading branch information
5 people committed Oct 14, 2024
1 parent 0375dbd commit ef4cea6
Show file tree
Hide file tree
Showing 55 changed files with 5,206 additions and 1,074 deletions.
84 changes: 73 additions & 11 deletions buildSrc/RustGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import { AssociationType, Type } from "../src/common/api/common/EntityConstants.
*/
export function generateRustType({ type, modelName }) {
let typeName = mapTypeName(type.name, modelName)
let buf = `#[derive(uniffi::Record, Clone, Serialize, Deserialize, Debug)]
let buf = `#[derive(uniffi::Record, Clone, Serialize, Deserialize)]
#[cfg_attr(test, derive(PartialEq, Debug))]
pub struct ${typeName} {\n`
for (let [valueName, valueProperties] of Object.entries(type.values)) {
const rustType = rustValueType(valueName, type, valueProperties)
Expand Down Expand Up @@ -59,20 +60,81 @@ pub struct ${typeName} {\n`
if (type.encrypted || Object.values(type.values).some((v) => v.encrypted)) {
buf += `\tpub _finalIvs: HashMap<String, FinalIv>,\n`
}

buf += "}"
buf += `
impl Entity for ${typeName} {
fn type_ref() -> TypeRef {
TypeRef {
app: "${modelName}",
type_: "${typeName}",
}
}
}
`

return buf + "\n\n"
}

buf += "\n\n"
export function generateRustServiceDefinition(appName, appVersion, services) {
let imports = new Set([
"#![allow(unused_imports, dead_code, unused_variables)]",
"use crate::ApiCallError;",
"use crate::entities::Entity;",
"use crate::services::{PostService, GetService, PutService, DeleteService, Service, Executor, ExtraServiceParams};",
"use crate::rest_client::HttpMethod;",
"use crate::services::hidden::Nothing;",
])
const code = services
.map((s) => {
let serviceDefinition = `
pub struct ${s.name};
buf += `impl Entity for ${typeName} {\n`
buf += "\tfn type_ref() -> TypeRef {\n"
buf += `\t\tTypeRef {\n`
buf += `\t\t\tapp: "${modelName}",\n`
buf += `\t\t\ttype_: "${typeName}",\n`
buf += `\t\t}\n`
buf += "\t}\n"
buf += "}"
crate::service_impl!(declare, ${s.name}, "${appName}/${s.name.toLowerCase()}", ${appVersion});
`

function getTypeRef(dataType) {
if (dataType) {
return `Some(${dataType}::type_ref())`
} else {
return "None"
}
}

function addImports(appName, input, output) {
if (input) {
imports.add(`use crate::entities::${appName}::${input};`)
}
if (output) {
imports.add(`use crate::entities::${appName}::${output};`)
}
}

function makeImpl(name, input, output) {
addImports(appName, input, output)
return `crate::service_impl!(${name}, ${s.name}, ${input ?? "()"}, ${output ?? "()"});\n`
}

if (s.bodyTypes.POST_IN || s.bodyTypes.POST_OUT) {
serviceDefinition += makeImpl("POST", s.bodyTypes.POST_IN, s.bodyTypes.POST_OUT)
}

if (s.bodyTypes.GET_IN || s.bodyTypes.GET_OUT) {
serviceDefinition += makeImpl("GET", s.bodyTypes.GET_IN, s.bodyTypes.GET_OUT)
}

if (s.bodyTypes.PUT_IN || s.bodyTypes.PUT_OUT) {
serviceDefinition += makeImpl("PUT", s.bodyTypes.PUT_IN, s.bodyTypes.PUT_OUT)
}

if (s.bodyTypes.DELETE_IN || s.bodyTypes.DELETE_OUT) {
serviceDefinition += makeImpl("DELETE", s.bodyTypes.DELETE_IN, s.bodyTypes.DELETE_OUt)
}

return buf
return serviceDefinition
})
.join("\n")
return Array.from(imports).join("\n") + code
}

/**
Expand Down
Loading

0 comments on commit ef4cea6

Please sign in to comment.