switch to color_eyre instead of anyhow

This commit is contained in:
Nova
2022-12-02 13:58:54 -05:00
parent 03ccf9127d
commit d7a607a663
29 changed files with 114 additions and 118 deletions

View File

@@ -9,7 +9,7 @@ repository = "https://github.com/StardustXR/stardust-xr-server/"
homepage = "https://stardustxr.org"
[dependencies]
anyhow = "1.0.57"
color-eyre = { version = "0.6.2", default-features = false }
clap = { version = "4.0.8", features = ["derive"] }
ctrlc = "3.2.2"
dashmap = "5.3.4"
@@ -32,7 +32,7 @@ prisma = "0.1.1"
slog = "2.7.0"
slog-stdlog = "4.1.1"
xkbcommon = { version = "0.5.0", default-features = false, optional = true }
stardust-xr = "0.7.1"
stardust-xr = "0.8.0"
directories = "4.0.1"
serde = { version = "1.0.145", features = ["derive"] }

View File

@@ -3,7 +3,7 @@ use crate::{
core::registry::Registry,
nodes::{data, drawable, fields, hmd, input, items, root::Root, spatial, startup, Node},
};
use anyhow::{anyhow, Result};
use color_eyre::eyre::{eyre, Result};
use lazy_static::lazy_static;
use once_cell::sync::OnceCell;
use parking_lot::Mutex;
@@ -104,7 +104,7 @@ impl Client {
pub fn get_node(&self, name: &'static str, path: &str) -> Result<Arc<Node>> {
self.scenegraph
.get_node(path)
.ok_or_else(|| anyhow!("{} not found", name))
.ok_or_else(|| eyre!("{} not found", name))
}
pub async fn disconnect(&self) {

View File

@@ -1,5 +1,5 @@
use super::client::Client;
use anyhow::Result;
use color_eyre::eyre::Result;
use slab::Slab;
use stardust_xr::server;
use std::sync::atomic::AtomicU64;

View File

@@ -6,31 +6,31 @@ use std::{
sync::{Arc, Weak},
};
#[derive(Default)]
pub struct LifeLinkedNodeList {
nodes: Mutex<Vec<Weak<Node>>>,
}
impl LifeLinkedNodeList {
pub fn add(&self, node: Weak<Node>) {
self.nodes.lock().push(node);
}
// #[derive(Default)]
// pub struct LifeLinkedNodeList {
// nodes: Mutex<Vec<Weak<Node>>>,
// }
// impl LifeLinkedNodeList {
// pub fn add(&self, node: Weak<Node>) {
// self.nodes.lock().push(node);
// }
pub fn clear(&self) {
self.nodes
.lock()
.iter()
.filter_map(|node| node.upgrade())
.for_each(|node| {
node.destroy();
});
self.nodes.lock().clear();
}
}
impl Drop for LifeLinkedNodeList {
fn drop(&mut self) {
self.clear();
}
}
// pub fn clear(&self) {
// self.nodes
// .lock()
// .iter()
// .filter_map(|node| node.upgrade())
// .for_each(|node| {
// node.destroy();
// });
// self.nodes.lock().clear();
// }
// }
// impl Drop for LifeLinkedNodeList {
// fn drop(&mut self) {
// self.clear();
// }
// }
#[derive(Default)]
pub struct LifeLinkedNodeMap<K: Hash + Eq> {

View File

@@ -1,4 +1,4 @@
use anyhow::anyhow;
use color_eyre::eyre::eyre;
use serde::{de::Visitor, Deserialize};
use std::path::PathBuf;
@@ -56,9 +56,7 @@ impl<'de> Visitor<'de> for ResourceVisitor {
path: PathBuf::from(path),
}
} else {
return Err(serde::de::Error::custom(anyhow!(
"Invalid format for string"
)));
return Err(serde::de::Error::custom(eyre!("Invalid format for string")));
})
}

View File

@@ -1,6 +1,6 @@
use crate::core::client::Client;
use crate::nodes::Node;
use anyhow::Result;
use color_eyre::eyre::Result;
use once_cell::sync::OnceCell;
use stardust_xr::scenegraph;
use stardust_xr::scenegraph::ScenegraphError;

View File

