This commit is contained in:
notplants 2022-06-01 15:31:51 +02:00
parent 003b48ed95
commit a86c2676c1
6 changed files with 1275 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

1114
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

11
Cargo.toml Normal file
View File

@ -0,0 +1,11 @@
[package]
name = "ssbbot"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
golgi = { "path" = "../peachcloud/golgi" }
futures = "0.3.21"
async-std = "1.10.0"

24
examples/basic.rs Normal file
View File

@ -0,0 +1,24 @@
use std::process;
use ssbbot::{SsbBot, Keystore};
use ssbbot::error::SsbBotError;
async fn run() -> Result<(), SsbBotError> {
println!("helloooo world");
let mut sbot = SsbBot::init(Keystore::Patchwork, None, None).await?;
sbot.listen_for_message("hello").await?;
Ok(())
}
// Enable an async main function and execute the `run()` function,
// catching any errors and printing them to `stderr` before exiting the
// process.
#[async_std::main]
async fn main() {
if let Err(e) = run().await {
eprintln!("Application error: {}", e);
process::exit(1);
}
}

67
src/error.rs Normal file
View File

@ -0,0 +1,67 @@
//! Custom error type.
use std::io::Error as IoError;
use golgi::error::GolgiError;
use std::str::Utf8Error;
/// A custom error type encapsulating all possible errors for this library.
/// `From` implementations are provided for external error types, allowing
/// the `?` operator to be used on functions which return `Result<_, GolgiError>`.
#[derive(Debug)]
pub enum SsbBotError {
/// IO error with context.
Io {
/// The underlying IO error.
source: IoError,
/// Description of the error context.
context: String,
},
/// Error within golgi library
Golgi {
/// The underlying error
source: GolgiError,
},
/// Error decoding UTF8 string from bytes
Utf8Parse {
/// The underlying parse error.
source: Utf8Error,
},
}
impl std::error::Error for SsbBotError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match *self {
SsbBotError::Io { ref source, .. } => Some(source),
SsbBotError::Golgi { ref source } => Some(source),
SsbBotError::Utf8Parse { ref source } => Some(source),
}
}
}
impl std::fmt::Display for SsbBotError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match &*self {
SsbBotError::Io { ref context, .. } => write!(f, "IO error: {}", context),
SsbBotError::Golgi { source } => write!(
f,
"Golgi encountered an error: {}",
source
),
SsbBotError::Utf8Parse { source } => {
write!(f, "Failed to deserialize UTF8 from bytes: {}", source)
}
}
}
}
impl From<Utf8Error> for SsbBotError {
fn from(err: Utf8Error) -> Self {
SsbBotError::Utf8Parse { source: err }
}
}
impl From<GolgiError> for SsbBotError {
fn from(err: GolgiError) -> Self {
SsbBotError::Golgi { source: err }
}
}

58
src/lib.rs Normal file
View File

@ -0,0 +1,58 @@
pub use golgi::sbot::{Sbot, Keystore};
use async_std::stream::StreamExt;
use futures::TryStreamExt;
use crate::error::SsbBotError;
pub mod error;
pub struct SsbBot {
sbot: Sbot
}
impl SsbBot {
pub async fn init(
keystore: Keystore,
ip_port: Option<String>,
net_id: Option<String>,
) -> Result<SsbBot, SsbBotError> {
println!("init!");
let sbot = Sbot::init(keystore, ip_port, net_id).await?;
Ok(Self {
sbot,
})
}
pub async fn listen_for_message(&mut self, msg: &str) -> Result<(), SsbBotError> {
// Create an ordered stream of all messages authored by the `author`
// identity.
let author = "@L/z54cbc8V1kL1/MiBhpEKuN3QJkSoZYNaukny3ghIs=.ed25519";
let mut history_stream = self.sbot
.create_history_stream(author.to_string())
.await?;
// Pin the stream to the stack to allow polling of the `future`.
futures::pin_mut!(history_stream);
println!("looping through stream");
// Iterate through each element in the stream and match on the `Result`.
// In this case, each element has type `Result<SsbMessageValue, GolgiError>`.
while let Some(res) = history_stream.next().await {
match res {
Ok(value) => {
// Print the `SsbMessageValue` of this element to `stdout`.
println!("value: {:?}", value);
}
Err(err) => {
// Print the `GolgiError` of this element to `stderr`.
eprintln!("err: {:?}", err);
}
}
}
Ok(())
}
}