mirror of
https://github.com/logos-messaging/libchat.git
synced 2026-05-02 16:23:06 +00:00
chore: use temp file
This commit is contained in:
parent
836cc4bdc4
commit
a14079807e
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -512,6 +512,7 @@ dependencies = [
|
|||||||
"rand_core",
|
"rand_core",
|
||||||
"safer-ffi",
|
"safer-ffi",
|
||||||
"storage",
|
"storage",
|
||||||
|
"tempfile",
|
||||||
"thiserror",
|
"thiserror",
|
||||||
"x25519-dalek",
|
"x25519-dalek",
|
||||||
]
|
]
|
||||||
|
|||||||
@ -18,3 +18,6 @@ safer-ffi = "0.1.13"
|
|||||||
storage = { path = "../storage" }
|
storage = { path = "../storage" }
|
||||||
thiserror = "2.0.17"
|
thiserror = "2.0.17"
|
||||||
x25519-dalek = { version = "2.0.1", features = ["static_secrets", "reusable_secrets", "getrandom"] }
|
x25519-dalek = { version = "2.0.1", features = ["static_secrets", "reusable_secrets", "getrandom"] }
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
tempfile = "3"
|
||||||
|
|||||||
@ -6,32 +6,32 @@
|
|||||||
//! Run with: cargo run -p logos-chat --example chat_session
|
//! Run with: cargo run -p logos-chat --example chat_session
|
||||||
|
|
||||||
use logos_chat::storage::ChatSession;
|
use logos_chat::storage::ChatSession;
|
||||||
|
use tempfile::TempDir;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("=== Chat Session Example ===\n");
|
println!("=== Chat Session Example ===\n");
|
||||||
|
|
||||||
// Use a temporary file for this example
|
// Create temporary directories for databases
|
||||||
let alice_db = "/tmp/alice_session.db";
|
let alice_dir = TempDir::new().expect("Failed to create temp dir");
|
||||||
let bob_db = "/tmp/bob_session.db";
|
let bob_dir = TempDir::new().expect("Failed to create temp dir");
|
||||||
|
|
||||||
// Clean up from previous runs
|
let alice_db = alice_dir.path().join("alice.db");
|
||||||
let _ = std::fs::remove_file(alice_db);
|
let bob_db = bob_dir.path().join("bob.db");
|
||||||
let _ = std::fs::remove_file(bob_db);
|
|
||||||
|
|
||||||
// =========================================
|
// =========================================
|
||||||
// Create sessions for Alice and Bob
|
// Create sessions for Alice and Bob
|
||||||
// =========================================
|
// =========================================
|
||||||
println!("Step 1: Creating chat sessions...\n");
|
println!("Step 1: Creating chat sessions...\n");
|
||||||
|
|
||||||
let mut alice = ChatSession::open_or_create(alice_db, "alice_secret_key")
|
let mut alice = ChatSession::open_or_create(alice_db.to_str().unwrap(), "alice_secret_key")
|
||||||
.expect("Failed to create Alice's session");
|
.expect("Failed to create Alice's session");
|
||||||
println!(" Alice's session created");
|
println!(" Alice's session created");
|
||||||
println!(" Address: {}", truncate(&alice.local_address()));
|
println!(" Address: {}", &alice.local_address());
|
||||||
|
|
||||||
let mut bob = ChatSession::open_or_create(bob_db, "bob_secret_key")
|
let mut bob = ChatSession::open_or_create(bob_db.to_str().unwrap(), "bob_secret_key")
|
||||||
.expect("Failed to create Bob's session");
|
.expect("Failed to create Bob's session");
|
||||||
println!(" Bob's session created");
|
println!(" Bob's session created");
|
||||||
println!(" Address: {}", truncate(&bob.local_address()));
|
println!(" Address: {}", &bob.local_address());
|
||||||
println!();
|
println!();
|
||||||
|
|
||||||
// =========================================
|
// =========================================
|
||||||
@ -44,7 +44,7 @@ fn main() {
|
|||||||
println!(" Bob's intro bundle created");
|
println!(" Bob's intro bundle created");
|
||||||
println!(
|
println!(
|
||||||
" Installation key: {}",
|
" Installation key: {}",
|
||||||
truncate(&hex::encode(bob_intro.installation_key.as_bytes()))
|
&hex::encode(bob_intro.installation_key.as_bytes())
|
||||||
);
|
);
|
||||||
println!();
|
println!();
|
||||||
|
|
||||||
@ -106,11 +106,11 @@ fn main() {
|
|||||||
|
|
||||||
println!(" Session closed. Reopening...\n");
|
println!(" Session closed. Reopening...\n");
|
||||||
|
|
||||||
let alice_restored =
|
let alice_restored = ChatSession::open(alice_db.to_str().unwrap(), "alice_secret_key")
|
||||||
ChatSession::open(alice_db, "alice_secret_key").expect("Failed to reopen Alice's session");
|
.expect("Failed to reopen Alice's session");
|
||||||
|
|
||||||
println!(" Session restored!");
|
println!(" Session restored!");
|
||||||
println!(" Address: {}", truncate(&alice_restored.local_address()));
|
println!(" Address: {}", &alice_restored.local_address());
|
||||||
|
|
||||||
// Note: The chats list will be empty because we haven't implemented
|
// Note: The chats list will be empty because we haven't implemented
|
||||||
// full chat restoration yet (which requires restoring ratchet states)
|
// full chat restoration yet (which requires restoring ratchet states)
|
||||||
@ -135,15 +135,5 @@ fn main() {
|
|||||||
println!(" - Ratchet state persistence (integration with double-ratchets storage)");
|
println!(" - Ratchet state persistence (integration with double-ratchets storage)");
|
||||||
println!(" - Complete chat restoration on session open");
|
println!(" - Complete chat restoration on session open");
|
||||||
|
|
||||||
// Clean up
|
// Temp directories are automatically cleaned up when dropped
|
||||||
let _ = std::fs::remove_file(alice_db);
|
|
||||||
let _ = std::fs::remove_file(bob_db);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn truncate(s: &str) -> String {
|
|
||||||
if s.len() > 16 {
|
|
||||||
format!("{}...{}", &s[..8], &s[s.len() - 8..])
|
|
||||||
} else {
|
|
||||||
s.to_string()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user