Skip to content

Commit 931ca3f

Browse files
authored
Refactor (#44)
* Temp commit * Refactor complete * make clippy happy
1 parent 57f6747 commit 931ca3f

File tree

17 files changed

+600
-216
lines changed

17 files changed

+600
-216
lines changed

Cargo.lock

+37-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/chat/src/async_list.rs

+25-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{fmt::Debug, future::Future, hash::Hash};
1+
use std::{fmt::Debug, future::Future, hash::Hash, sync::Arc};
22

33
pub trait AsyncList {
44
type Content: AsyncListItem;
@@ -12,7 +12,30 @@ pub trait AsyncList {
1212
fn bounded_at_bottom_by(&self) -> impl Future<Output = Option<<Self::Content as AsyncListItem>::Identifier>>;
1313
}
1414

15-
pub trait AsyncListItem: Clone + Debug {
15+
impl<L: AsyncList> AsyncList for Arc<L> {
16+
type Content = L::Content;
17+
18+
fn bounded_at_bottom_by(&self) -> impl Future<Output = Option<<Self::Content as AsyncListItem>::Identifier>> {
19+
(**self).bounded_at_bottom_by()
20+
}
21+
22+
fn bounded_at_top_by(&self) -> impl Future<Output = Option<<Self::Content as AsyncListItem>::Identifier>> {
23+
(**self).bounded_at_top_by()
24+
}
25+
26+
fn find(&self, identifier: &<Self::Content as AsyncListItem>::Identifier) -> impl Future<Output = Option<Self::Content>> {
27+
(**self).find(identifier)
28+
}
29+
30+
fn get(
31+
&self,
32+
index: AsyncListIndex<<Self::Content as AsyncListItem>::Identifier>,
33+
) -> impl Future<Output = Option<AsyncListResult<Self::Content>>> + Send {
34+
(**self).get(index)
35+
}
36+
}
37+
38+
pub trait AsyncListItem: Clone {
1639
type Identifier: Eq + Hash + Clone + Send + Debug;
1740

1841
fn get_list_identifier(&self) -> Self::Identifier;

src/chat/src/channel.rs

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,33 @@
1+
use std::{fmt::Debug, sync::Arc};
2+
13
use tokio::sync::broadcast;
24

35
use crate::{async_list::AsyncList, message::Message};
46

57
pub trait Channel: AsyncList<Content = Self::Message> + Send + Sync + Clone {
6-
type Message: Message;
8+
type Message: Message<Identifier = Self::Identifier>;
9+
type Identifier: Sized + Copy + Clone + Debug + Eq + PartialEq;
710

811
fn get_receiver(&self) -> broadcast::Receiver<Self::Message>;
912

1013
fn send_message(&self, content: String, nonce: String) -> Self::Message;
14+
15+
fn get_identifier(&self) -> Self::Identifier;
16+
}
17+
18+
impl<C: Channel> Channel for Arc<C> {
19+
type Identifier = C::Identifier;
20+
type Message = C::Message;
21+
22+
fn get_identifier(&self) -> Self::Identifier {
23+
(**self).get_identifier()
24+
}
25+
26+
fn get_receiver(&self) -> broadcast::Receiver<Self::Message> {
27+
(**self).get_receiver()
28+
}
29+
30+
fn send_message(&self, content: String, nonce: String) -> Self::Message {
31+
(**self).send_message(content, nonce)
32+
}
1133
}

src/chat/src/client.rs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use std::{fmt::Debug, future::Future};
2+
3+
use crate::channel::Channel;
4+
5+
pub trait ClientConstructor<C: Client> {
6+
type ConstructorArguments;
7+
type ConstructorFailure;
8+
9+
fn construct(args: Self::ConstructorArguments) -> impl Future<Output = Result<C, Self::ConstructorFailure>>;
10+
}
11+
12+
pub trait Client {
13+
type Identifier: Sized + Copy + Clone + Debug + Eq + PartialEq;
14+
type Channel: Channel;
15+
16+
fn channel(identifier: Self::Identifier) -> impl Future<Output = Option<Self::Channel>>;
17+
}

src/chat/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
pub mod async_list;
22
pub mod channel;
3+
pub mod client;
34
pub mod message;

src/chat/src/message.rs

+44-9
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,55 @@
1+
use std::fmt::Debug;
2+
13
use chrono::{DateTime, Utc};
2-
use gpui::Element;
4+
use gpui::{IntoElement, Render, View, WindowContext};
35

46
use crate::async_list::AsyncListItem;
57

68
pub trait Message: Clone + AsyncListItem + Send {
7-
fn get_author(&self) -> &impl MessageAuthor;
8-
fn get_content(&self) -> impl Element;
9-
fn get_identifier(&self) -> String;
10-
fn get_nonce(&self) -> Option<&String>;
9+
type Identifier: Sized + Copy + Clone + Debug + Eq + PartialEq;
10+
type Author: MessageAuthor<Identifier = <Self as Message>::Identifier>;
11+
type Content: Render;
12+
13+
fn get_author(&self) -> Self::Author;
14+
fn get_content(&self, cx: &mut WindowContext) -> View<Self::Content>;
15+
fn get_identifier(&self) -> Option<<Self as Message>::Identifier>;
16+
fn get_nonce(&self) -> impl PartialEq;
1117
fn should_group(&self, previous: &Self) -> bool;
1218
fn get_timestamp(&self) -> Option<DateTime<Utc>>;
1319
}
1420

21+
#[derive(Debug, Clone, Copy)]
22+
pub struct IconRenderConfig {
23+
size: usize,
24+
}
25+
26+
impl Default for IconRenderConfig {
27+
fn default() -> Self {
28+
IconRenderConfig { size: 1024 }
29+
}
30+
}
31+
32+
impl IconRenderConfig {
33+
pub fn small() -> Self {
34+
IconRenderConfig { size: 32 }
35+
}
36+
37+
pub fn with_size(mut self, size: usize) -> IconRenderConfig {
38+
self.size = size;
39+
self
40+
}
41+
42+
pub fn size(&self) -> usize {
43+
self.size
44+
}
45+
}
46+
1547
pub trait MessageAuthor: PartialEq + Eq {
16-
fn get_display_name(&self) -> impl Element;
17-
fn get_icon(&self) -> String;
18-
fn get_small_icon(&self) -> String;
19-
fn get_id(&self) -> String;
48+
type Identifier: Sized + Copy + Clone + Debug + Eq + PartialEq;
49+
type DisplayName: IntoElement + Clone;
50+
type Icon: IntoElement + Clone;
51+
52+
fn get_display_name(&self) -> Self::DisplayName;
53+
fn get_icon(&self, config: IconRenderConfig) -> Self::Icon;
54+
fn get_identifier(&self) -> Self::Identifier;
2055
}

src/discord/Cargo.toml

+6
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,9 @@ serenity = { git = "https://github.com/scopeclient/serenity", version = "0.12" }
1010
tokio = "1.41.1"
1111
chrono.workspace = true
1212
scope-backend-cache = { version = "0.1.0", path = "../cache" }
13+
url = "2.5.3"
14+
querystring = "1.1.0"
15+
catty = "0.1.5"
16+
atomic_refcell = "0.1.13"
17+
rand = "0.8.5"
18+
dashmap = "6.1.0"

0 commit comments

Comments
 (0)