diff --git a/src/daemon_backend.rs b/src/daemon_backend.rs index 9a128cd..ea3f922 100644 --- a/src/daemon_backend.rs +++ b/src/daemon_backend.rs @@ -1,9 +1,10 @@ -use std::{fmt::Debug, sync::Arc}; +use std::{fmt::Debug, str::FromStr, sync::Arc}; use futures_util::{SinkExt, StreamExt}; use giterated_models::{ authenticated::{Authenticated, AuthenticationSourceProviders}, error::OperationError, + error::{IntoInternalError, NetworkOperationError}, message::GiteratedMessage, object::{GiteratedObject, Object, ObjectRequest, ObjectRequestError, ObjectResponse}, object_backend::ObjectBackend, @@ -49,11 +50,9 @@ impl ObjectBackend for DaemonConnectionPool { payload, }; - let mut connection = self - .0 - .get() - .await - .map_err(|e| OperationError::Internal(e.to_string()))?; + let mut connection = self.0.get().await.map_err(|e| { + OperationError::Internal(anyhow::Error::from(ConnectionFailed(e.to_string()))) + })?; let mut authenticated = Authenticated::new(message); for authentication in &operation_state.authentication { @@ -76,11 +75,9 @@ impl ObjectBackend for DaemonConnectionPool { payload: operation, }; - let mut connection = self - .0 - .get() - .await - .map_err(|e| OperationError::Internal(e.to_string()))?; + let mut connection = self.0.get().await.map_err(|e| { + OperationError::Internal(anyhow::Error::from(ConnectionFailed(e.to_string()))) + })?; let mut authenticated = Authenticated::new(message); for authentication in &operation_state.authentication { @@ -88,13 +85,12 @@ impl ObjectBackend for DaemonConnectionPool { } let object_raw: ObjectResponse = send_expect(&mut connection, authenticated).await?; - Ok(unsafe { - Object::new_unchecked( - O::from_str(&object_raw.0) - .map_err(|_e| OperationError::Internal("heck".to_string()))?, - self.clone(), - ) - }) + + if let Ok(object) = O::from_str(&object_raw.0) { + Ok(unsafe { Object::new_unchecked(object, self.clone()) }) + } else { + panic!() + } } } @@ -112,30 +108,33 @@ async fn send_expect< socket .send(Message::Binary(payload)) .await - .map_err(|e| OperationError::Internal(e.to_string()))?; + .as_internal_error()?; while let Some(message) = socket.next().await { - let payload = match message.map_err(|e| OperationError::Internal(e.to_string()))? { + let payload = match message.as_internal_error()? { Message::Binary(payload) => payload, _ => { continue; } }; - let raw_result = bincode::deserialize::, OperationError>>>(&payload) - .map_err(|e| OperationError::Internal(e.to_string()))?; + let raw_result = + bincode::deserialize::, NetworkOperationError>>>(&payload) + .map_err(|e| OperationError::Internal(anyhow::Error::from(e)))?; // Map ok let raw_result = match raw_result { Ok(raw) => Ok(serde_json::from_slice(&raw) - .map_err(|e| OperationError::Internal(e.to_string()))?), + .map_err(|e| OperationError::Internal(anyhow::Error::from(e)))?), Err(err) => Err(match err { - OperationError::Operation(err) => OperationError::Operation( + NetworkOperationError::Operation(err) => OperationError::Operation( serde_json::from_slice(&err) - .map_err(|e| OperationError::Internal(e.to_string()))?, + .map_err(|e| OperationError::Internal(anyhow::Error::from(e)))?, ), - OperationError::Internal(err) => OperationError::Internal(err), - OperationError::Unhandled => OperationError::Unhandled, + NetworkOperationError::Internal => { + OperationError::Internal(anyhow::Error::from(NetworkError)) + } + NetworkOperationError::Unhandled => OperationError::Unhandled, }), }; @@ -144,3 +143,11 @@ async fn send_expect< panic!() } + +#[derive(Debug, thiserror::Error)] +#[error("a remote internal error occurred")] +pub struct NetworkError; + +#[derive(Debug, thiserror::Error)] +#[error("failed to get connection from pool: {0}")] +pub struct ConnectionFailed(String); diff --git a/src/lib.rs b/src/lib.rs index 73ca3e5..8bc72e5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -27,9 +27,7 @@ impl Debug for DaemonConnectionPool { } impl DaemonConnectionPool { - pub fn connect( - instance: impl ToOwned, - ) -> Result { + pub fn connect(instance: impl ToOwned) -> Result { let instance = instance.to_owned(); Ok(Self( Pool::builder(GiteratedConnectionPool { diff --git a/src/main.rs b/src/main.rs index ecaeeb5..425b56f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,9 +5,9 @@ use giterated_models::{ authenticated::{InstanceAuthenticator, UserAuthenticator}, instance::Instance, object_backend::ObjectBackend, - repository::{AccessList, Repository}, + repository::{AccessList, Description, Repository}, settings::AnySetting, - user::{DisplayName, User}, + user::{Bio, DisplayName, User}, }; use color_eyre::eyre::Result; @@ -29,88 +29,32 @@ async fn main() -> Result<()> { let mut operation_state = NetworkOperationState::new(); let mut user = pool - .get_object::("ambee:giterated.dev", &operation_state) + .get_object::("test:giterated.dev", &operation_state) .await?; - // info!("Getting display name for {}!", user); + let bio = user.get::(&operation_state).await?; - let display_name = user.get::(&operation_state).await?; - - // info!("{}'s display name is {}", user, display_name); + info!("Bio: {}", bio); let repositories = user - .repositories( - &Instance::from_str("giterated.dev").unwrap(), - &operation_state, - ) + .repositories(&Instance::from_str("giterated.dev")?, &operation_state) .await?; - // info!("Repository: {:#?}", repositories); + let repository = repositories.first().unwrap(); let mut repository = pool - .get_object::( - &repositories.first().unwrap().repository.to_string(), - &operation_state, - ) + .get_object::(&repository.repository.to_string(), &operation_state) .await?; - // info!("First repository: {}", repository.object().to_string()); - - // info!("First repository description: {}", repository.get::().await?); - // info!("First repository visibility: {:?}", repository.get::().await?); - // info!("First repository default branch: {}", repository.get::().await?); - info!( - "First repository info: {:#?}", - repository.info(false, None, None, &operation_state).await? + "Repository description: {}", + repository.get::(&operation_state).await? ); - let cold_duration = start.elapsed(); - let warm_start = Instant::now(); - - let mut operation_state = NetworkOperationState::new(); - - let mut user = pool - .get_object::("ambee:giterated.dev", &operation_state) - .await?; - - // info!("Getting display name for {}!", user); - - let display_name = user.get::(&operation_state).await?; - - // info!("{}'s display name is {}", user, display_name); - - let repositories = user - .repositories( - &Instance::from_str("giterated.dev").unwrap(), - &operation_state, - ) - .await?; - - // info!("Repository: {:#?}", repositories); - - let mut repository = pool - .get_object::( - &repositories.first().unwrap().repository.to_string(), - &operation_state, - ) - .await?; - - // info!("First repository: {}", repository.object().to_string()); - - // info!("First repository description: {}", repository.get::().await?); - // info!("First repository visibility: {:?}", repository.get::().await?); - // info!("First repository default branch: {}", repository.get::().await?); - info!( - "First repository info: {:#?}", - repository.info(false, None, None, &operation_state).await? + "User Bio Setting: {}", + user.get_setting::(&operation_state).await? ); - let warm_duration = warm_start.elapsed(); - - info!("Cold request time: {}ms", cold_duration.as_millis()); - info!("Warm request time: {}ms", warm_duration.as_millis()); - Ok(()) } diff --git a/src/pool.rs b/src/pool.rs index d3a4f6c..16bd03c 100644 --- a/src/pool.rs +++ b/src/pool.rs @@ -1,6 +1,6 @@ use std::net::SocketAddr; -use deadpool::managed::{Manager, RecycleError, RecycleResult, Metrics}; +use deadpool::managed::{Manager, Metrics, RecycleError, RecycleResult}; use futures_util::SinkExt; use giterated_models::instance::Instance; use tokio_tungstenite::{connect_async, tungstenite::Message};