@@ -11,8 +11,8 @@ use crate::objects::input::sk_controller::SkController;
use crate::objects::input::sk_hand::SkHand;
use self::core::eventloop::EventLoop;
use anyhow::Result;
use clap::Parser;
use color_eyre::eyre::Result;
use directories::ProjectDirs;
use std::sync::Arc;
use stereokit::input::Handed;
@@ -151,7 +151,7 @@ fn main() -> Result<()> {
async fn event_loop(
handle_sender: oneshot::Sender<Handle>,
stop_rx: oneshot::Receiver<()>,
) -> anyhow::Result<()> {
) -> color_eyre::eyre::Result<()> {
let _ = handle_sender.send(Handle::current());
// console_subscriber::init();

View File

@@ -7,7 +7,7 @@ use crate::core::node_collections::LifeLinkedNodeMap;
use crate::core::registry::Registry;
use crate::nodes::fields::find_field;
use crate::nodes::spatial::find_spatial_parent;
use anyhow::{anyhow, ensure, Result};
use color_eyre::eyre::{ensure, eyre, Result};
use glam::vec3a;
use mint::{Quaternion, Vector3};
use nanoid::nanoid;
@@ -37,9 +37,9 @@ pub struct Mask(pub Vec<u8>);
impl Mask {
pub fn get_mask(&self) -> Result<flexbuffers::MapReader<&[u8]>> {
flexbuffers::Reader::get_root(self.0.as_slice())
.map_err(|_| anyhow!("Mask is not a valid flexbuffer"))?
.map_err(|_| eyre!("Mask is not a valid flexbuffer"))?
.get_map()
.map_err(|_| anyhow!("Mask is not a valid map"))
.map_err(|_| eyre!("Mask is not a valid map"))
}
}

View File

@@ -5,7 +5,7 @@ use crate::{
Node,
},
};
use anyhow::{ensure, Result};
use color_eyre::eyre::{ensure, Result};
use glam::Vec3A;
use mint::Vector3;
use parking_lot::Mutex;

View File

@@ -4,7 +4,7 @@ pub mod text;
use super::Node;
use crate::core::client::Client;
use anyhow::Result;
use color_eyre::eyre::Result;
use parking_lot::Mutex;
use serde::Deserialize;
use stardust_xr::schemas::flex::deserialize;

View File

@@ -4,7 +4,7 @@ use crate::core::destroy_queue;
use crate::core::registry::Registry;
use crate::core::resource::ResourceID;
use crate::nodes::spatial::{find_spatial_parent, parse_transform, Spatial};
use anyhow::{anyhow, ensure, Result};
use color_eyre::eyre::{ensure, eyre, Result};
use once_cell::sync::OnceCell;
use parking_lot::Mutex;
use prisma::{Rgb, Rgba};
@@ -66,12 +66,12 @@ impl Model {
.get_file(
&node
.get_client()
.ok_or_else(|| anyhow!("Client not found"))?
.ok_or_else(|| eyre!("Client not found"))?
.base_resource_prefixes
.lock()
.clone(),
)
.ok_or_else(|| anyhow!("Resource not found"))?,
.ok_or_else(|| eyre!("Resource not found"))?,
);
let _ = node.model.set(model_arc.clone());
Ok(model_arc)

View File

@@ -5,7 +5,7 @@ use crate::{
Node,
},
};
use anyhow::{anyhow, ensure, Result};
use color_eyre::eyre::{ensure, eyre, Result};
use glam::{vec3, Mat4, Vec2};
use mint::Vector2;
use once_cell::sync::OnceCell;
@@ -63,9 +63,7 @@ impl Text {
"Internal: Node already has text attached!"
);
let client = node
.get_client()
.ok_or_else(|| anyhow!("Client not found"))?;
let client = node.get_client().ok_or_else(|| eyre!("Client not found"))?;
let text = TEXT_REGISTRY.add(Text {
space: node.spatial.get().unwrap().clone(),
font_path: font_resource_id
@@ -90,21 +88,21 @@ impl Text {
}
fn draw(&self, sk: &StereoKit, draw_ctx: &DrawContext) {
let style =
self.style
.get_or_try_init(|| -> Result<SendWrapper<TextStyle>, anyhow::Error> {
let font = self
.font_path
.as_deref()
.and_then(|path| Font::from_file(sk, path))
.unwrap_or(Font::default(sk));
Ok(SendWrapper::new(TextStyle::new(
sk,
font,
1.0,
Rgba::new(Rgb::new(1.0, 1.0, 1.0), 1.0),
)))
});
let style = self.style.get_or_try_init(
|| -> Result<SendWrapper<TextStyle>, color_eyre::eyre::Error> {
let font = self
.font_path
.as_deref()
.and_then(|path| Font::from_file(sk, path))
.unwrap_or(Font::default(sk));
Ok(SendWrapper::new(TextStyle::new(
sk,
font,
1.0,
Rgba::new(Rgb::new(1.0, 1.0, 1.0), 1.0),
)))
},
);
if let Ok(style) = style {
let data = self.data.lock();

View File

@@ -1,7 +1,7 @@
use super::{Field, FieldTrait, Node};
use crate::core::client::Client;
use crate::nodes::spatial::{find_spatial_parent, parse_transform, Spatial};
use anyhow::{ensure, Result};
use color_eyre::eyre::{ensure, Result};
use glam::{vec3, vec3a, Vec3, Vec3A};
use mint::Vector3;
use parking_lot::Mutex;

View File

@@ -1,7 +1,7 @@
use super::{Field, FieldTrait, Node};
use crate::core::client::Client;
use crate::nodes::spatial::{find_spatial_parent, parse_transform, Spatial};
use anyhow::{ensure, Result};
use color_eyre::eyre::{ensure, Result};
use glam::{swizzles::*, vec2, Vec3A};
use portable_atomic::AtomicF32;
use serde::Deserialize;

View File

@@ -12,7 +12,7 @@ use super::spatial::Spatial;
use super::Node;
use crate::core::client::Client;
use crate::nodes::spatial::find_reference_space;
use anyhow::Result;
use color_eyre::eyre::Result;
use glam::{vec2, vec3a, Vec3, Vec3A};
use mint::Vector3;
use serde::{Deserialize, Serialize};

View File

@@ -1,7 +1,7 @@
use super::{Field, FieldTrait, Node};
use crate::core::client::Client;
use crate::nodes::spatial::{find_spatial_parent, Spatial};
use anyhow::{ensure, Result};
use color_eyre::eyre::{ensure, Result};
use glam::{Mat4, Vec3A};
use mint::Vector3;
use portable_atomic::AtomicF32;

View File

@@ -1,7 +1,7 @@
use super::{Field, FieldTrait, Node};
use crate::core::client::Client;
use crate::nodes::spatial::{find_spatial_parent, parse_transform, Spatial};
use anyhow::{ensure, Result};
use color_eyre::eyre::{ensure, Result};
use glam::{swizzles::*, vec2, Vec3A};
use portable_atomic::AtomicF32;
use serde::Deserialize;

View File

@@ -13,7 +13,7 @@ use crate::core::client::Client;
use crate::core::eventloop::FRAME;
use crate::core::registry::Registry;
use crate::nodes::fields::find_field;
use anyhow::{ensure, Result};
use color_eyre::eyre::{ensure, Result};
use glam::Mat4;
use nanoid::nanoid;
use parking_lot::Mutex;

View File

@@ -4,7 +4,7 @@ use crate::nodes::fields::Field;
use crate::nodes::input::{InputMethod, InputType};
use crate::nodes::spatial::{find_spatial_parent, parse_transform, Spatial};
use crate::nodes::Node;
use anyhow::Result;
use color_eyre::eyre::Result;
use glam::{vec3a, Mat4};
use serde::Deserialize;
use stardust_xr::schemas::flat::{Datamap, InputDataType, Tip as FlatTip};

View File

@@ -10,7 +10,7 @@ use crate::{
Node,
},
};
use anyhow::{anyhow, Result};
use color_eyre::eyre::{eyre, Result};
use lazy_static::lazy_static;
use serde::Deserialize;
use stardust_xr::{
@@ -46,7 +46,7 @@ impl EnvironmentItem {
fn get_path_flex(node: &Node, _calling_client: Arc<Client>, _data: &[u8]) -> Result<Vec<u8>> {
let ItemType::Environment(environment_item) = &node.item.get().unwrap().specialization else {
return Err(anyhow!("Wrong item type?"))
return Err(eyre!("Wrong item type?"))
};
Ok(flexbuffers::singleton(environment_item.path.as_str()))
}

View File

@@ -11,7 +11,7 @@ use crate::nodes::alias::AliasInfo;
use crate::nodes::fields::find_field;
#[cfg(feature = "wayland")]
use crate::wayland::panel_item::{PanelItem, ITEM_TYPE_INFO_PANEL};
use anyhow::{anyhow, ensure, Result};
use color_eyre::eyre::{ensure, eyre, Result};
use lazy_static::lazy_static;
use nanoid::nanoid;
use parking_lot::Mutex;
@@ -372,7 +372,7 @@ fn type_info(name: &str) -> Result<&'static TypeInfo> {
"environment" => Ok(&ITEM_TYPE_INFO_ENVIRONMENT),
#[cfg(feature = "wayland")]
"panel" => Ok(&ITEM_TYPE_INFO_PANEL),
_ => Err(anyhow!("Invalid item type")),
_ => Err(eyre!("Invalid item type")),
}
}

View File

@@ -9,7 +9,7 @@ pub mod root;
pub mod spatial;
pub mod startup;
use anyhow::{anyhow, Result};
use color_eyre::eyre::{bail, eyre, Result};
use nanoid::nanoid;
use once_cell::sync::OnceCell;
use parking_lot::Mutex;
@@ -162,7 +162,7 @@ impl Node {
{
aspect_fn(self)
.get()
.ok_or_else(|| anyhow!("{} is not a {} node", node_name, aspect_type))
.ok_or_else(|| eyre!("{} is not a {} node", node_name, aspect_type))
}
pub fn send_local_signal(
@@ -185,8 +185,9 @@ impl Node {
.local_signals
.get(method)
.ok_or(ScenegraphError::SignalNotFound)?;
signal(self, calling_client, data)
.map_err(|error| ScenegraphError::SignalError { error })
signal(self, calling_client, data).map_err(|error| ScenegraphError::SignalError {
error: error.to_string(),
})
}
}
pub fn execute_local_method(
@@ -209,8 +210,9 @@ impl Node {
.local_methods
.get(method)
.ok_or(ScenegraphError::MethodNotFound)?;
method(self, calling_client, data)
.map_err(|error| ScenegraphError::MethodError { error })
method(self, calling_client, data).map_err(|error| ScenegraphError::MethodError {
error: error.to_string(),
})
}
}
pub fn send_remote_signal(&self, method: &str, data: &[u8]) -> Result<()> {
@@ -236,17 +238,15 @@ impl Node {
Ok(())
}
pub async fn execute_remote_method(&self, method: &str, data: Vec<u8>) -> Result<Vec<u8>> {
if let Some(client) = self.get_client() {
match client.message_sender_handle.as_ref() {
None => Err(anyhow!("Messenger does not exist for this node's client")),
Some(message_sender_handle) => {
message_sender_handle
.method(self.path.as_str(), method, &data)?
.await
}
}
} else {
Err(anyhow!("Client does not exist somehow?"))
}
let Some(client) = self.get_client() else {bail!("Client does not exist somehow?")};
let message_sender_handle = client
.message_sender_handle
.as_ref()
.ok_or(eyre!("Messenger does not exist for this node's client"))?;
message_sender_handle
.method(self.path.as_str(), method, &data)?
.await
.map_err(|e| eyre!(e))
}
}

View File

@@ -3,7 +3,7 @@ use super::startup::DESKTOP_STARTUP_IDS;
use super::Node;
use crate::core::client::Client;
use crate::core::registry::Registry;
use anyhow::{anyhow, Result};
use color_eyre::eyre::{eyre, Result};
use glam::Mat4;
use stardust_xr::schemas::flex::{deserialize, serialize};
@@ -39,7 +39,7 @@ impl Root {
let startup_settings = DESKTOP_STARTUP_IDS
.lock()
.remove(flexbuffers::Reader::get_root(data)?.get_str()?)
.ok_or_else(|| anyhow!("Desktop startup ID not found in the list!"))?;
.ok_or_else(|| eyre!("Desktop startup ID not found in the list!"))?;
node.spatial
.get()
.unwrap()

View File

@@ -4,7 +4,7 @@ use self::zone::{create_zone_flex, Zone};
use super::Node;
use crate::core::client::Client;
use crate::core::registry::Registry;
use anyhow::{anyhow, ensure, Result};
use color_eyre::eyre::{ensure, eyre, Result};
use glam::{vec3a, Mat4, Quat};
use mint::Vector3;
use nanoid::nanoid;
@@ -147,7 +147,7 @@ impl Spatial {
.map(|parent| self.is_ancestor_of(parent.clone()))
.unwrap_or(false);
if is_ancestor {
return Err(anyhow!("Setting spatial parent would cause a loop"));
return Err(eyre!("Setting spatial parent would cause a loop"));
}
*self.parent.lock() = parent.cloned();
@@ -160,7 +160,7 @@ impl Spatial {
.map(|parent| self.is_ancestor_of(parent.clone()))
.unwrap_or(false);
if is_ancestor {
return Err(anyhow!("Setting spatial parent would cause a loop"));
return Err(eyre!("Setting spatial parent would cause a loop"));
}
self.set_local_transform(Spatial::space_to_space_matrix(
@@ -180,7 +180,7 @@ impl Spatial {
let this_spatial = node
.spatial
.get()
.ok_or_else(|| anyhow!("Node doesn't have a spatial?"))?;
.ok_or_else(|| eyre!("Node doesn't have a spatial?"))?;
let relative_spatial = find_reference_space(&calling_client, deserialize(data)?)?;
let (scale, rotation, position) = Spatial::space_to_space_matrix(
@@ -295,7 +295,7 @@ pub fn find_spatial(
calling_client: &Arc<Client>,
node_name: &'static str,
node_path: &str,
) -> anyhow::Result<Arc<Spatial>> {
) -> color_eyre::eyre::Result<Arc<Spatial>> {
calling_client
.get_node(node_name, node_path)?
.get_aspect(node_name, "spatial", |n| &n.spatial)
@@ -304,13 +304,13 @@ pub fn find_spatial(
pub fn find_spatial_parent(
calling_client: &Arc<Client>,
node_path: &str,
) -> anyhow::Result<Arc<Spatial>> {
) -> color_eyre::eyre::Result<Arc<Spatial>> {
find_spatial(calling_client, "Spatial parent", node_path)
}
pub fn find_reference_space(
calling_client: &Arc<Client>,
node_path: &str,
) -> anyhow::Result<Arc<Spatial>> {
) -> color_eyre::eyre::Result<Arc<Spatial>> {
find_spatial(calling_client, "Reference space", node_path)
}

View File

@@ -8,7 +8,7 @@ use crate::{
Node,
},
};
use anyhow::Result;
use color_eyre::eyre::Result;
use glam::vec3a;
use parking_lot::Mutex;
use rustc_hash::FxHashMap;
@@ -81,12 +81,12 @@ impl Zone {
}
fn update(node: &Node, _calling_client: Arc<Client>, _data: &[u8]) -> Result<()> {
let zone = node.zone.get().unwrap();
let Some(field) = zone.field.upgrade() else { return Err(anyhow::anyhow!("Zone's field has been destroyed")) };
let Some(field) = zone.field.upgrade() else { return Err(color_eyre::eyre::eyre!("Zone's field has been destroyed")) };
let Some((zone_client, zone_node)) = zone
.spatial
.node
.upgrade()
.and_then(|n| n.get_client().zip(Some(n))) else { return Err(anyhow::anyhow!("No client on node?")) };
.and_then(|n| n.get_client().zip(Some(n))) else { return Err(color_eyre::eyre::eyre!("No client on node?")) };
let mut old_zoneables = zone.zoneables.lock();
for (_uid, zoneable) in old_zoneables.iter() {
zoneable.destroy();

View File

@@ -1,7 +1,7 @@
use crate::core::client::Client;
use super::Node;
use anyhow::{anyhow, Result};
use color_eyre::eyre::{eyre, Result};
use glam::Mat4;
use parking_lot::Mutex;
use rustc_hash::FxHashMap;
@@ -27,11 +27,11 @@ impl StartupSettings {
let spatial_node = calling_client
.scenegraph
.get_node(startup_id)
.ok_or_else(|| anyhow!("Root spatial node does not exist"))?;
.ok_or_else(|| eyre!("Root spatial node does not exist"))?;
let spatial = spatial_node
.spatial
.get()
.ok_or_else(|| anyhow!("Root spatial node is not a spatial"))?;
.ok_or_else(|| eyre!("Root spatial node is not a spatial"))?;
node.startup_settings.get().unwrap().lock().transform = spatial.global_transform();
Ok(())

View File

@@ -11,7 +11,7 @@ pub mod xdg_shell;
use self::{panel_item::PanelItem, state::WaylandState, surface::CORE_SURFACES};
use crate::wayland::state::ClientState;
use anyhow::{ensure, Result};
use color_eyre::eyre::{ensure, Result};
use once_cell::sync::OnceCell;
use parking_lot::Mutex;
use slog::Drain;

View File

@@ -13,7 +13,7 @@ use crate::{
Node,
},
};
use anyhow::{anyhow, bail, Result};
use color_eyre::eyre::{bail, eyre, Result};
use glam::Mat4;
use lazy_static::lazy_static;
use mint::Vector2;
@@ -127,11 +127,11 @@ impl PanelItem {
let model_node = calling_client
.scenegraph
.get_node(info.model_path)
.ok_or_else(|| anyhow!("Model node not found"))?;
.ok_or_else(|| eyre!("Model node not found"))?;
let model = model_node
.model
.get()
.ok_or_else(|| anyhow!("Node is not a model"))?;
.ok_or_else(|| eyre!("Node is not a model"))?;
if let ItemType::Panel(panel_item) = &node.item.get().unwrap().specialization {
if let Some(core_surface) = panel_item.core_surface.upgrade() {
@@ -161,11 +161,11 @@ impl PanelItem {
let model_node = calling_client
.scenegraph
.get_node(info.model_path)
.ok_or_else(|| anyhow!("Model node not found"))?;
.ok_or_else(|| eyre!("Model node not found"))?;
let model = model_node
.model
.get()
.ok_or_else(|| anyhow!("Node is not a model"))?;
.ok_or_else(|| eyre!("Node is not a model"))?;
core_surface.apply_material(model.clone(), info.idx);
@@ -347,7 +347,7 @@ impl PanelItem {
let context = xkb::Context::new(0);
let keymap =
Keymap::new_from_string(&context, deserialize(data)?, XKB_KEYMAP_FORMAT_TEXT_V1, 0)
.ok_or_else(|| anyhow!("Keymap is not valid"))?;
.ok_or_else(|| eyre!("Keymap is not valid"))?;
PanelItem::keyboard_activate_flex(node, &keymap)
}
@@ -376,7 +376,7 @@ impl PanelItem {
names.options,
XKB_KEYMAP_FORMAT_TEXT_V1,
)
.ok_or_else(|| anyhow!("Keymap is not valid"))?;
.ok_or_else(|| eyre!("Keymap is not valid"))?;
PanelItem::keyboard_activate_flex(node, &keymap)
}

View File

@@ -1,6 +1,6 @@
use super::{state::WaylandState, surface::CoreSurface, GLOBAL_DESTROY_QUEUE};
use crate::nodes::items::Item;
use anyhow::Result;
use color_eyre::eyre::Result;
use mint::Vector2;
use nanoid::nanoid;
use once_cell::sync::OnceCell;
@@ -46,12 +46,12 @@ impl KeyboardInfo {
let wl_key_state = match state {
0 => KeyState::Released,
1 => KeyState::Pressed,
_ => anyhow::bail!("Invalid key state!"),
_ => color_eyre::eyre::bail!("Invalid key state!"),
};
let xkb_key_state = match state {
0 => xkb::KeyDirection::Up,
1 => xkb::KeyDirection::Down,
_ => anyhow::bail!("Invalid key state!"),
_ => color_eyre::eyre::bail!("Invalid key state!"),
};
let state_components = self.state.update_key(key + 8, xkb_key_state);
if state_components != 0 {