Start working on rust compile step
This commit is contained in:
30
crates/libjrpc/templates/Dart/analysis_options.yaml
Normal file
30
crates/libjrpc/templates/Dart/analysis_options.yaml
Normal file
@ -0,0 +1,30 @@
|
||||
# This file configures the static analysis results for your project (errors,
|
||||
# warnings, and lints).
|
||||
#
|
||||
# This enables the 'recommended' set of lints from `package:lints`.
|
||||
# This set helps identify many issues that may lead to problems when running
|
||||
# or consuming Dart code, and enforces writing Dart using a single, idiomatic
|
||||
# style and format.
|
||||
#
|
||||
# If you want a smaller set of lints you can change this to specify
|
||||
# 'package:lints/core.yaml'. These are just the most critical lints
|
||||
# (the recommended set includes the core lints).
|
||||
# The core lints are also what is used by pub.dev for scoring packages.
|
||||
|
||||
include: package:lints/recommended.yaml
|
||||
|
||||
# Uncomment the following section to specify additional rules.
|
||||
|
||||
# linter:
|
||||
# rules:
|
||||
# - camel_case_types
|
||||
|
||||
# analyzer:
|
||||
# exclude:
|
||||
# - path/to/excluded/files/**
|
||||
|
||||
# For more information about the core and recommended set of lints, see
|
||||
# https://dart.dev/go/core-lints
|
||||
|
||||
# For additional information about configuring this file, see
|
||||
# https://dart.dev/guides/language/analysis-options
|
52
crates/libjrpc/templates/Dart/base.dart
Normal file
52
crates/libjrpc/templates/Dart/base.dart
Normal file
@ -0,0 +1,52 @@
|
||||
class JRPCError extends Error {
|
||||
String message;
|
||||
JRPCError(this.message);
|
||||
|
||||
@override
|
||||
String toString() => message;
|
||||
}
|
||||
|
||||
int? int_fromJson(dynamic val) {
|
||||
if (val == null) {
|
||||
return null;
|
||||
} else if (val is int) {
|
||||
return val;
|
||||
} else if (val is double) {
|
||||
return val.toInt();
|
||||
} else {
|
||||
throw JRPCError("Not a number!");
|
||||
}
|
||||
}
|
||||
|
||||
double? double_fromJson(dynamic val) {
|
||||
if (val == null) {
|
||||
return null;
|
||||
} else if (val is int) {
|
||||
return val.toDouble();
|
||||
} else if (val is double) {
|
||||
return val;
|
||||
} else {
|
||||
throw JRPCError("Not a number!");
|
||||
}
|
||||
}
|
||||
|
||||
bool? bool_fromJson(dynamic val) {
|
||||
if (val == null) {
|
||||
return null;
|
||||
} else if (val is bool) {
|
||||
return val;
|
||||
} else {
|
||||
throw JRPCError("Not a bool!");
|
||||
}
|
||||
}
|
||||
|
||||
String? String_fromJson(dynamic val) {
|
||||
if (val == null) {
|
||||
return null;
|
||||
} else if (val is String) {
|
||||
return val;
|
||||
} else {
|
||||
return val.toString(); // TODO: Either this or error
|
||||
// throw JRPCError("Not a string!");
|
||||
}
|
||||
}
|
10
crates/libjrpc/templates/Dart/pubspec.yaml
Normal file
10
crates/libjrpc/templates/Dart/pubspec.yaml
Normal file
@ -0,0 +1,10 @@
|
||||
name: __NAME__
|
||||
description: JRPC
|
||||
version: 1.0.0
|
||||
|
||||
environment:
|
||||
sdk: ">=2.17.6 <3.0.0"
|
||||
|
||||
dependencies: {}
|
||||
dev_dependencies:
|
||||
lints: ^2.0.0
|
86
crates/libjrpc/templates/Dart/service_client.dart
Normal file
86
crates/libjrpc/templates/Dart/service_client.dart
Normal file
@ -0,0 +1,86 @@
|
||||
import "dart:async";
|
||||
import 'dart:math';
|
||||
|
||||
import "./base.dart";
|
||||
|
||||
abstract class Service {
|
||||
String name;
|
||||
ServiceProvider provider;
|
||||
|
||||
Service(this.provider, this.name) {
|
||||
provider._services[name] = this;
|
||||
}
|
||||
}
|
||||
|
||||
class ServiceProvider {
|
||||
final Map<String, Service> _services = {};
|
||||
final Map<String, Completer<dynamic>> _requests = {};
|
||||
|
||||
StreamController<Map<String, dynamic>> output = StreamController();
|
||||
late StreamSubscription s;
|
||||
|
||||
ServiceProvider(Stream<Map<String, dynamic>> input) {
|
||||
s = input.listen(onMessage);
|
||||
}
|
||||
|
||||
void onMessage(Map<String, dynamic> msg) {
|
||||
// print("Working on message");
|
||||
if (msg.containsKey("method")) {
|
||||
if (msg.containsKey("id")) {
|
||||
print("Message is request");
|
||||
// Request, not supported!
|
||||
return;
|
||||
} else {
|
||||
// print("Message is notification");
|
||||
// Notification
|
||||
// TODO: Implement
|
||||
}
|
||||
} else {
|
||||
// print("Message is response");
|
||||
// Response
|
||||
var req = _requests[msg["id"]];
|
||||
|
||||
if (req == null) {
|
||||
// print("Could not find related request. Ignoring");
|
||||
return; // Irrelevant response
|
||||
}
|
||||
if (msg.containsKey("error")) {
|
||||
//TODO:
|
||||
req.completeError(JRPCError(msg["error"]["message"]));
|
||||
} else {
|
||||
req.complete(msg["result"]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<dynamic> sendRequest(String method, dynamic params) {
|
||||
var id = nanoid(10);
|
||||
var req = {"jsonrpc": "2.0", "id": id, "method": method, "params": params};
|
||||
|
||||
var completer = Completer<dynamic>();
|
||||
|
||||
output.add(req);
|
||||
_requests[id] = completer;
|
||||
|
||||
return completer.future;
|
||||
}
|
||||
|
||||
void sendNotification(String method, dynamic params) {
|
||||
var req = {"jsonrpc": "2.0", "method": method, "params": params};
|
||||
output.add(req);
|
||||
}
|
||||
}
|
||||
|
||||
// Copied from: https://github.com/pd4d10/nanoid-dart (MIT License)
|
||||
final _random = Random.secure();
|
||||
const urlAlphabet =
|
||||
'ModuleSymbhasOwnPr-0123456789ABCDEFGHNRVfgctiUvz_KqYTJkLxpZXIjQW';
|
||||
|
||||
String nanoid([int size = 21]) {
|
||||
final len = urlAlphabet.length;
|
||||
String id = '';
|
||||
while (0 < size--) {
|
||||
id += urlAlphabet[_random.nextInt(len)];
|
||||
}
|
||||
return id;
|
||||
}
|
Reference in New Issue
Block a user