From d57fb2c4b6f144b4f030d702729848fde0e4f203 Mon Sep 17 00:00:00 2001 From: alemi Date: Wed, 11 Sep 2024 15:12:31 +0200 Subject: [PATCH] feat: added codemp config, changed connect methods --- src/api/config.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/api/mod.rs | 4 ++++ src/client.rs | 28 +++++++++------------------- src/workspace.rs | 4 ++-- 4 files changed, 59 insertions(+), 21 deletions(-) create mode 100644 src/api/config.rs diff --git a/src/api/config.rs b/src/api/config.rs new file mode 100644 index 0000000..f0df96c --- /dev/null +++ b/src/api/config.rs @@ -0,0 +1,44 @@ +//! # Config +//! Data structure defining clients configuration + + +/// Configuration struct for `codemp` client +#[derive(Debug, Clone)] +pub struct Config { + /// user identifier used to register, possibly your email + pub username: String, + /// user password chosen upon registration + pub password: String, + /// address of server to connect to, default api.code.mp + pub host: Option, + /// port to connect to, default 50053 + pub port: Option, + /// enable or disable tls, default true + pub tls: Option, +} + +impl Config { + #[inline] + pub(crate) fn host(&self) -> &str { + self.host.as_deref().unwrap_or("api.code.mp") + } + + #[inline] + pub(crate) fn port(&self) -> u16 { + self.port.unwrap_or(50053) + } + + #[inline] + pub(crate) fn tls(&self) -> bool { + self.tls.unwrap_or(true) + } + + pub(crate) fn endpoint(&self) -> String { + format!( + "{}{}:{}", + if self.tls() { "https://" } else { "http" }, + self.host(), + self.port() + ) + } +} diff --git a/src/api/mod.rs b/src/api/mod.rs index 8097b14..2e5f00d 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -7,6 +7,9 @@ pub mod controller; /// a generic representation of a text change pub mod change; +/// client configuration +pub mod config; + /// representation for an user's cursor pub mod cursor; @@ -18,6 +21,7 @@ pub mod user; pub use controller::Controller; pub use change::TextChange; +pub use config::Config; pub use cursor::Cursor; pub use event::Event; pub use user::User; diff --git a/src/client.rs b/src/client.rs index 2bdc60b..8930994 100644 --- a/src/client.rs +++ b/src/client.rs @@ -28,7 +28,7 @@ pub struct Client(Arc); #[derive(Debug)] struct ClientInner { user: User, - host: String, + config: crate::api::Config, workspaces: DashMap, auth: AuthClient, session: SessionClient>, @@ -37,39 +37,29 @@ struct ClientInner { impl Client { /// Connect to the server, authenticate and instantiate a new [`Client`]. - pub async fn connect( - host: impl AsRef, - username: impl AsRef, - password: impl AsRef, - ) -> ConnectionResult { - let host = if host.as_ref().starts_with("http") { - host.as_ref().to_string() - } else { - format!("https://{}", host.as_ref()) - }; - - let channel = Endpoint::from_shared(host.clone())?.connect().await?; + pub async fn connect(config: crate::api::Config) -> ConnectionResult { + // TODO move these two into network.rs + let channel = Endpoint::from_shared(config.endpoint())?.connect().await?; let mut auth = AuthClient::new(channel.clone()); let resp = auth.login(LoginRequest { - username: username.as_ref().to_string(), - password: password.as_ref().to_string(), + username: config.username.clone(), + password: config.password.clone(), }) .await? .into_inner(); let claims = InternallyMutable::new(resp.token); + // TODO move this one into network.rs let session = SessionClient::with_interceptor( channel, network::SessionInterceptor(claims.channel()) ); Ok(Client(Arc::new(ClientInner { - host, user: resp.user.into(), workspaces: DashMap::default(), - claims, - auth, session, + claims, auth, session, config }))) } @@ -139,7 +129,7 @@ impl Client { let ws = Workspace::try_new( workspace.as_ref().to_string(), self.0.user.clone(), - &self.0.host, + self.0.config.clone(), token, self.0.claims.channel(), ) diff --git a/src/workspace.rs b/src/workspace.rs index 6c4dc97..0bb3bcd 100644 --- a/src/workspace.rs +++ b/src/workspace.rs @@ -54,12 +54,12 @@ impl Workspace { pub(crate) async fn try_new( name: String, user: User, - dest: &str, + config: crate::api::Config, token: Token, claims: tokio::sync::watch::Receiver, // TODO ughh receiving this ) -> ConnectionResult { let workspace_claim = InternallyMutable::new(token); - let services = Services::try_new(dest, claims, workspace_claim.channel()).await?; + let services = Services::try_new(&config.endpoint(), claims, workspace_claim.channel()).await?; let ws_stream = services.ws().attach(Empty {}).await?.into_inner(); let (tx, rx) = mpsc::channel(128);