Explorar o código

rpc: Implement From for json-rpc enums.

Now instead of wrapping JsonResponse, JsonError, and JsonNotification,
we can just use into().
parazyd %!s(int64=4) %!d(string=hai) anos
pai
achega
a44ce744c5
Modificáronse 2 ficheiros con 26 adicións e 0 borrados
  1. 18 0
      src/rpc/jsonrpc.rs
  2. 8 0
      src/util/path.rs

+ 18 - 0
src/rpc/jsonrpc.rs

@@ -80,6 +80,24 @@ pub enum JsonResult {
     Notif(JsonNotification),
 }
 
+impl From<JsonResponse> for JsonResult {
+    fn from(resp: JsonResponse) -> Self {
+        Self::Resp(resp)
+    }
+}
+
+impl From<JsonError> for JsonResult {
+    fn from(err: JsonError) -> Self {
+        Self::Err(err)
+    }
+}
+
+impl From<JsonNotification> for JsonResult {
+    fn from(notif: JsonNotification) -> Self {
+        Self::Notif(notif)
+    }
+}
+
 #[derive(Serialize, Deserialize, Clone, Debug)]
 pub struct JsonRequest {
     pub jsonrpc: Value,

+ 8 - 0
src/util/path.rs

@@ -35,6 +35,14 @@ pub fn join_config_path(file: &Path) -> Result<PathBuf> {
     Ok(path)
 }
 
+pub fn get_config_path(arg: Option<String>, fallback: &str) -> Result<PathBuf> {
+    if arg.is_some() {
+        expand_path(&arg.unwrap())
+    } else {
+        join_config_path(&PathBuf::from(fallback))
+    }
+}
+
 pub fn load_keypair_to_str(path: PathBuf) -> Result<String> {
     if Path::new(&path).exists() {
         let key = fs::read(&path)?;