Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ nostr-lmdb = { version = "0.45.0-alpha.1", path = "./database/nostr-lmdb", defau
nostr-ndb = { version = "0.45.0-alpha.1", path = "./database/nostr-ndb", default-features = false }
nostr-relay-builder = { version = "0.45.0-alpha.1", path = "./crates/nostr-relay-builder", default-features = false }
nostr-sdk = { version = "0.45.0-alpha.1", path = "./sdk", default-features = false }
opaquerr = { git = "https://github.com/shadowylab/opaquerr", rev = "1d4a95bb39d7f2fa1638d84af8540e898a3d902f" }
reqwest = { version = "0.12", default-features = false }
rusqlite = { version = "0.38", default-features = false }
serde = { version = "1.0", default-features = false }
Expand Down
1 change: 1 addition & 0 deletions crates/nostr-keyring/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ async = ["dep:async-utility"]
async-utility = { workspace = true, optional = true }
keyring = { version = "3.6", features = ["apple-native", "linux-native", "linux-native-sync-persistent", "windows-native"] } # MSRV: 1.75.0
nostr = { workspace = true, features = ["std"] }
opaquerr.workspace = true

[dev-dependencies]
tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }
Expand Down
2 changes: 1 addition & 1 deletion crates/nostr-keyring/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The crate keeps all serialization in-memory and relies on the OS-provided creden
```rust,no_run
use nostr_keyring::prelude::*;

fn main() -> Result<()> {
fn main() -> Result<(), Box<dyn std::error::Error>> {
let keyring = NostrKeyring::new("my-nostr-app");

// Save a key
Expand Down
2 changes: 1 addition & 1 deletion crates/nostr-keyring/examples/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use nostr_keyring::prelude::*;

#[tokio::main]
async fn main() -> Result<()> {
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let keys = Keys::parse("nsec1j4c6269y9w0q2er2xjw8sv2ehyrtfxq3jwgdlxj6qfn8z4gjsq5qfvfk99")?;

let keyring = NostrKeyring::new("rust-nostr-test");
Expand Down
2 changes: 1 addition & 1 deletion crates/nostr-keyring/examples/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use nostr_keyring::prelude::*;

fn main() -> Result<()> {
fn main() -> Result<(), Box<dyn std::error::Error>> {
let keys = Keys::parse("nsec1j4c6269y9w0q2er2xjw8sv2ehyrtfxq3jwgdlxj6qfn8z4gjsq5qfvfk99")?;

let keyring = NostrKeyring::new("rust-nostr-test");
Expand Down
104 changes: 104 additions & 0 deletions crates/nostr-keyring/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
//! Nostr Keyring error.

use std::{error, fmt};

#[cfg(feature = "async")]
use async_utility::tokio;

opaquerr::define_kind! {
/// Category for a [`Error`].
pub ErrorKind {
/// Nostr protocol error.
Protocol => "nostr protocol error",
/// Keyring error.
Keyring => "keyring error",
/// Anything not covered by the stable categories above.
Other => "other error",
}
}

enum Inner {
Protocol(nostr::error::Error),
Keyring(keyring::Error),
#[cfg(feature = "async")]
Join(tokio::task::JoinError),
}

impl Inner {
const fn kind(&self) -> ErrorKind {
match self {
Inner::Protocol(_) => ErrorKind::Protocol,
Inner::Keyring(_) => ErrorKind::Keyring,
#[cfg(feature = "async")]
Inner::Join(_) => ErrorKind::Other,
}
}
}

/// Nostr keyring error
pub struct Error(Inner);

impl fmt::Debug for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let kind = self.kind();

match &self.0 {
Inner::Protocol(e) => f.debug_tuple("Error").field(&kind).field(e).finish(),
Inner::Keyring(e) => f.debug_tuple("Error").field(&kind).field(e).finish(),
#[cfg(feature = "async")]
Inner::Join(e) => f.debug_tuple("Error").field(&kind).field(e).finish(),
}
}
}

impl error::Error for Error {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match &self.0 {
Inner::Protocol(e) => Some(e),
Inner::Keyring(e) => Some(e),
#[cfg(feature = "async")]
Inner::Join(e) => Some(e),
}
}
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.0 {
Inner::Protocol(e) => e.fmt(f),
Inner::Keyring(e) => e.fmt(f),
#[cfg(feature = "async")]
Inner::Join(e) => e.fmt(f),
}
}
}

impl Error {
/// Returns the error category.
#[inline]
pub const fn kind(&self) -> ErrorKind {
self.0.kind()
}
}

impl From<nostr::error::Error> for Error {
#[inline]
fn from(inner: nostr::error::Error) -> Self {
Self(Inner::Protocol(inner))
}
}

impl From<keyring::Error> for Error {
#[inline]
fn from(inner: keyring::Error) -> Self {
Self(Inner::Keyring(inner))
}
}

#[cfg(feature = "async")]
impl From<tokio::task::JoinError> for Error {
#[inline]
fn from(inner: tokio::task::JoinError) -> Self {
Self(Inner::Join(inner))
}
}
51 changes: 4 additions & 47 deletions crates/nostr-keyring/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,58 +10,15 @@
#![warn(clippy::large_futures)]
#![doc = include_str!("../README.md")]

use std::fmt;

#[cfg(feature = "async")]
use async_utility::{task, tokio};
use async_utility::task;
pub use keyring::{Entry, Error as KeyringError};
use nostr::{Keys, SecretKey, key};
use nostr::key::{Keys, SecretKey};

pub mod error;
pub mod prelude;

/// Keyring error
#[derive(Debug)]
pub enum Error {
/// Join error
#[cfg(feature = "async")]
Join(tokio::task::JoinError),
/// Keyring error
Keyring(KeyringError),
/// Nostr keys error
Keys(key::Error),
}

impl std::error::Error for Error {}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
#[cfg(feature = "async")]
Self::Join(e) => write!(f, "{e}"),
Self::Keyring(e) => write!(f, "{e}"),
Self::Keys(e) => write!(f, "{e}"),
}
}
}

#[cfg(feature = "async")]
impl From<tokio::task::JoinError> for Error {
fn from(e: tokio::task::JoinError) -> Self {
Self::Join(e)
}
}

impl From<KeyringError> for Error {
fn from(e: KeyringError) -> Self {
Self::Keyring(e)
}
}

impl From<key::Error> for Error {
fn from(e: key::Error) -> Self {
Self::Keys(e)
}
}
use self::error::Error;

/// Nostr keyring
#[derive(Debug, Clone)]
Expand Down
2 changes: 2 additions & 0 deletions crates/nostr-keyring/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

//! Prelude

#![allow(unused_imports)]
#![allow(unknown_lints)]
#![allow(ambiguous_glob_reexports)]
#![doc(hidden)]

pub use nostr::prelude::*;

pub use crate::error::{Error, ErrorKind};
pub use crate::*;
1 change: 1 addition & 0 deletions crates/nostr-relay-builder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ nostr = { workspace = true, default-features = false, features = ["std", "rand"]
nostr-database.workspace = true
nostr-memory.workspace = true
nostr-sdk.workspace = true
opaquerr = { workspace = true, features = ["alloc"] }
tokio = { workspace = true, features = ["macros", "net", "sync"] }
tracing.workspace = true

Expand Down
2 changes: 1 addition & 1 deletion crates/nostr-relay-builder/examples/hyper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl Service<Request<Incoming>> for HttpServer {
}

#[tokio::main]
async fn main() -> nostr_relay_builder::prelude::Result<()> {
async fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing_subscriber::fmt::init();

let relay = LocalRelay::new();
Expand Down
2 changes: 1 addition & 1 deletion crates/nostr-relay-builder/examples/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::time::Duration;
use nostr_relay_builder::prelude::*;

#[tokio::main]
async fn main() -> Result<()> {
async fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing_subscriber::fmt::init();

let relay = MockRelay::run().await?;
Expand Down
2 changes: 1 addition & 1 deletion crates/nostr-relay-builder/examples/policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl QueryPolicy for RejectAuthorLimit {
}

#[tokio::main]
async fn main() -> Result<()> {
async fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing_subscriber::fmt::init();

let accept_profile_data = AcceptKinds {
Expand Down
2 changes: 1 addition & 1 deletion crates/nostr-relay-builder/examples/simple_relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use nostr_lmdb::NostrLmdb;
use nostr_relay_builder::prelude::*;

#[tokio::main]
async fn main() -> Result<()> {
async fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing_subscriber::fmt::init();

let db = NostrLmdb::open("./db/nostr-lmdb").await?;
Expand Down
Loading
Loading