Skip to content

Commit ce2e025

Browse files
committed
refactor(method_response): move to foundation
1 parent 356344a commit ce2e025

File tree

7 files changed

+74
-71
lines changed

7 files changed

+74
-71
lines changed

codegen/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ fn generate_aspect(aspect: &Aspect) -> TokenStream {
306306
_node: std::sync::Arc<crate::nodes::Node>,
307307
_method: u64,
308308
_message: crate::nodes::Message,
309-
_method_response: crate::core::scenegraph::MethodResponseSender,
309+
_method_response: stardust_xr_server_foundation::MethodResponseSender,
310310
) {
311311
match _method {
312312
#run_methods

foundation/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
pub mod delta;
22
pub mod error;
3-
pub mod id;
3+
mod id;
4+
mod method_response;
45
pub mod registry;
56
pub mod resource;
67
pub mod task;
78

89
pub use id::*;
10+
pub use method_response::*;

foundation/src/method_response.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
use crate::error::{Result, ServerError};
2+
use serde::Serialize;
3+
use stardust_xr_wire::{flex::serialize, messenger::MethodResponse, scenegraph::ScenegraphError};
4+
use std::os::fd::OwnedFd;
5+
6+
pub struct MethodResponseSender(pub(crate) MethodResponse);
7+
impl MethodResponseSender {
8+
pub fn send_err(self, error: ScenegraphError) {
9+
self.0.send(Err(error));
10+
}
11+
pub fn send<T: Serialize>(self, result: Result<T, ServerError>) {
12+
let data = match result {
13+
Ok(d) => d,
14+
Err(e) => {
15+
self.0.send(Err(ScenegraphError::MemberError {
16+
error: e.to_string(),
17+
}));
18+
return;
19+
}
20+
};
21+
let Ok(serialized) = stardust_xr_wire::flex::serialize(data) else {
22+
self.0.send(Err(ScenegraphError::MemberError {
23+
error: "Internal: Failed to serialize".to_string(),
24+
}));
25+
return;
26+
};
27+
self.0.send(Ok((&serialized, Vec::<OwnedFd>::new())));
28+
}
29+
pub fn wrap<T: Serialize, F: FnOnce() -> Result<T>>(self, f: F) {
30+
self.send(f())
31+
}
32+
pub fn wrap_async<T: Serialize>(
33+
self,
34+
f: impl Future<Output = Result<(T, Vec<OwnedFd>)>> + Send + 'static,
35+
) {
36+
tokio::task::spawn(async move {
37+
let (value, fds) = match f.await {
38+
Ok(d) => d,
39+
Err(e) => {
40+
self.0.send(Err(ScenegraphError::MemberError {
41+
error: e.to_string(),
42+
}));
43+
return;
44+
}
45+
};
46+
let Ok(serialized) = serialize(value) else {
47+
self.0.send(Err(ScenegraphError::MemberError {
48+
error: "Internal: Failed to serialize".to_string(),
49+
}));
50+
return;
51+
};
52+
self.0.send(Ok((&serialized, fds)));
53+
});
54+
}
55+
}
56+
impl From<MethodResponse> for MethodResponseSender {
57+
fn from(response: MethodResponse) -> Self {
58+
Self(response)
59+
}
60+
}
61+
impl std::fmt::Debug for MethodResponseSender {
62+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
63+
f.debug_struct("TypedMethodResponse").finish()
64+
}
65+
}

src/core/scenegraph.rs

Lines changed: 2 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
use crate::{
2-
core::{
3-
Id,
4-
client::Client,
5-
error::{Result, ServerError},
6-
},
2+
core::{Id, client::Client, error::Result},
73
nodes::{Message, Node, alias::get_original},
84
};
95
use dashmap::DashMap;
10-
use serde::Serialize;
116
use stardust_xr_wire::{
12-
flex::serialize,
137
messenger::MethodResponse,
148
scenegraph::{self, ScenegraphError},
159
};
@@ -19,62 +13,6 @@ use std::{
1913
};
2014
use tracing::{debug, debug_span};
2115

22-
pub struct MethodResponseSender(pub(crate) MethodResponse);
23-
impl MethodResponseSender {
24-
pub fn send_err(self, error: ScenegraphError) {
25-
self.0.send(Err(error));
26-
}
27-
pub fn send<T: Serialize>(self, result: Result<T, ServerError>) {
28-
let data = match result {
29-
Ok(d) => d,
30-
Err(e) => {
31-
self.0.send(Err(ScenegraphError::MemberError {
32-
error: e.to_string(),
33-
}));
34-
return;
35-
}
36-
};
37-
let Ok(serialized) = stardust_xr_wire::flex::serialize(data) else {
38-
self.0.send(Err(ScenegraphError::MemberError {
39-
error: "Internal: Failed to serialize".to_string(),
40-
}));
41-
return;
42-
};
43-
self.0.send(Ok((&serialized, Vec::<OwnedFd>::new())));
44-
}
45-
pub fn wrap<T: Serialize, F: FnOnce() -> Result<T>>(self, f: F) {
46-
self.send(f())
47-
}
48-
pub fn wrap_async<T: Serialize>(
49-
self,
50-
f: impl Future<Output = Result<(T, Vec<OwnedFd>)>> + Send + 'static,
51-
) {
52-
tokio::task::spawn(async move {
53-
let (value, fds) = match f.await {
54-
Ok(d) => d,
55-
Err(e) => {
56-
self.0.send(Err(ScenegraphError::MemberError {
57-
error: e.to_string(),
58-
}));
59-
return;
60-
}
61-
};
62-
let Ok(serialized) = serialize(value) else {
63-
self.0.send(Err(ScenegraphError::MemberError {
64-
error: "Internal: Failed to serialize".to_string(),
65-
}));
66-
return;
67-
};
68-
self.0.send(Ok((&serialized, fds)));
69-
});
70-
}
71-
}
72-
impl std::fmt::Debug for MethodResponseSender {
73-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
74-
f.debug_struct("TypedMethodResponse").finish()
75-
}
76-
}
77-
7816
#[derive(Default)]
7917
pub struct Scenegraph {
8018
pub(super) client: OnceLock<Weak<Client>>,
@@ -157,7 +95,7 @@ impl scenegraph::Scenegraph for Scenegraph {
15795
data: data.to_vec(),
15896
fds,
15997
},
160-
MethodResponseSender(response),
98+
response.into(),
16199
);
162100
}
163101
}

