1 /// List of dependency-based snippets for packages outside phobos and druntime.
2 module workspaced.com.snippets.external_builtin;
3 
4 import workspaced.com.snippets;
5 
6 static immutable PlainSnippet[] builtinVibeHttpSnippets = [
7 	{
8 		levels: [SnippetLevel.method],
9 		shortcut: "viberouter",
10 		title: "vibe.d router",
11 		documentation: "Basic router instance code with GET / path.\n\nReference: https://vibed.org/api/vibe.http.router/URLRouter",
12 		snippet: "auto ${1:router} = new URLRouter();\n${1:router}.get(\"/\", &${2:index});",
13 		imports: ["vibe.http.router"]
14 	},
15 	{
16 		levels: [SnippetLevel.method],
17 		shortcut: "vibeserver",
18 		title: "vibe.d HTTP server",
19 		documentation: "Basic vibe.d HTTP server startup code.\n\nReference: https://vibed.org/api/vibe.http.server/",
20 		snippet: "auto ${3:settings} = new HTTPServerSettings();\n"
21 			~ "${3:settings}.port = ${1:3000};\n"
22 			~ "${3:settings}.bindAddresses = ${2:[\"::1\", \"127.0.0.1\"]};\n"
23 			~ "\n"
24 			~ "auto ${4:router} = new URLRouter();\n"
25 			~ "${4:router}.get(\"/\", &${5:index});\n"
26 			~ "\n"
27 			~ "listenHTTP(${3:settings}, ${4:router});\n",
28 		imports: ["vibe.http.server", "vibe.http.router"]
29 	},
30 	{
31 		levels: [SnippetLevel.method],
32 		shortcut: "vibeget",
33 		title: "vibe.d GET request",
34 		documentation: "Code for a simple low-level async GET request.\n\nReference: https://vibed.org/api/vibe.http.client/requestHTTP",
35 		snippet: "requestHTTP(URL(\"$1\"), null, (scope HTTPClientResponse res) {\n"
36 			~ "\t${2:// TODO: check res.statusCode and read response into parent scope variables.}\n"
37 			~ "});",
38 		imports: ["vibe.http.client"]
39 	},
40 	{
41 		levels: [SnippetLevel.method],
42 		shortcut: "viberequest",
43 		title: "vibe.d HTTP request (POST/GET/PUT/...)",
44 		documentation: "Code for a simple low-level async HTTP request.\n\nReference: https://vibed.org/api/vibe.http.client/requestHTTP",
45 		snippet: "requestHTTP(URL(\"$1\"), (scope HTTPClientRequest req) {\n"
46 			~ "\treq.method = HTTPMethod.${2:POST};\n"
47 			~ "\t${3:// TODO: write request body}\n"
48 			~ "}, (scope HTTPClientResponse res) {\n"
49 			~ "\t${4:// TODO: check res.statusCode and read response into parent scope variables.}\n"
50 			~ "});",
51 		imports: ["vibe.http.client"]
52 	},
53 	{
54 		levels: [SnippetLevel.method],
55 		shortcut: "vibegetstring",
56 		title: "vibe.d GET request into string",
57 		documentation: "Code for a simple async GET request storing the full response body in a string.\n\nReference: https://vibed.org/api/vibe.http.client/requestHTTP",
58 		snippet: "string ${1:text};\n"
59 			~ "requestHTTP(URL(\"$2\"), null, (scope HTTPClientResponse res) {\n"
60 			~ "\t${3:// TODO: check res.statusCode}\n"
61 			~ "\t${1:text} = res.bodyReader.readAllUTF8();\n"
62 			~ "});",
63 		imports: ["vibe.http.client"]
64 	},
65 	{
66 		levels: [SnippetLevel.method],
67 		shortcut: "vibegetjson",
68 		title: "vibe.d GET request as json",
69 		documentation: "Code for a simple async GET request storing the full response body in a string.\n\nReference: https://vibed.org/api/vibe.http.client/requestHTTP",
70 		snippet: "Json ${1:json};\n"
71 			~ "requestHTTP(URL(\"$2\"), null, (scope HTTPClientResponse res) {\n"
72 			~ "\t${3:// TODO: check res.statusCode}\n"
73 			~ "\t${1:json} = res.readJson(); // TODO: possibly want to add .deserializeJson!T\n"
74 			~ "});",
75 		imports: ["vibe.data.json", "vibe.http.client"]
76 	},
77 ];
78 
79 static immutable PlainSnippet[] builtinMirSerdeSnippets = [
80 	{
81 		levels: [SnippetLevel.type, SnippetLevel.mixinTemplate],
82 		shortcut: "deserializeFromIon",
83 		title: "mir-ion deserializeFromIon",
84 		documentation: "Custom mir-ion struct deserializion code.\n\n"
85 			~ "**Note:** this is an advanced construct and you probably don't "
86 			~ "need to use this unless you have very specific needs. You can "
87 			~ "probably use a proxy instead.",
88 		snippet: "@safe pure scope\n"
89 			~ "IonException deserializeFromIon(scope const char[][] symbolTable, IonDescribedValue value) {\n"
90 			~ "\timport mir.deser.ion : deserializeIon;\n"
91 			~ "\timport mir.ion.type_code : IonTypeCode;\n"
92 			~ "\n"
93 			~ "\tif (value.descriptor.type == IonTypeCode.struct_) {\n"
94 			~ "\t\t${1:this.impl} = deserializeIon!${2:DeserializeType}(symbolTable, value);$0\n"
95 			~ "\t} else {\n"
96 			~ "\t\treturn ionException(IonErrorCode.expectedStructValue);\n"
97 			~ "\t}\n"
98 			~ "\treturn null;\n"
99 			~ "}\n",
100 		imports: ["mir.ion.exception", "mir.ion.value"]
101 	},
102 	{
103 		levels: [SnippetLevel.type, SnippetLevel.mixinTemplate],
104 		shortcut: "serializeIon",
105 		title: "mir-ion serialize",
106 		documentation: "Custom mir-ion struct serializion code.\n\n"
107 			~ "**Note:** a proxy might achieve the same thing if you just want to "
108 			~ "serialize a single member.",
109 		snippet: "void serialize(S)(scope ref S serializer) const scope {\n"
110 			~ "\timport mir.ser : serializeValue;\n"
111 			~ "\n"
112 			~ "\tserializeValue(serializer, ${1:this.impl});$0\n"
113 			~ "}\n"
114 	},
115 ];
116 
117 static immutable DependencySnippets[] builtinDependencySnippets = [
118 	DependencySnippets(["vibe-d:http"], builtinVibeHttpSnippets),
119 	DependencySnippets(["mir-ion"], builtinMirSerdeSnippets),
120 ];