-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenderClient.ts
More file actions
80 lines (65 loc) · 2.39 KB
/
renderClient.ts
File metadata and controls
80 lines (65 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { ServersInterface, ServerInterface } from '@asyncapi/parser';
import { FormatHelpers } from '@asyncapi/modelina';
import { RenderFile } from './renderFile';
import { contentClientFn } from './renderClientFn';
import { appendLines } from './tool';
/** client code from server */
export function contentClient(server: ServerInterface, exchangeName: string): string {
const serverName = FormatHelpers.toSnakeCase(server.id());
let fnCode: string[] = [];
let enumCode: string[] = [];
// gather GeneratedCode
for (const channel of server.channels()) {
const result = contentClientFn(channel, exchangeName, serverName);
fnCode = fnCode.concat(result.fnCode);
enumCode = enumCode.concat(result.enumCode);
}
return `
use crate::model::*;
use serde::{Deserialize, Serialize};
use tokio::net::TcpStream;
use tokio_tungstenite::tungstenite::Result;
use tokio_tungstenite::{connect_async, MaybeTlsStream};
use typed_websocket::TypedWebSocketStream;
pub type Stream<INPUT, OUTPUT> = TypedWebSocketStream<MaybeTlsStream<TcpStream>, INPUT, OUTPUT>;
${appendLines(enumCode)}
#[derive(Debug)]
pub struct ${FormatHelpers.toPascalCase(exchangeName)}${FormatHelpers.toPascalCase(server.id())}Client {
base_url: String,
}
impl ${FormatHelpers.toPascalCase(exchangeName)}${FormatHelpers.toPascalCase(server.id())}Client {
/// connect to the ${exchangeName} websocket server
pub async fn new() -> Self {
Self {
base_url: "${server.url()}".to_string(),
}
}
${appendLines(fnCode)}
}
`.trimStart()
}
function contentModClientItem(server: ServerInterface): string {
return `
/// ${server.description()}
pub mod ${FormatHelpers.toSnakeCase(server.id())};
`.trim()
}
export function contentModClient(servers: ServersInterface): string {
let result = '';
for (const server of servers) {
result += contentModClientItem(server);
}
return result;
}
// TODO set up the render function where we can set up directories as well directly
export function renderClientDir(servers: ServersInterface, exchangeName: string): RenderFile[] {
let files: RenderFile[] = [];
// every server
for (const server of servers) {
const serverName = FormatHelpers.toSnakeCase(server.id());
// operations within conentClient
files = files.concat(new RenderFile(`src/client/${serverName}.rs`, contentClient(server, exchangeName)));
}
files = files.concat(new RenderFile(`src/client/mod.rs`, contentModClient(servers)));
return files;
}