diff --git a/giterated-daemon/src/authorization.rs b/giterated-daemon/src/authorization.rs index 9d759ed..2f65f81 100644 --- a/giterated-daemon/src/authorization.rs +++ b/giterated-daemon/src/authorization.rs @@ -12,7 +12,7 @@ use giterated_models::user::User; use giterated_models::{ object::ObjectRequest, - settings::{SetSetting, Setting}, + settings::SetSetting, value::{GetValue, GiteratedObjectValue}, }; #[async_trait::async_trait] diff --git a/giterated-daemon/src/connection/wrapper.rs b/giterated-daemon/src/connection/wrapper.rs index cfdefa3..12413ef 100644 --- a/giterated-daemon/src/connection/wrapper.rs +++ b/giterated-daemon/src/connection/wrapper.rs @@ -39,7 +39,6 @@ use toml::Table; use crate::{ authentication::AuthenticationTokenGranter, backend::{MetadataBackend, RepositoryBackend, UserBackend}, - database_backend::DatabaseBackend, federation::connections::InstanceConnections, keys::PublicKeyCache, }; @@ -206,112 +205,6 @@ pub async fn connection_wrapper( } } } - - // loop { - // let mut socket = connection_state.socket.lock().await; - // let message = socket.next().await; - // drop(socket); - - // match message { - // Some(Ok(message)) => { - // let payload = match message { - // Message::Binary(payload) => payload, - // Message::Ping(_) => { - // let mut socket = connection_state.socket.lock().await; - // let _ = socket.send(Message::Pong(vec![])).await; - // drop(socket); - // continue; - // } - // Message::Close(_) => return, - // _ => continue, - // }; - - // let message = NetworkMessage(payload.clone()); - - // if !handshaked { - // if handshake_handle(&message, &connection_state).await.is_ok() { - // if connection_state.handshaked.load(Ordering::SeqCst) { - // handshaked = true; - // } - // } - // } else { - // let raw = serde_json::from_slice::(&payload).unwrap(); - - // if let Some(target_instance) = &raw.target_instance { - // if connection_state.instance != *target_instance { - // // Forward request - // info!("Forwarding message to {}", target_instance.url); - // let mut instance_connections = instance_connections.lock().await; - // let pool = instance_connections.get_or_open(&target_instance).unwrap(); - // let pool_clone = pool.clone(); - // drop(pool); - - // let result = wrap_forwarded(&pool_clone, raw).await; - - // let mut socket = connection_state.socket.lock().await; - // let _ = socket.send(result).await; - - // continue; - // } - // } - - // let message_type = &raw.message_type; - - // match authentication_handle(message_type, &message, &connection_state).await { - // Err(e) => { - // let _ = connection_state - // .send_raw(ConnectionError(e.to_string())) - // .await; - // } - // Ok(true) => continue, - // Ok(false) => {} - // } - - // match repository_handle(message_type, &message, &connection_state).await { - // Err(e) => { - // let _ = connection_state - // .send_raw(ConnectionError(e.to_string())) - // .await; - // } - // Ok(true) => continue, - // Ok(false) => {} - // } - - // match user_handle(message_type, &message, &connection_state).await { - // Err(e) => { - // let _ = connection_state - // .send_raw(ConnectionError(e.to_string())) - // .await; - // } - // Ok(true) => continue, - // Ok(false) => {} - // } - - // match authentication_handle(message_type, &message, &connection_state).await { - // Err(e) => { - // let _ = connection_state - // .send_raw(ConnectionError(e.to_string())) - // .await; - // } - // Ok(true) => continue, - // Ok(false) => {} - // } - - // error!( - // "Message completely unhandled: {}", - // std::str::from_utf8(&payload).unwrap() - // ); - // } - // } - // Some(Err(e)) => { - // error!("Closing connection for {:?} for {}", e, addr); - // return; - // } - // _ => { - // continue; - // } - // } - // } } #[derive(Clone)] diff --git a/giterated-daemon/src/database_backend/handler.rs b/giterated-daemon/src/database_backend/handler.rs index 1b728a2..7e276d5 100644 --- a/giterated-daemon/src/database_backend/handler.rs +++ b/giterated-daemon/src/database_backend/handler.rs @@ -9,19 +9,19 @@ use giterated_models::{ }, object_backend::ObjectBackend, repository::{ - AccessList, Commit, DefaultBranch, Description, LatestCommit, Repository, + Commit, DefaultBranch, Description, LatestCommit, Repository, RepositoryCommitBeforeRequest, RepositoryDiff, RepositoryDiffPatchRequest, RepositoryDiffRequest, RepositoryFile, RepositoryFileFromIdRequest, RepositoryFileFromPathRequest, RepositoryFileInspectRequest, RepositoryInfoRequest, RepositorySummary, RepositoryView, Visibility, }, - settings::{AnySetting, GetSetting, GetSettingError, SetSetting, SetSettingError}, + settings::{AnySetting, GetSetting, GetSettingError}, user::{Bio, DisplayName, User, UserRepositoriesRequest}, value::{AnyValue, GetValue}, }; use giterated_stack::{ runtime::GiteratedRuntime, AuthenticatedUser, AuthorizedInstance, AuthorizedUser, - BackendWrapper, StackOperationState, + StackOperationState, }; use super::DatabaseBackend; @@ -30,7 +30,7 @@ pub fn user_get_repositories( object: &User, _operation: UserRepositoriesRequest, state: DatabaseBackend, - operation_state: StackOperationState, + _operation_state: StackOperationState, requester: Option, ) -> BoxFuture<'static, Result, OperationError>> { let object = object.clone(); @@ -99,25 +99,6 @@ pub fn user_get_setting( .boxed() } -// pub fn user_set_setting( -// object: &User, -// operation: SetSetting, -// state: DatabaseBackend, -// ) -> BoxFuture<'static, Result<(), OperationError>> { -// let object = object.clone(); - -// async move { -// let mut user_backend = state.user_backend.lock().await; -// let value = user_backend -// .write_setting(&object, &operation.setting_name, &operation.value.0) -// .await -// .map_err(|e| OperationError::Internal(e.to_string()))?; - -// Ok(value) -// } -// .boxed() -// } - pub fn repository_info( object: &Repository, operation: RepositoryInfoRequest, @@ -364,25 +345,6 @@ pub fn repository_get_setting( .boxed() } -// pub fn repository_set_setting( -// object: &Repository, -// operation: SetSetting, -// state: DatabaseBackend, -// ) -> BoxFuture<'static, Result<(), OperationError>> { -// let object = object.clone(); - -// async move { -// let mut repository_backend = state.repository_backend.lock().await; -// let value = repository_backend -// .write_setting(&object, &operation.setting_name, &operation.value.0) -// .await -// .map_err(|e| OperationError::Internal(e.to_string()))?; - -// Ok(value) -// } -// .boxed() -// } - pub fn instance_authentication_request( object: &Instance, operation: AuthenticationTokenRequest, diff --git a/giterated-daemon/src/database_backend/mod.rs b/giterated-daemon/src/database_backend/mod.rs index 648c7e7..9970676 100644 --- a/giterated-daemon/src/database_backend/mod.rs +++ b/giterated-daemon/src/database_backend/mod.rs @@ -21,12 +21,8 @@ use self::handler::{ instance_authentication_request, instance_create_repository_request, instance_registration_request, repository_commit_before, repository_diff, repository_diff_patch, repository_file_from_id, repository_file_from_path, - repository_get_setting, repository_get_value, repository_get_value_description, - repository_get_value_visibility, repository_info, user_get_repositories, user_get_setting, - user_get_value, user_get_value_bio, user_get_value_display_name, -}; -use self::updates::{ - repository_set_setting, repository_set_value, user_set_setting, user_set_value, + repository_get_value_description, repository_get_value_visibility, repository_info, + user_get_value_bio, user_get_value_display_name, }; #[derive(Clone, Debug)] @@ -56,6 +52,7 @@ impl ObjectBackend for Foobackend { /// A backend implementation which attempts to resolve data from the instance's database. #[derive(Clone)] +#[allow(unused)] pub struct DatabaseBackend { pub(self) our_instance: Instance, pub(self) user_backend: Arc>, @@ -116,310 +113,3 @@ impl Debug for DatabaseBackend { f.debug_struct("DatabaseBackend").finish() } } - -// TODO: These should be on the stack -// These tests verify that the essential handling of the database backend is -// functional and correct. -// #[cfg(test)] -// mod test { -// use std::{str::FromStr, sync::Arc}; - -// use anyhow::Error; - -// use giterated_models::authenticated::UserAuthenticationToken; - -// use giterated_models::instance::{ -// AuthenticationTokenRequest, Instance, RegisterAccountRequest, RepositoryCreateRequest, -// }; - -// use giterated_models::object_backend::ObjectBackend; - -// use giterated_models::repository::{ -// Commit, Description, Repository, RepositoryCommitBeforeRequest, RepositoryDiff, -// RepositoryDiffPatchRequest, RepositoryDiffRequest, RepositoryFile, -// RepositoryFileFromIdRequest, RepositoryFileFromPathRequest, RepositoryFileInspectRequest, -// RepositorySummary, RepositoryTreeEntry, -// }; -// use giterated_models::settings::AnySetting; -// use giterated_models::user::{DisplayName, User}; -// use giterated_models::value::{AnyValue, GiteratedObjectValue}; -// use giterated_stack::{AuthenticatedUser, StackOperationState}; -// use serde_json::Value; -// use tokio::sync::Mutex; - -// use crate::backend::{git::GitBackendError, AuthBackend, RepositoryBackend, UserBackend}; - -// use super::DatabaseBackend; -// pub struct TestUserDatabaseBackend; - -// #[async_trait::async_trait] -// impl UserBackend for TestUserDatabaseBackend { -// async fn get_value(&mut self, _user: &User, name: &str) -> Result, Error> { -// assert_eq!(name, DisplayName::value_name()); - -// Ok(serde_json::from_slice( -// &serde_json::to_vec(&DisplayName(String::from("test"))).unwrap(), -// ) -// .unwrap()) -// } -// async fn get_setting(&mut self, _user: &User, _name: &str) -> Result { -// Ok(serde_json::from_slice( -// &serde_json::to_vec(&DisplayName(String::from("test"))).unwrap(), -// ) -// .unwrap()) -// } -// async fn write_setting( -// &mut self, -// _user: &User, -// _name: &str, -// _setting: &Value, -// ) -> Result<(), Error> { -// Ok(()) -// } -// async fn exists(&mut self, user: &User) -> Result { -// Ok(user == &User::from_str("test_user:test.giterated.dev").unwrap()) -// } -// async fn repositories_for_user( -// &mut self, -// _requester: &Option, -// _user: &User, -// ) -> Result, Error> { -// todo!() -// } -// } - -// #[async_trait::async_trait] -// impl AuthBackend for TestUserDatabaseBackend { -// async fn register( -// &mut self, -// _request: RegisterAccountRequest, -// ) -> Result { -// todo!() -// } - -// async fn login( -// &mut self, -// _source: &Instance, -// _request: AuthenticationTokenRequest, -// ) -> Result { -// todo!() -// } -// } - -// pub struct TestUserRepositoryBackend; - -// #[async_trait::async_trait] -// impl RepositoryBackend for TestUserRepositoryBackend { -// async fn create_repository( -// &mut self, -// _user: &AuthenticatedUser, -// _request: &RepositoryCreateRequest, -// ) -> Result { -// todo!() -// } -// async fn repository_file_inspect( -// &mut self, -// _requester: &Option, -// _repository: &Repository, -// _request: &RepositoryFileInspectRequest, -// ) -> Result, Error> { -// todo!() -// } -// async fn repository_file_from_id( -// &mut self, -// _requester: &Option, -// _repository: &Repository, -// _request: &RepositoryFileFromIdRequest, -// ) -> Result { -// todo!() -// } -// async fn repository_file_from_path( -// &mut self, -// _requester: &Option, -// _repository: &Repository, -// _request: &RepositoryFileFromPathRequest, -// ) -> Result { -// todo!() -// } -// async fn repository_diff( -// &mut self, -// _requester: &Option, -// _repository: &Repository, -// _request: &RepositoryDiffRequest, -// ) -> Result { -// todo!() -// } -// async fn repository_diff_patch( -// &mut self, -// _requester: &Option, -// _repository: &Repository, -// _request: &RepositoryDiffPatchRequest, -// ) -> Result { -// todo!() -// } -// async fn repository_commit_before( -// &mut self, -// _requester: &Option, -// _repository: &Repository, -// _request: &RepositoryCommitBeforeRequest, -// ) -> Result { -// todo!() -// } -// async fn get_value( -// &mut self, -// _repository: &Repository, -// _name: &str, -// ) -> Result, Error> { -// Ok(serde_json::from_slice( -// &serde_json::to_vec(&Description(String::from("test"))).unwrap(), -// ) -// .unwrap()) -// } -// async fn get_setting( -// &mut self, -// _repository: &Repository, -// _name: &str, -// ) -> Result { -// Ok(serde_json::from_slice( -// &serde_json::to_vec(&Description(String::from("test"))).unwrap(), -// ) -// .unwrap()) -// } -// async fn write_setting( -// &mut self, -// _repository: &Repository, -// _name: &str, -// _setting: &Value, -// ) -> Result<(), Error> { -// Ok(()) -// } - -// async fn exists( -// &mut self, -// _requester: &Option, -// repository: &Repository, -// ) -> Result { -// // Ok(true) -// Ok(repository -// == &Repository::from_str( -// "test_user:test.giterated.dev/repository@test.giterated.dev", -// ) -// .unwrap()) -// } -// } - -// fn test_backend() -> GiteratedBackend { -// DatabaseBackend { -// our_instance: Instance::from_str("testing.giterated.dev").unwrap(), -// user_backend: Arc::new(Mutex::new(TestUserDatabaseBackend)) as _, -// repository_backend: Arc::new(Mutex::new(TestUserRepositoryBackend)) as _, -// } -// .into_backend() -// } - -// fn operation_state() -> StackOperationState { -// todo!() -// } - -// #[tokio::test] -// async fn test_user_get() { -// let backend = test_backend(); -// let operation_state = operation_state(); - -// let mut user = backend -// .get_object::("test_user:test.giterated.dev", &operation_state) -// .await -// .expect("object should have been returned"); - -// user.get::(&operation_state) -// .await -// .expect("object value should have been returned"); -// } - -// #[tokio::test] -// async fn test_user_get_setting() { -// let backend = test_backend(); -// let operation_state = operation_state(); - -// let mut user = backend -// .get_object::("test_user:test.giterated.dev", &operation_state) -// .await -// .expect("object should have been returned"); - -// user.get_setting::(&operation_state) -// .await -// .expect("object value should have been returned"); -// } - -// #[tokio::test] -// async fn test_user_set_setting() { -// let backend = test_backend(); -// let operation_state = operation_state(); - -// let mut user = backend -// .get_object::("test_user:test.giterated.dev", &operation_state) -// .await -// .expect("object should have been returned"); - -// user.set_setting::(DisplayName(String::from("test")), &operation_state) -// .await -// .expect("object value should have been returned"); -// } - -// #[tokio::test] -// async fn test_respository_get() { -// let backend = test_backend(); -// let operation_state = operation_state(); - -// let mut repository = backend -// .get_object::( -// "test_user:test.giterated.dev/repository@test.giterated.dev", -// &operation_state, -// ) -// .await -// .expect("object should have been returned"); - -// repository -// .get::(&operation_state) -// .await -// .expect("object value should have been returned"); -// } - -// #[tokio::test] -// async fn test_repository_get_setting() { -// let backend = test_backend(); -// let operation_state = operation_state(); - -// let mut repository = backend -// .get_object::( -// "test_user:test.giterated.dev/repository@test.giterated.dev", -// &operation_state, -// ) -// .await -// .expect("object should have been returned"); - -// repository -// .get_setting::(&operation_state) -// .await -// .expect("object value should have been returned"); -// } - -// #[tokio::test] -// async fn test_repository_set_setting() { -// let backend = test_backend(); -// let operation_state = operation_state(); - -// let mut repository = backend -// .get_object::( -// "test_user:test.giterated.dev/repository@test.giterated.dev", -// &operation_state, -// ) -// .await -// .expect("object should have been returned"); - -// repository -// .set_setting::(Description(String::from("test")), &operation_state) -// .await -// .expect("object value should have been returned"); -// } -// } diff --git a/giterated-daemon/src/database_backend/updates.rs b/giterated-daemon/src/database_backend/updates.rs index f5994b5..95ff671 100644 --- a/giterated-daemon/src/database_backend/updates.rs +++ b/giterated-daemon/src/database_backend/updates.rs @@ -11,45 +11,45 @@ use giterated_stack::{AuthorizedUser, StackOperationState}; use super::DatabaseBackend; pub fn user_set_value( - object: User, - value_name: String, - value: AnyValue, - operation_state: &StackOperationState, + _object: User, + _value_name: String, + _value: AnyValue, + _operation_state: &StackOperationState, ) -> BoxFuture<'static, Result<(), ()>> { todo!() } pub fn user_set_setting( - object: User, - value_name: String, - value: AnySetting, - operation_state: &StackOperationState, + _object: User, + _value_name: String, + _value: AnySetting, + _operation_state: &StackOperationState, ) -> BoxFuture<'static, Result<(), ()>> { todo!() } pub fn repository_set_value( - object: Repository, - value_name: String, - value: AnyValue, - operation_state: &StackOperationState, + _object: Repository, + _value_name: String, + _value: AnyValue, + _operation_state: &StackOperationState, ) -> BoxFuture<'static, Result<(), ()>> { todo!() } pub fn repository_set_setting( - object: Repository, - value_name: String, - value: AnySetting, - operation_state: &StackOperationState, + _object: Repository, + _value_name: String, + _value: AnySetting, + _operation_state: &StackOperationState, ) -> BoxFuture<'static, Result<(), ()>> { todo!() } pub fn repository_set_description( - object: Repository, - description: Description, - user: AuthorizedUser, + _object: Repository, + _description: Description, + _user: AuthorizedUser, ) -> BoxFuture<'static, Result<(), ()>> { async { Ok(()) }.boxed() } @@ -73,7 +73,7 @@ pub fn repository_set_default_branch( .await .unwrap(); - let set_value = ValueUpdate { + let _set_value = ValueUpdate { object: object.to_string(), value_name: DefaultBranch::value_name().to_owned(), value: unsafe { AnyValue::from_raw(serde_json::to_value(default_branch).unwrap()) }, diff --git a/giterated-daemon/src/main.rs b/giterated-daemon/src/main.rs index ebd8b0c..315b755 100644 --- a/giterated-daemon/src/main.rs +++ b/giterated-daemon/src/main.rs @@ -12,7 +12,7 @@ use giterated_daemon::{ use giterated_models::instance::Instance; -use giterated_stack::{runtime::GiteratedRuntime, BackendWrapper, StackOperationState}; +use giterated_stack::{runtime::GiteratedRuntime, StackOperationState}; use sqlx::{postgres::PgConnectOptions, ConnectOptions, PgPool}; use std::{net::SocketAddr, str::FromStr, sync::Arc}; use tokio::{ diff --git a/giterated-models/src/authenticated.rs b/giterated-models/src/authenticated.rs index 4f3944f..8989d05 100644 --- a/giterated-models/src/authenticated.rs +++ b/giterated-models/src/authenticated.rs @@ -49,7 +49,7 @@ impl AuthenticatedPayload { } } pub fn into_message_v2(self) -> GiteratedMessage { - let payload = serde_json::from_slice::(&self.payload).unwrap(); + let _payload = serde_json::from_slice::(&self.payload).unwrap(); GiteratedMessage { object: AnyObject(self.object), operation: self.operation, diff --git a/giterated-models/src/lib.rs b/giterated-models/src/lib.rs index 0ecbafe..5b557af 100644 --- a/giterated-models/src/lib.rs +++ b/giterated-models/src/lib.rs @@ -1,7 +1,3 @@ -use error::OperationError; -use object::GiteratedObject; -use operation::GiteratedOperation; - pub mod authenticated; pub mod discovery; pub mod error; diff --git a/giterated-models/src/settings/operations.rs b/giterated-models/src/settings/operations.rs index 3a9c091..3adf3b7 100644 --- a/giterated-models/src/settings/operations.rs +++ b/giterated-models/src/settings/operations.rs @@ -1,12 +1,12 @@ -use std::{fmt::Debug, marker::PhantomData}; +use std::fmt::Debug; -use serde::{de::DeserializeOwned, Deserialize, Serialize}; +use serde::{Deserialize, Serialize}; use serde_json::Value; use thiserror::Error; use crate::{object::GiteratedObject, operation::GiteratedOperation}; -use super::{AnySetting, Setting}; +use super::AnySetting; #[derive(Serialize, Deserialize, Debug, Clone)] pub struct GetSetting { diff --git a/giterated-models/src/update/instance.rs b/giterated-models/src/update/instance.rs index d6ff76d..8b13789 100644 --- a/giterated-models/src/update/instance.rs +++ b/giterated-models/src/update/instance.rs @@ -1 +1 @@ -pub struct InstanceUpdate {} + diff --git a/giterated-models/src/update/mod.rs b/giterated-models/src/update/mod.rs index d7fcacb..7fce100 100644 --- a/giterated-models/src/update/mod.rs +++ b/giterated-models/src/update/mod.rs @@ -10,6 +10,7 @@ pub struct ValueUpdate { pub value: AnyValue<()>, } +#[allow(unused)] pub struct SettingUpdate { object: String, value_name: String, diff --git a/giterated-stack/src/handler.rs b/giterated-stack/src/handler.rs index b3bcba8..8b13789 100644 --- a/giterated-stack/src/handler.rs +++ b/giterated-stack/src/handler.rs @@ -1,169 +1 @@ -use giterated_models::{ - error::OperationError, - instance::Instance, - object::{ - AnyObject, GiteratedObject, Object, ObjectRequest, ObjectRequestError, ObjectResponse, - }, - object_backend::ObjectBackend, - operation::{AnyOperation, GiteratedOperation}, -}; -use std::{any::Any, collections::HashMap, fmt::Debug, str::FromStr, sync::Arc}; -use tokio::sync::Mutex; -use tracing::warn; -use crate::{ - state::HandlerState, - update::{HandleSettingUpdatedFunction, HandleValueUpdatedFunction, ValueUpdateKind}, -}; - -use crate::StackOperationState; - -// #[derive(Clone)] -// pub struct GiteratedBackend { -// state: S, -// handlers: Arc>, -// } - -// impl GiteratedBackend { -// pub fn new(state: S, handlers: OperationHandlers) -> Self { -// Self { -// state, -// handlers: Arc::new(handlers), -// } -// } - -// pub fn state(&self) -> &S { -// &self.state -// } -// } - -// #[async_trait::async_trait] -// impl ObjectBackend for GiteratedBackend { -// async fn object_operation( -// &self, -// object: O, -// operation: &str, -// payload: D, -// operation_state: &StackOperationState, -// ) -> Result> -// where -// O: GiteratedObject + Debug + 'static, -// D: GiteratedOperation + Debug, -// { -// let serialized = -// serde_json::to_value(payload).map_err(|e| OperationError::Internal(e.to_string()))?; -// let object = (Box::new(object) as Box) -// .downcast::() -// .unwrap(); - -// if operation == ObjectRequest::operation_name() { -// // We're doing an object request -// let raw_result = self -// .handlers -// .resolve_object( -// *(Box::new(object) as Box) -// .downcast() -// .unwrap(), -// serde_json::from_value(serialized).unwrap(), -// self.state.clone(), -// &operation_state, -// ) -// .await; - -// return match raw_result { -// Ok(result) => Ok(serde_json::from_slice(&result) -// .map_err(|e| OperationError::Internal(e.to_string()))?), -// Err(err) => match err { -// OperationError::Internal(internal) => { -// warn!( -// "Internal Error: {:?}", -// OperationError::<()>::Internal(internal.clone()) -// ); - -// Err(OperationError::Internal(internal)) -// } -// OperationError::Unhandled => Err(OperationError::Unhandled), -// OperationError::Operation(err) => Err(OperationError::Operation( -// serde_json::from_slice(&err) -// .map_err(|e| OperationError::Internal(e.to_string()))?, -// )), -// }, -// }; -// } - -// let raw_result = self -// .handlers -// .handle( -// &*object, -// operation, -// AnyOperation(serialized), -// self.state.clone(), -// &operation_state, -// ) -// .await; - -// match raw_result { -// Ok(result) => Ok(serde_json::from_slice(&result) -// .map_err(|e| OperationError::Internal(e.to_string()))?), -// Err(err) => match err { -// OperationError::Internal(internal) => { -// warn!( -// "Internal Error: {:?}", -// OperationError::<()>::Internal(internal.clone()) -// ); - -// Err(OperationError::Internal(internal)) -// } -// OperationError::Unhandled => Err(OperationError::Unhandled), -// OperationError::Operation(err) => Err(OperationError::Operation( -// serde_json::from_slice(&err) -// .map_err(|e| OperationError::Internal(e.to_string()))?, -// )), -// }, -// } -// } - -// async fn get_object( -// &self, -// object_str: &str, -// operation_state: &StackOperationState, -// ) -> Result, OperationError> { -// let raw_result = self -// .handlers -// .resolve_object( -// Instance::from_str("giterated.dev").unwrap(), -// ObjectRequest(object_str.to_string()), -// self.state.clone(), -// operation_state, -// ) -// .await; - -// let object: ObjectResponse = match raw_result { -// Ok(result) => Ok(serde_json::from_slice(&result) -// .map_err(|e| OperationError::Internal(e.to_string()))?), -// Err(err) => match err { -// OperationError::Internal(internal) => { -// warn!( -// "Internal Error: {:?}", -// OperationError::<()>::Internal(internal.clone()) -// ); - -// Err(OperationError::Internal(internal)) -// } -// OperationError::Unhandled => Err(OperationError::Unhandled), -// OperationError::Operation(err) => Err(OperationError::Operation( -// serde_json::from_slice(&err) -// .map_err(|e| OperationError::Internal(e.to_string()))?, -// )), -// }, -// }?; - -// unsafe { -// Ok(Object::new_unchecked( -// O::from_str(&object.0) -// .map_err(|_| OperationError::Internal("deserialize failure".to_string()))?, -// self.clone(), -// )) -// } -// } -// } diff --git a/giterated-stack/src/lib.rs b/giterated-stack/src/lib.rs index d013804..bf15597 100644 --- a/giterated-stack/src/lib.rs +++ b/giterated-stack/src/lib.rs @@ -3,39 +3,25 @@ pub mod runtime; pub mod state; pub mod update; -use std::{ - any::Any, collections::HashMap, future::Future, ops::Deref, pin::Pin, str::FromStr, sync::Arc, -}; +use std::{any::Any, future::Future, ops::Deref, pin::Pin, sync::Arc}; +use core::fmt::Debug; use futures_util::FutureExt; use giterated_models::{ error::OperationError, instance::{ AuthenticationTokenRequest, Instance, RegisterAccountRequest, RepositoryCreateRequest, }, - object::{ - AnyObject, GiteratedObject, Object, ObjectRequest, ObjectRequestError, ObjectResponse, - }, + object::GiteratedObject, object_backend::ObjectBackend, - operation::{AnyOperation, GiteratedOperation}, + operation::GiteratedOperation, repository::{AccessList, Repository}, - settings::{AnySetting, GetSetting, SetSetting, Setting}, + settings::{GetSetting, SetSetting}, user::User, - value::{AnyValue, GetValue, GiteratedObjectValue}, + value::{GetValue, GiteratedObjectValue}, }; use runtime::{GiteratedRuntime, GiteratedRuntimeState}; use serde::{de::DeserializeOwned, Serialize}; -use serde_json::Value; -use state::HandlerState; -use tokio::{ - sync::{mpsc::channel, Mutex}, - task::JoinHandle, -}; -use tracing::{error, warn}; -use update::{ - HandleSettingUpdate, HandleSettingUpdatedFunction, HandleValueUpdate, - HandleValueUpdatedFunction, SettingUpdateKind, ValueUpdateKind, -}; #[derive(Clone, Debug, Hash, Eq, PartialEq)] struct ObjectOperationPair { @@ -69,287 +55,12 @@ pub struct OperationMeta { serialize_error: Box) -> Result, serde_json::Error> + Send + Sync>, } -// pub struct OperationHandlers { -// operations: HashMap>, -// get_object: Vec>, -// value_getters: HashMap>, -// settings_getter: HashMap>, -// settings: HashMap, -// objects: HashMap, -// operations_meta: HashMap, -// value_updated: HashMap, -// setting_updated: HashMap, -// } - #[derive(Clone, Debug, Hash, Eq, PartialEq)] pub struct ObjectValuePair { pub object_kind: String, pub value_kind: String, } -// impl Default for OperationHandlers { -// fn default() -> Self { -// Self { -// operations: HashMap::new(), -// get_object: Vec::new(), -// value_updated: HashMap::default(), -// setting_updated: HashMap::default(), -// value_getters: HashMap::default(), -// settings_getter: HashMap::default(), -// settings: HashMap::default(), -// objects: HashMap::default(), -// operations_meta: HashMap::default(), -// } -// } -// } - -// impl OperationHandlers { -// fn insert_operation + Send + Sync + 'static>( -// &mut self, -// ) { -// // let object_name = O::object_name().to_string(); -// // let operation_name = D::operation_name().to_string(); - -// // self.operations_meta.insert( -// // ObjectOperationPair { -// // object_name: object_name.clone(), -// // operation_name: operation_name.clone(), -// // }, -// // OperationMeta { -// // name: operation_name, -// // object_kind: object_name, -// // deserialize: Box::new(|bytes| { -// // Ok(Box::new(serde_json::from_slice::(bytes).unwrap()) -// // as Box) -// // }), -// // any_is_same: Box::new(|any_box| any_box.is::()), -// // }, -// // ); -// } - -// pub fn setting(&mut self) -> &mut Self { -// let setting_meta = SettingMeta { -// name: T::name().to_string(), -// deserialize: Box::new(|slice| { -// Ok(Box::new(serde_json::from_slice(slice)?) as Box) -// }), -// }; - -// self.settings.insert(T::name().to_string(), setting_meta); - -// self -// } - -// pub fn insert< -// A, -// O: GiteratedObject + Send + Sync + 'static, -// D: GiteratedOperation + Send + Sync + 'static, -// H: GiteratedOperationHandler + Send + Sync + 'static + Clone, -// >( -// &mut self, -// handler: H, -// ) -> &mut Self { -// let object_name = handler.object_name().to_string(); -// let operation_name = handler.operation_name().to_string(); - -// let wrapped = OperationWrapper::new(handler); - -// let pair = ObjectOperationPair { -// object_name, -// operation_name, -// }; - -// assert!(self.operations.insert(pair, wrapped).is_none()); - -// self.insert_operation::(); - -// self -// } - -// pub fn object(&mut self) -> &mut Self { -// // let object_meta = ObjectMeta { -// // name: O::object_name().to_string(), -// // from_str: Box::new(|str| Ok(Box::new(O::from_str(&str).map_err(|_| ())?))), - -// // }; - -// // self.objects -// // .insert(O::object_name().to_string(), object_meta); - -// let closure = |_: &Instance, operation: ObjectRequest, _state| { -// async move { -// if O::from_str(&operation.0).is_ok() { -// Ok(ObjectResponse(operation.0)) -// } else { -// Err(OperationError::Unhandled) -// } -// } -// .boxed() -// }; - -// let wrapped = OperationWrapper::new(closure); - -// self.get_object.push(wrapped); - -// self -// } - -// pub async fn handle( -// &self, -// object: &O, -// operation_name: &str, -// operation: AnyOperation, -// state: S, -// operation_state: &StackOperationState, -// ) -> Result, OperationError>> { -// // TODO -// let object = object.to_string(); -// let object_name = O::object_name().to_string(); - -// let object_meta = self -// .objects -// .get(&object_name) -// .ok_or_else(|| OperationError::Unhandled)?; - -// let object_box = (object_meta.from_str)(&object).map_err(|_| OperationError::Unhandled)?; - -// let target_handler = ObjectOperationPair { -// object_name, -// operation_name: operation_name.to_string(), -// }; - -// let operation_meta = self -// .operations_meta -// .get(&target_handler) -// .ok_or_else(|| OperationError::Unhandled)?; - -// let operation_box = -// (operation_meta.deserialize)(&serde_json::to_vec(&operation.0).unwrap()) -// .map_err(|_| OperationError::Unhandled)?; - -// if let Some(handler) = self.operations.get(&target_handler) { -// handler -// .handle(object_box, operation_box, operation_state) -// .await -// } else { -// Err(OperationError::Unhandled) -// } -// } - -// pub async fn resolve_object( -// &self, -// instance: Instance, -// request: ObjectRequest, -// state: S, -// operation_state: &StackOperationState, -// ) -> Result, OperationError>> { -// for handler in self.get_object.iter() { -// if let Ok(response) = handler -// .handle( -// Box::new(instance.clone()) as _, -// Box::new(request.clone()) as _, -// operation_state, -// ) -// .await -// { -// return Ok(response); -// } -// } - -// Err(OperationError::Unhandled) -// } - -// pub fn insert_value_update_handler< -// H: HandleValueUpdate + Send + Sync + Clone + 'static, -// O: GiteratedObject + Send + Sync, -// V: GiteratedObjectValue + Send + Sync, -// >( -// &mut self, -// handler: H, -// ) -> &mut Self { -// let wrapper = HandleValueUpdatedFunction::new(handler, V::value_name()); - -// assert!(self -// .value_updated -// .insert(wrapper.target.clone(), wrapper) -// .is_none()); - -// self -// } - -// pub fn insert_setting_update_handler< -// H: HandleSettingUpdate + Send + Sync + Clone + 'static, -// O: GiteratedObject + Send + Sync, -// T: Setting + Send + Sync, -// >( -// &mut self, -// handler: H, -// ) -> &mut Self { -// let wrapper = HandleSettingUpdatedFunction::new(handler, T::name()); - -// assert!(self -// .setting_updated -// .insert(wrapper.target.clone(), wrapper) -// .is_none()); - -// self -// } - -// pub fn value_getter(&mut self, handler: F) -> &mut Self -// where -// O: GiteratedObject + Send + Sync + 'static, -// V: GiteratedObjectValue + Send + Sync + 'static, -// F: GiteratedOperationHandler, S> + Send + Sync + Clone + 'static, -// { -// let object_name = handler.object_name().to_string(); -// let value_name = V::value_name().to_string(); - -// let wrapped = OperationWrapper::new(handler); - -// assert!(self -// .value_getters -// .insert( -// ValueGetter { -// object_type: object_name, -// value_type: value_name -// }, -// wrapped -// ) -// .is_none()); - -// self -// } - -// pub fn setting_getter(&mut self, handler: F) -> &mut Self -// where -// O: GiteratedObject + Send + Sync + 'static, -// F: GiteratedOperationHandler + Send + Sync + Clone + 'static, -// { -// let object_name = handler.object_name().to_string(); - -// let wrapped = OperationWrapper::new(handler); - -// assert!(self.settings_getter.insert(object_name, wrapped).is_none()); - -// self -// } -// } - -#[derive(Clone, Debug, Hash, PartialEq, Eq)] -pub struct ValueGetter { - pub object_type: String, - pub value_type: String, -} - -impl Default for ValueGetter { - fn default() -> Self { - Self { - object_type: AnyObject::object_name().to_string(), - value_type: "any".to_string(), - } - } -} - #[async_trait::async_trait] pub trait GiteratedOperationHandler< L, @@ -987,156 +698,3 @@ impl Deref for AuthenticatedUser { &self.0 } } - -#[derive(Clone)] -pub struct BackendWrapper { - sender: tokio::sync::mpsc::Sender<( - tokio::sync::oneshot::Sender>>, - WrappedOperation, - )>, - task: Arc>, -} - -pub struct WrappedOperation { - object: AnyObject, - operation_payload: AnyOperation, - operation_name: String, - state: StackOperationState, -} - -// impl BackendWrapper { -// pub fn new(backend: GiteratedBackend) -> Self { -// // Spawn listener task - -// let (send, mut recv) = channel::<( -// tokio::sync::oneshot::Sender>>, -// WrappedOperation, -// )>(1024); - -// let task = tokio::spawn(async move { -// while let Some((responder, message)) = recv.recv().await { -// let raw_result = backend -// .object_operation( -// message.object, -// &message.operation_name, -// message.operation_payload, -// &message.state, -// ) -// .await; - -// responder.send(raw_result).unwrap(); -// } -// error!("Error, thing's dead"); -// }); - -// Self { -// sender: send, -// task: Arc::new(task), -// } -// } - -// pub async fn call(&self, operation: WrappedOperation) -> Result> { -// let (sender, response) = tokio::sync::oneshot::channel(); - -// self.sender -// .send((sender, operation)) -// .await -// .map_err(|e| OperationError::Internal(e.to_string()))?; - -// match response.await { -// Ok(result) => Ok(result?), -// Err(err) => Err(OperationError::Internal(err.to_string())), -// } -// } -// } - -use std::fmt::Debug; - -// #[async_trait::async_trait] -// impl ObjectBackend for BackendWrapper { -// async fn object_operation( -// &self, -// object: O, -// operation: &str, -// payload: D, -// operation_state: &StackOperationState, -// ) -> Result> -// where -// O: GiteratedObject + Debug, -// D: GiteratedOperation + Debug, -// { -// let operation = WrappedOperation { -// object: AnyObject(object.to_string()), -// operation_name: operation.to_string(), -// operation_payload: AnyOperation(serde_json::to_value(payload).unwrap()), -// state: operation_state.clone(), -// }; - -// let raw_result = self.call(operation).await; - -// match raw_result { -// Ok(result) => Ok(serde_json::from_value(result) -// .map_err(|e| OperationError::Internal(e.to_string()))?), -// Err(err) => match err { -// OperationError::Internal(internal) => { -// warn!( -// "Internal Error: {:?}", -// OperationError::<()>::Internal(internal.clone()) -// ); - -// Err(OperationError::Internal(internal)) -// } -// OperationError::Unhandled => Err(OperationError::Unhandled), -// OperationError::Operation(err) => Err(OperationError::Operation( -// serde_json::from_value(err) -// .map_err(|e| OperationError::Internal(e.to_string()))?, -// )), -// }, -// } -// } - -// async fn get_object( -// &self, -// object_str: &str, -// operation_state: &StackOperationState, -// ) -> Result, OperationError> { -// let operation = WrappedOperation { -// object: AnyObject(object_str.to_string()), -// operation_name: ObjectRequest::operation_name().to_string(), -// operation_payload: AnyOperation( -// serde_json::to_value(ObjectRequest(object_str.to_string())).unwrap(), -// ), -// state: operation_state.clone(), -// }; - -// let raw_result = self.call(operation).await; - -// let object: ObjectResponse = match raw_result { -// Ok(result) => Ok(serde_json::from_value(result) -// .map_err(|e| OperationError::Internal(e.to_string()))?), -// Err(err) => match err { -// OperationError::Internal(internal) => { -// warn!( -// "Internal Error: {:?}", -// OperationError::<()>::Internal(internal.clone()) -// ); - -// Err(OperationError::Internal(internal)) -// } -// OperationError::Unhandled => Err(OperationError::Unhandled), -// OperationError::Operation(err) => Err(OperationError::Operation( -// serde_json::from_value(err) -// .map_err(|e| OperationError::Internal(e.to_string()))?, -// )), -// }, -// }?; - -// unsafe { -// Ok(Object::new_unchecked( -// O::from_str(&object.0) -// .map_err(|_| OperationError::Internal("deserialize failure".to_string()))?, -// self.clone(), -// )) -// } -// } -// } diff --git a/giterated-stack/src/runtime.rs b/giterated-stack/src/runtime.rs index 0c3008b..7365f38 100644 --- a/giterated-stack/src/runtime.rs +++ b/giterated-stack/src/runtime.rs @@ -6,8 +6,8 @@ use giterated_models::{ message::GiteratedMessage, object::{AnyObject, GiteratedObject, Object, ObjectRequestError}, object_backend::ObjectBackend, - operation::{AnyOperation, AnyOperationV2, GiteratedOperation}, - settings::{AnySetting, GetSetting, SetSetting, Setting}, + operation::{AnyOperationV2, GiteratedOperation}, + settings::{GetSetting, SetSetting, Setting}, value::{GetValue, GetValueV2, GiteratedObjectValue}, }; use tracing::trace; @@ -79,9 +79,9 @@ impl HandlerTree { pub fn handle( &self, - object: &dyn Any, - operation: Box, - operation_state: &StackOperationState, + _object: &dyn Any, + _operation: Box, + _operation_state: &StackOperationState, ) -> Result, OperationError>> { todo!() } @@ -370,7 +370,7 @@ where &self, object: &O, operation: D, - state: S, + _state: S, operation_state: &StackOperationState, ) -> Result> { // Erase object and operation types. @@ -451,7 +451,7 @@ impl GiteratedRuntime { pub async fn handle_network_message( &self, message: AuthenticatedPayload, - state: &S, + _state: &S, operation_state: &StackOperationState, ) -> Result, OperationError>> { let message: GiteratedMessage = message.into_message_v2(); @@ -517,10 +517,10 @@ use core::fmt::Debug; impl ObjectBackend for Arc { async fn object_operation( &self, - object: O, - operation: &str, - payload: D, - operation_state: &StackOperationState, + _object: O, + _operation: &str, + _payload: D, + _operation_state: &StackOperationState, ) -> Result> where O: GiteratedObject + Debug + 'static, @@ -531,8 +531,8 @@ impl ObjectBackend for Arc { async fn get_object( &self, - object_str: &str, - operation_state: &StackOperationState, + _object_str: &str, + _operation_state: &StackOperationState, ) -> Result, OperationError> { todo!() } diff --git a/giterated-stack/src/update.rs b/giterated-stack/src/update.rs index efd833e..9879358 100644 --- a/giterated-stack/src/update.rs +++ b/giterated-stack/src/update.rs @@ -1,5 +1,3 @@ -use std::sync::Arc; - use futures_util::{future::BoxFuture, FutureExt}; use giterated_models::{ object::GiteratedObject, @@ -129,7 +127,7 @@ impl HandleSettingUpdatedFunction { let setting: S = serde_json::from_value(value.0).unwrap(); - handler + let _ = handler .handle_setting_update(object, setting_name, setting, &state) .await; @@ -181,7 +179,7 @@ impl HandleValueUpdatedFunction { let setting: V = serde_json::from_value(value.0).unwrap(); - handler + let _ = handler .handle_value_update(object, setting_name, setting, &state) .await;