improve simple example

This commit is contained in:
dignifiedquire
2020-05-24 21:24:21 +02:00
parent 9442df0cf8
commit 00e8f2271a
2 changed files with 26 additions and 23 deletions

View File

@@ -82,6 +82,7 @@ members = [
[[example]] [[example]]
name = "simple" name = "simple"
path = "examples/simple.rs" path = "examples/simple.rs"
required-features = ["repl"]
[[example]] [[example]]
name = "repl" name = "repl"

View File

@@ -1,6 +1,3 @@
extern crate deltachat;
use std::time;
use tempfile::tempdir; use tempfile::tempdir;
use deltachat::chat; use deltachat::chat;
@@ -11,32 +8,38 @@ use deltachat::context::*;
use deltachat::Event; use deltachat::Event;
fn cb(event: Event) { fn cb(event: Event) {
print!("[{:?}]", event);
match event { match event {
Event::ConfigureProgress(progress) => { Event::ConfigureProgress(progress) => {
println!(" progress: {}", progress); log::info!("progress: {}", progress);
} }
Event::Info(msg) | Event::Warning(msg) | Event::Error(msg) | Event::ErrorNetwork(msg) => { Event::Info(msg) => {
println!(" {}", msg); log::info!("{}", msg);
} }
_ => { Event::Warning(msg) => {
println!(); log::warn!("{}", msg);
}
Event::Error(msg) | Event::ErrorNetwork(msg) => {
log::error!("{}", msg);
}
event => {
log::info!("{:?}", event);
} }
} }
} }
/// Run with `RUST_LOG=simple=info cargo run --release --example simple --features repl -- email pw`.
#[async_std::main] #[async_std::main]
async fn main() { async fn main() {
pretty_env_logger::try_init_timed().ok();
let dir = tempdir().unwrap(); let dir = tempdir().unwrap();
let dbfile = dir.path().join("db.sqlite"); let dbfile = dir.path().join("db.sqlite");
println!("creating database {:?}", dbfile); log::info!("creating database {:?}", dbfile);
let ctx = Context::new("FakeOs".into(), dbfile.into()) let ctx = Context::new("FakeOs".into(), dbfile.into())
.await .await
.expect("Failed to create context"); .expect("Failed to create context");
let info = ctx.get_info().await; let info = ctx.get_info().await;
let duration = time::Duration::from_millis(4000); log::info!("info: {:#?}", info);
println!("info: {:#?}", info);
let events = ctx.get_event_emitter(); let events = ctx.get_event_emitter();
let events_spawn = async_std::task::spawn(async move { let events_spawn = async_std::task::spawn(async move {
@@ -45,7 +48,7 @@ async fn main() {
} }
}); });
println!("configuring"); log::info!("configuring");
let args = std::env::args().collect::<Vec<String>>(); let args = std::env::args().collect::<Vec<String>>();
assert_eq!(args.len(), 3, "requires email password"); assert_eq!(args.len(), 3, "requires email password");
let email = args[1].clone(); let email = args[1].clone();
@@ -59,9 +62,9 @@ async fn main() {
ctx.configure().await.unwrap(); ctx.configure().await.unwrap();
println!("------ RUN ------"); log::info!("------ RUN ------");
ctx.clone().start_io().await; ctx.start_io().await;
println!("--- SENDING A MESSAGE ---"); log::info!("--- SENDING A MESSAGE ---");
let contact_id = Contact::create(&ctx, "dignifiedquire", "dignifiedquire@gmail.com") let contact_id = Contact::create(&ctx, "dignifiedquire", "dignifiedquire@gmail.com")
.await .await
@@ -74,20 +77,19 @@ async fn main() {
.unwrap(); .unwrap();
} }
println!("fetching chats.."); log::info!("fetching chats..");
let chats = Chatlist::try_load(&ctx, 0, None, None).await.unwrap(); let chats = Chatlist::try_load(&ctx, 0, None, None).await.unwrap();
for i in 0..chats.len() { for i in 0..chats.len() {
let summary = chats.get_summary(&ctx, 0, None).await; let summary = chats.get_summary(&ctx, 0, None).await;
let text1 = summary.get_text1(); let text1 = summary.get_text1();
let text2 = summary.get_text2(); let text2 = summary.get_text2();
println!("chat: {} - {:?} - {:?}", i, text1, text2,); log::info!("chat: {} - {:?} - {:?}", i, text1, text2,);
} }
async_std::task::sleep(duration).await; log::info!("stopping");
println!("stopping");
ctx.stop_io().await; ctx.stop_io().await;
println!("closing"); log::info!("closing");
drop(ctx);
events_spawn.await; events_spawn.await;
} }