src/nodes/alias.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use super::{Aspect, AspectIdentifier, Node};
2-
use crate::core::{
3-
Id, client::Client, error::Result, registry::Registry, scenegraph::MethodResponseSender,
4-
};
2+
use crate::core::{Id, MethodResponseSender, client::Client, error::Result, registry::Registry};
53
use std::{
64
ops::Add,
75
sync::{Arc, Weak},

src/nodes/items/camera.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::nodes::AspectIdentifier;
77
use crate::nodes::items::ITEM_ACCEPTOR_ASPECT_ALIAS_INFO;
88
use crate::nodes::items::ITEM_ASPECT_ALIAS_INFO;
99
use crate::{
10-
core::{client::Client, registry::Registry, scenegraph::MethodResponseSender},
10+
core::{MethodResponseSender, client::Client, registry::Registry},
1111
nodes::{
1212
Message, Node,
1313
drawable::model::ModelPart,

src/nodes/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use self::alias::Alias;
1111
use crate::core::client::Client;
1212
use crate::core::error::{Result, ServerError};
1313
use crate::core::registry::Registry;
14-
use crate::core::{Id, scenegraph::MethodResponseSender};
14+
use crate::core::{Id, MethodResponseSender};
1515
use dashmap::DashMap;
1616
use serde::{Serialize, de::DeserializeOwned};
1717
use spatial::Spatial;

0 commit comments

Comments
 (0)