diff options
Diffstat (limited to 'filamento/examples')
| -rw-r--r-- | filamento/examples/example.rs | 54 | 
1 files changed, 48 insertions, 6 deletions
diff --git a/filamento/examples/example.rs b/filamento/examples/example.rs index 74a9aa1..12ede0d 100644 --- a/filamento/examples/example.rs +++ b/filamento/examples/example.rs @@ -1,17 +1,47 @@ -use std::{path::Path, str::FromStr, time::Duration}; +use std::{path::Path, str::FromStr, sync::Arc, time::Duration}; -use filamento::{Client, db::Db}; +use filamento::{Client, db::Db, files::FileStore};  use jid::JID; +use tokio::io;  use tracing::info; +#[derive(Clone)] +pub struct Files; + +impl FileStore for Files { +    type Err = Arc<io::Error>; + +    async fn is_stored(&self, name: &str) -> Result<bool, Self::Err> { +        Ok(tokio::fs::try_exists(format!("files/{}", name)) +            .await +            .map_err(|err| Arc::new(err))?) +    } + +    async fn store(&self, name: &str, data: &[u8]) -> Result<(), Self::Err> { +        Ok(tokio::fs::write(format!("files/{}", name), data) +            .await +            .map_err(|err| Arc::new(err))?) +    } + +    async fn delete(&self, name: &str) -> Result<(), Self::Err> { +        Ok(tokio::fs::remove_file(format!("files/{}", name)) +            .await +            .map_err(|err| Arc::new(err))?) +    } +} +  #[tokio::main]  async fn main() {      tracing_subscriber::fmt::init();      let db = Db::create_connect_and_migrate(Path::new("./filamento.db"))          .await          .unwrap(); -    let (client, mut recv) = -        Client::new("test@blos.sm".try_into().unwrap(), "slayed".to_string(), db); +    let (client, mut recv) = Client::new( +        "test@blos.sm".try_into().unwrap(), +        "slayed".to_string(), +        db, +        Files, +    );      tokio::spawn(async move {          while let Some(msg) = recv.recv().await { @@ -22,7 +52,10 @@ async fn main() {      client.connect().await.unwrap();      tokio::time::sleep(Duration::from_secs(5)).await;      info!("changing nick"); -    client.change_nick("britney".to_string()).await.unwrap(); +    // client +    //     .change_nick(Some("britney".to_string())) +    //     .await +    //     .unwrap();      info!("sending message");      client          .send_message( @@ -34,7 +67,16 @@ async fn main() {          .await          .unwrap();      info!("sent message"); -    tokio::time::sleep(Duration::from_secs(5)).await; +    client +        .send_message( +            JID::from_str("cel@blos.sm").unwrap(), +            filamento::chat::Body { +                body: "hallo 2".to_string(), +            }, +        ) +        .await +        .unwrap(); +    tokio::time::sleep(Duration::from_secs(15)).await;      // info!("sending disco query");      // let info = client.disco_info(None, None).await.unwrap();      // info!("got disco result: {:#?}", info);  | 
