Skip to main content

grp_core/common/users/
orgs.rs

1use std::{future, vec};
2use futures::StreamExt;
3
4use crate::animation;
5use crate::error::structs::Error;
6use crate::common::users::structs::User;
7use crate::config::Config;
8use crate::platform::Platform;
9
10impl Platform {
11    
12    /// # Return
13    /// a vector of `grp_core::structs::User`
14    /// 
15    /// # Error
16    /// a `grp_core::Error` containing the detail of the error. 
17    pub async fn get_logged_orgs(&self, config: &Config) -> Result<Vec<User>, Error> {
18        let an = Box::new(animation::None);
19        
20        let mut error: Option<Error> = None;
21        let orgs: Vec<User> = self.list_orgs(config, &an)
22            .take_while(|result| {
23                match result {
24                    Ok(_) => future::ready(true),
25                    Err(e) => {
26                        error = Some(e.clone());
27                        future::ready(false)
28                    },
29                }
30            })
31            .fold(vec![], async move |acc, act| {
32                let mut curr = acc;
33                if let Ok(act) = act { curr.extend(act) };
34                curr
35            })
36            .await
37        ;
38        
39        match (orgs.len(), error) {
40            (0, Some(e)) => Err(e),
41            (_, _) => Ok(orgs)
42        }
43    }
44}