From 932d07b893c61f3d10672d98cf8d3465ed9fa100 Mon Sep 17 00:00:00 2001 From: xeruf <27jf@pm.me> Date: Fri, 6 Dec 2024 22:12:05 +0100 Subject: [PATCH] test(examples): track various experiments --- .gitignore | 4 --- examples/question.rs | 22 ++++++++++++++++ examples/relay-test-0_34.rs | 24 ++++++++++++++++++ examples/relay-test.rs | 44 ++++++++++++++++++++++++++++++++ examples/rustyline.rs | 50 +++++++++++++++++++++++++++++++++++++ 5 files changed, 140 insertions(+), 4 deletions(-) create mode 100644 examples/question.rs create mode 100644 examples/relay-test-0_34.rs create mode 100644 examples/relay-test.rs create mode 100644 examples/rustyline.rs diff --git a/.gitignore b/.gitignore index 5c9c38f..3b567ed 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,3 @@ /target -/examples - /.idea -relays -keys *.html \ No newline at end of file diff --git a/examples/question.rs b/examples/question.rs new file mode 100644 index 0000000..9cce0f7 --- /dev/null +++ b/examples/question.rs @@ -0,0 +1,22 @@ +use std::collections::HashMap; + +fn main() { + let mut map: HashMap = HashMap::new(); + let add_string = |map: &mut HashMap, string: String| { + map.insert(string.len(), string); + }; + add_string(&mut map, "hi".to_string()); + add_string(&mut map, "ho".to_string()); + map.add_string("hi".to_string()); + map.add_string("ho".to_string()); + map.get(&1); +} + +trait InsertString { + fn add_string(&mut self, event: String); +} +impl InsertString for HashMap { + fn add_string(&mut self, event: String) { + self.insert(event.len(), event); + } +} diff --git a/examples/relay-test-0_34.rs b/examples/relay-test-0_34.rs new file mode 100644 index 0000000..b03e0b9 --- /dev/null +++ b/examples/relay-test-0_34.rs @@ -0,0 +1,24 @@ +use std::time::Duration; +use nostr_sdk::prelude::*; + +#[tokio::main] +async fn main() { + //tracing_subscriber::fmt::init(); + let client = Client::new(Keys::generate()); + let result = client.subscribe(vec![Filter::new()], None).await; + println!("subscribe: {:?}", result); + let result = client.add_relay("ws://localhost:4736").await; + println!("add relay: {:?}", result); + client.connect().await; + + let mut notifications = client.notifications(); + let _thread = tokio::spawn(async move { + client.send_event_builder(EventBuilder::new(Kind::TextNote, "test")).await; + tokio::time::sleep(Duration::from_secs(20)).await; + }); + while let Ok(notification) = notifications.recv().await { + if let RelayPoolNotification::Event { event, .. } = notification { + println!("At {} found {} kind {} content \"{}\"", event.created_at, event.id, event.kind, event.content); + } + } +} \ No newline at end of file diff --git a/examples/relay-test.rs b/examples/relay-test.rs new file mode 100644 index 0000000..43c57c6 --- /dev/null +++ b/examples/relay-test.rs @@ -0,0 +1,44 @@ +use nostr_sdk::prelude::*; + +#[tokio::main] +async fn main() { + //tracing_subscriber::fmt::init(); + let client = Client::new(Keys::generate()); + //let result = client.subscribe(vec![Filter::new()], None).await; + //println!("{:?}", result); + let mut notifications = client.notifications(); + + let result = client.add_relay("ws://localhost:3333").await; + println!("{:?}", result); + let result = client.connect_relay("ws://localhost:3333").await; + println!("{:?}", result); + + //let _thread = tokio::spawn(async move { + // let result = client.add_relay("ws://localhost:4736").await; + // println!("{:?}", result); + // let result = client.connect_relay("ws://localhost:4736").await; + // println!("{:?}", result); + + // // Block b + // //let result = client.add_relay("ws://localhost:54736").await; + // //println!("{:?}", result); + // //let result = client.connect_relay("ws://localhost:54736").await; + // //println!("{:?}", result); + + // tokio::time::sleep(Duration::from_secs(20)).await; + //}); + + loop { + match notifications.recv().await { + Ok(notification) => { + if let RelayPoolNotification::Event { event, .. } = notification { + println!("At {} found {} kind {} content \"{}\"", event.created_at, event.id, event.kind, event.content); + } + } + Err(e) => { + println!("Aborting due to {:?}", e); + break + } + } + } +} \ No newline at end of file diff --git a/examples/rustyline.rs b/examples/rustyline.rs new file mode 100644 index 0000000..d478799 --- /dev/null +++ b/examples/rustyline.rs @@ -0,0 +1,50 @@ +use rustyline::error::ReadlineError; +use rustyline::{Cmd, ConditionalEventHandler, DefaultEditor, Event, EventContext, EventHandler, KeyEvent, Movement, RepeatCount, Result}; + +struct CtrlCHandler; +impl ConditionalEventHandler for CtrlCHandler { + fn handle(&self, evt: &Event, n: RepeatCount, positive: bool, ctx: &EventContext) -> Option { + Some(if !ctx.line().is_empty() { + Cmd::Kill(Movement::WholeLine) + } else { + Cmd::Interrupt + }) + } +} + +fn main() -> Result<()> { + // `()` can be used when no completer is required + let mut rl = DefaultEditor::new()?; + + rl.bind_sequence( + KeyEvent::ctrl('c'), + EventHandler::Conditional(Box::from(CtrlCHandler))); + #[cfg(feature = "with-file-history")] + if rl.load_history("history.txt").is_err() { + println!("No previous history."); + } + loop { + let readline = rl.readline(">> "); + match readline { + Ok(line) => { + rl.add_history_entry(line.as_str()); + println!("Line: {}", line); + }, + Err(ReadlineError::Interrupted) => { + println!("CTRL-C"); + break + }, + Err(ReadlineError::Eof) => { + println!("CTRL-D"); + break + }, + Err(err) => { + println!("Error: {:?}", err); + break + } + } + } + #[cfg(feature = "with-file-history")] + rl.save_history("history.txt"); + Ok(()) +} \ No newline at end of file