From 48c2945172b88c35c187d298a35bf26716af4e91 Mon Sep 17 00:00:00 2001
From: "Jannis M. Hoffmann" <jannis@fehcom.de>
Date: Thu, 21 Nov 2024 21:14:40 +0100
Subject: Switch to varlink as IPC protocol

This is a lot! Whole new design on top of a statefult varlink interface.

You can now handle multiple request response cycles over a single
connection.

The error responses are lot more refined than just status codes with
optional messages and finally part of the protocol.

TODO: A lot of error handling needs to be improved.
---
 src/cmd/folders.rs | 35 +++++++++++++++++------------------
 1 file changed, 17 insertions(+), 18 deletions(-)

(limited to 'src/cmd/folders.rs')

diff --git a/src/cmd/folders.rs b/src/cmd/folders.rs
index 417203c..8bf9a30 100644
--- a/src/cmd/folders.rs
+++ b/src/cmd/folders.rs
@@ -1,9 +1,7 @@
-use std::path::{Path, PathBuf};
+use std::path::Path;
 
-use protobuf::Message as _;
-
-use crate::error::Result;
-use crate::pb3::jwebmail::{FoldersReq, FoldersResp};
+use crate::cmd::MailStorage;
+use crate::de_jmhoffmann_jwebmail_mailstorage::Call_Folders;
 
 fn is_mailsubdir(p: &Path) -> bool {
     if !p.is_dir() {
@@ -32,19 +30,20 @@ fn is_mailsubdir(p: &Path) -> bool {
     true
 }
 
-pub fn folders(path: PathBuf, req: &[u8]) -> Result<Vec<u8>> {
-    let _ = FoldersReq::parse_from_bytes(req)?;
-
-    let mut subdirs: Vec<_> = path
-        .read_dir()?
-        .filter_map(|d| d.ok())
-        .filter(|d| is_mailsubdir(&d.path()))
-        .filter_map(|d| Some(d.path().file_name()?.to_string_lossy()[1..].to_owned()))
-        .collect();
+pub fn folders(ms: &MailStorage, call: &mut dyn Call_Folders) -> varlink::Result<()> {
+    if let Some(path) = &*ms.maildir_path.read().unwrap() {
+        let mut subdirs: Vec<_> = path
+            .read_dir()
+            .map_err(varlink::map_context!())?
+            .filter_map(|d| d.ok())
+            .filter(|d| is_mailsubdir(&d.path()))
+            .filter_map(|d| Some(d.path().file_name()?.to_string_lossy()[1..].to_owned()))
+            .collect();
 
-    subdirs.sort();
+        subdirs.sort();
 
-    let mut res = FoldersResp::new();
-    res.folders = subdirs;
-    res.write_to_bytes().map_err(|e| e.into())
+        call.reply(subdirs)
+    } else {
+        call.reply_not_initialized()
+    }
 }
-- 
cgit v1.2.3