From bbb27b9654ecb417e1d460c4733fef5f057e7329 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Mon, 4 Oct 2021 17:30:11 -0700 Subject: [PATCH] Move ChannelList, UserStore into client crate Co-Authored-By: Nathan Sobo --- Cargo.lock | 4 +++ crates/client/Cargo.toml | 4 +++ crates/{zed => client}/src/channel.rs | 16 ++++++----- crates/{zed => client}/src/http.rs | 0 crates/client/src/lib.rs | 6 +++++ crates/client/src/test.rs | 38 +++++++++++++++++++++++--- crates/{zed => client}/src/user.rs | 10 ++++--- crates/server/src/rpc.rs | 8 +++--- crates/zed/Cargo.toml | 2 +- crates/zed/src/chat_panel.rs | 6 ++--- crates/zed/src/lib.rs | 8 ++---- crates/zed/src/main.rs | 8 +++--- crates/zed/src/people_panel.rs | 7 ++--- crates/zed/src/test.rs | 39 ++------------------------- crates/zed/src/workspace.rs | 3 +-- 15 files changed, 83 insertions(+), 76 deletions(-) rename crates/{zed => client}/src/channel.rs (98%) rename crates/{zed => client}/src/http.rs (100%) rename crates/{zed => client}/src/user.rs (97%) diff --git a/Cargo.lock b/Cargo.lock index e18a854fba..173ffeb260 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1028,7 +1028,9 @@ dependencies = [ "anyhow", "async-recursion", "async-tungstenite", + "futures", "gpui", + "image 0.23.14", "lazy_static", "log", "parking_lot", @@ -1036,8 +1038,10 @@ dependencies = [ "rand 0.8.3", "rpc", "smol", + "sum_tree", "surf", "thiserror", + "time 0.3.2", "tiny_http", "util", ] diff --git a/crates/client/Cargo.toml b/crates/client/Cargo.toml index 0931de7c31..e71d7d4510 100644 --- a/crates/client/Cargo.toml +++ b/crates/client/Cargo.toml @@ -10,10 +10,13 @@ test-support = [] gpui = { path = "../gpui" } util = { path = "../util" } rpc = { path = "../rpc" } +sum_tree = { path = "../sum_tree" } anyhow = "1.0.38" async-recursion = "0.3" async-tungstenite = { version = "0.14", features = ["async-tls"] } +futures = "0.3" +image = "0.23" lazy_static = "1.4.0" log = "0.4" parking_lot = "0.11.1" @@ -22,4 +25,5 @@ rand = "0.8.3" smol = "1.2.5" surf = "2.2" thiserror = "1.0.29" +time = "0.3" tiny_http = "0.8" diff --git a/crates/zed/src/channel.rs b/crates/client/src/channel.rs similarity index 98% rename from crates/zed/src/channel.rs rename to crates/client/src/channel.rs index ac23e15520..90b5bbe425 100644 --- a/crates/zed/src/channel.rs +++ b/crates/client/src/channel.rs @@ -1,6 +1,9 @@ -use crate::user::{User, UserStore}; +use super::{ + proto, + user::{User, UserStore}, + Client, Status, Subscription, TypedEnvelope, +}; use anyhow::{anyhow, Context, Result}; -use client::{proto, Client, TypedEnvelope}; use gpui::{ AsyncAppContext, Entity, ModelContext, ModelHandle, MutableAppContext, Task, WeakModelHandle, }; @@ -38,7 +41,7 @@ pub struct Channel { user_store: ModelHandle, rpc: Arc, rng: StdRng, - _subscription: client::Subscription, + _subscription: Subscription, } #[derive(Clone, Debug)] @@ -91,7 +94,7 @@ impl ChannelList { let mut status = rpc.status(); while let Some((status, this)) = status.recv().await.zip(this.upgrade(&cx)) { match status { - client::Status::Connected { .. } => { + Status::Connected { .. } => { let response = rpc .request(proto::GetChannels {}) .await @@ -115,7 +118,7 @@ impl ChannelList { cx.notify(); }); } - client::Status::SignedOut { .. } => { + Status::SignedOut { .. } => { this.update(&mut cx, |this, cx| { this.available_channels = None; this.channels.clear(); @@ -589,8 +592,7 @@ impl<'a> sum_tree::Dimension<'a, ChannelMessageSummary> for Count { #[cfg(test)] mod tests { use super::*; - use crate::test::FakeHttpClient; - use client::test::FakeServer; + use crate::test::{FakeHttpClient, FakeServer}; use gpui::TestAppContext; use surf::http::Response; diff --git a/crates/zed/src/http.rs b/crates/client/src/http.rs similarity index 100% rename from crates/zed/src/http.rs rename to crates/client/src/http.rs diff --git a/crates/client/src/lib.rs b/crates/client/src/lib.rs index b62697b195..c3eb6807d4 100644 --- a/crates/client/src/lib.rs +++ b/crates/client/src/lib.rs @@ -1,6 +1,10 @@ #[cfg(any(test, feature = "test-support"))] pub mod test; +pub mod channel; +pub mod http; +pub mod user; + use anyhow::{anyhow, Context, Result}; use async_recursion::async_recursion; use async_tungstenite::tungstenite::{ @@ -26,7 +30,9 @@ use surf::Url; use thiserror::Error; use util::ResultExt; +pub use channel::*; pub use rpc::*; +pub use user::*; lazy_static! { static ref ZED_SERVER_URL: String = diff --git a/crates/client/src/test.rs b/crates/client/src/test.rs index ec4a30465c..e471398b53 100644 --- a/crates/client/src/test.rs +++ b/crates/client/src/test.rs @@ -1,11 +1,13 @@ -use super::*; -use std::sync::atomic::Ordering::SeqCst; - use super::Client; +use super::*; +use crate::http::{HttpClient, Request, Response, ServerResponse}; +use futures::{future::BoxFuture, Future}; use gpui::TestAppContext; use parking_lot::Mutex; use postage::{mpsc, prelude::Stream}; use rpc::{proto, ConnectionId, Peer, Receipt, TypedEnvelope}; +use std::fmt; +use std::sync::atomic::Ordering::SeqCst; use std::sync::{ atomic::{AtomicBool, AtomicUsize}, Arc, @@ -154,3 +156,33 @@ impl FakeServer { self.connection_id.lock().expect("not connected") } } + +pub struct FakeHttpClient { + handler: + Box BoxFuture<'static, Result>>, +} + +impl FakeHttpClient { + pub fn new(handler: F) -> Arc + where + Fut: 'static + Send + Future>, + F: 'static + Send + Sync + Fn(Request) -> Fut, + { + Arc::new(Self { + handler: Box::new(move |req| Box::pin(handler(req))), + }) + } +} + +impl fmt::Debug for FakeHttpClient { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("FakeHttpClient").finish() + } +} + +impl HttpClient for FakeHttpClient { + fn send<'a>(&'a self, req: Request) -> BoxFuture<'a, Result> { + let future = (self.handler)(req); + Box::pin(async move { future.await.map(Into::into) }) + } +} diff --git a/crates/zed/src/user.rs b/crates/client/src/user.rs similarity index 97% rename from crates/zed/src/user.rs rename to crates/client/src/user.rs index 3058211f63..47ea9ae55c 100644 --- a/crates/zed/src/user.rs +++ b/crates/client/src/user.rs @@ -1,6 +1,8 @@ -use crate::http::{HttpClient, Method, Request, Url}; +use super::{ + http::{HttpClient, Method, Request, Url}, + proto, Client, Status, TypedEnvelope, +}; use anyhow::{anyhow, Context, Result}; -use client::{proto, Client, TypedEnvelope}; use futures::future; use gpui::{AsyncAppContext, Entity, ImageData, ModelContext, ModelHandle, Task}; use postage::{prelude::Stream, sink::Sink, watch}; @@ -79,7 +81,7 @@ impl UserStore { let mut status = rpc.status(); while let Some(status) = status.recv().await { match status { - client::Status::Connected { .. } => { + Status::Connected { .. } => { if let Some((this, user_id)) = this.upgrade(&cx).zip(rpc.user_id()) { let user = this .update(&mut cx, |this, cx| this.fetch_user(user_id, cx)) @@ -88,7 +90,7 @@ impl UserStore { current_user_tx.send(user).await.ok(); } } - client::Status::SignedOut => { + Status::SignedOut => { current_user_tx.send(None).await.ok(); } _ => {} diff --git a/crates/server/src/rpc.rs b/crates/server/src/rpc.rs index 647ad6794c..1261671c85 100644 --- a/crates/server/src/rpc.rs +++ b/crates/server/src/rpc.rs @@ -977,14 +977,14 @@ mod tests { }; use zed::{ buffer::LanguageRegistry, - channel::{Channel, ChannelDetails, ChannelList}, - client::{self, Client, Credentials, EstablishConnectionError}, + client::{ + self, test::FakeHttpClient, Channel, ChannelDetails, ChannelList, Client, Credentials, + EstablishConnectionError, UserStore, + }, editor::{Editor, EditorSettings, Insert}, fs::{FakeFs, Fs as _}, people_panel::JoinWorktree, project::{ProjectPath, Worktree}, - test::FakeHttpClient, - user::UserStore, workspace::Workspace, }; diff --git a/crates/zed/Cargo.toml b/crates/zed/Cargo.toml index a1f645e756..b024560a0c 100644 --- a/crates/zed/Cargo.toml +++ b/crates/zed/Cargo.toml @@ -68,7 +68,7 @@ smol = "1.2.5" surf = "2.2" tempdir = { version = "0.3.7", optional = true } thiserror = "1.0.29" -time = { version = "0.3" } +time = "0.3" tiny_http = "0.8" toml = "0.5" tree-sitter = "0.19.5" diff --git a/crates/zed/src/chat_panel.rs b/crates/zed/src/chat_panel.rs index 47bdd86e13..e81946ed6a 100644 --- a/crates/zed/src/chat_panel.rs +++ b/crates/zed/src/chat_panel.rs @@ -1,8 +1,8 @@ -use crate::{ +use crate::{theme, Settings}; +use client::{ channel::{Channel, ChannelEvent, ChannelList, ChannelMessage}, - theme, Settings, + Client, }; -use client::Client; use editor::{Editor, EditorSettings}; use gpui::{ action, diff --git a/crates/zed/src/lib.rs b/crates/zed/src/lib.rs index 25d407791c..6d5be2f0a6 100644 --- a/crates/zed/src/lib.rs +++ b/crates/zed/src/lib.rs @@ -1,8 +1,6 @@ pub mod assets; -pub mod channel; pub mod chat_panel; pub mod file_finder; -pub mod http; pub mod language; pub mod menus; pub mod people_panel; @@ -12,12 +10,10 @@ pub mod settings; pub mod test; pub mod theme; pub mod theme_selector; -pub mod user; pub mod workspace; pub use buffer; use buffer::LanguageRegistry; -use channel::ChannelList; pub use client; pub use editor; use gpui::{action, keymap::Binding, ModelHandle}; @@ -41,9 +37,9 @@ pub struct AppState { pub languages: Arc, pub themes: Arc, pub client: Arc, - pub user_store: ModelHandle, + pub user_store: ModelHandle, pub fs: Arc, - pub channel_list: ModelHandle, + pub channel_list: ModelHandle, } pub fn init(app_state: &Arc, cx: &mut gpui::MutableAppContext) { diff --git a/crates/zed/src/main.rs b/crates/zed/src/main.rs index ed87076a70..95b53bbde3 100644 --- a/crates/zed/src/main.rs +++ b/crates/zed/src/main.rs @@ -10,11 +10,11 @@ use std::{fs, path::PathBuf, sync::Arc}; use zed::{ self, assets::Assets, - channel::ChannelList, - chat_panel, client, editor, file_finder, + chat_panel, client, + client::{http, ChannelList, UserStore}, + editor, file_finder, fs::RealFs, - http, language, menus, project_panel, settings, theme_selector, - user::UserStore, + language, menus, project_panel, settings, theme_selector, workspace::{self, OpenNew, OpenParams, OpenPaths}, AppState, }; diff --git a/crates/zed/src/people_panel.rs b/crates/zed/src/people_panel.rs index 10d3bdaa02..d59ec5c2b2 100644 --- a/crates/zed/src/people_panel.rs +++ b/crates/zed/src/people_panel.rs @@ -1,8 +1,5 @@ -use crate::{ - theme::Theme, - user::{Collaborator, UserStore}, - Settings, -}; +use crate::{theme::Theme, Settings}; +use client::{Collaborator, UserStore}; use gpui::{ action, elements::*, diff --git a/crates/zed/src/test.rs b/crates/zed/src/test.rs index c4fae8c1dd..37ac9a869d 100644 --- a/crates/zed/src/test.rs +++ b/crates/zed/src/test.rs @@ -1,20 +1,15 @@ use crate::{ assets::Assets, - channel::ChannelList, - http::{HttpClient, Request, Response, ServerResponse}, language, settings::{self, ThemeRegistry}, - user::UserStore, AppState, }; -use anyhow::Result; use buffer::LanguageRegistry; -use client::Client; -use futures::{future::BoxFuture, Future}; +use client::{http::ServerResponse, test::FakeHttpClient, ChannelList, Client, UserStore}; use gpui::MutableAppContext; use parking_lot::Mutex; use project::fs::FakeFs; -use std::{fmt, sync::Arc}; +use std::sync::Arc; #[cfg(test)] #[ctor::ctor] @@ -41,33 +36,3 @@ pub fn test_app_state(cx: &mut MutableAppContext) -> Arc { fs: Arc::new(FakeFs::new()), }) } - -pub struct FakeHttpClient { - handler: - Box BoxFuture<'static, Result>>, -} - -impl FakeHttpClient { - pub fn new(handler: F) -> Arc - where - Fut: 'static + Send + Future>, - F: 'static + Send + Sync + Fn(Request) -> Fut, - { - Arc::new(Self { - handler: Box::new(move |req| Box::pin(handler(req))), - }) - } -} - -impl fmt::Debug for FakeHttpClient { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_struct("FakeHttpClient").finish() - } -} - -impl HttpClient for FakeHttpClient { - fn send<'a>(&'a self, req: Request) -> BoxFuture<'a, Result> { - let future = (self.handler)(req); - Box::pin(async move { future.await.map(Into::into) }) - } -} diff --git a/crates/zed/src/workspace.rs b/crates/zed/src/workspace.rs index 77f07c2fb1..770bc838ae 100644 --- a/crates/zed/src/workspace.rs +++ b/crates/zed/src/workspace.rs @@ -10,7 +10,6 @@ use crate::{ project::{Project, ProjectPath}, project_panel::ProjectPanel, settings::Settings, - user, workspace::sidebar::{Side, Sidebar, SidebarItemId, ToggleSidebarItem, ToggleSidebarItemFocus}, AppState, Authenticate, }; @@ -357,7 +356,7 @@ impl Clone for Box { pub struct Workspace { pub settings: watch::Receiver, client: Arc, - user_store: ModelHandle, + user_store: ModelHandle, fs: Arc, modal: Option, center: PaneGroup,