...
This commit is contained in:
108
herodb/examples/circle_basic_demo.rs
Normal file
108
herodb/examples/circle_basic_demo.rs
Normal file
@@ -0,0 +1,108 @@
|
||||
// This example demonstrates the basic functionality of the circle models
|
||||
// without using the database functionality
|
||||
|
||||
use herodb::models::circle::{Circle, Member, Name, Wallet, Role, Record, RecordType};
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
println!("Circle Module Basic Demo");
|
||||
|
||||
// Create a circle
|
||||
let circle = Circle::new(
|
||||
1,
|
||||
"ThreeFold Community".to_string(),
|
||||
"A circle for ThreeFold community members".to_string(),
|
||||
);
|
||||
|
||||
println!("Created circle: {:?}", circle);
|
||||
|
||||
// Create members
|
||||
let mut alice = Member::new(
|
||||
1,
|
||||
"Alice".to_string(),
|
||||
"Core contributor".to_string(),
|
||||
Role::Admin,
|
||||
);
|
||||
alice.add_email("alice@example.com".to_string());
|
||||
|
||||
let mut bob = Member::new(
|
||||
2,
|
||||
"Bob".to_string(),
|
||||
"Community member".to_string(),
|
||||
Role::Member,
|
||||
);
|
||||
bob.add_email("bob@example.com".to_string());
|
||||
|
||||
println!("Created members: {:?}, {:?}", alice, bob);
|
||||
|
||||
// Create a domain name
|
||||
let mut domain = Name::new(
|
||||
1,
|
||||
"threefold.io".to_string(),
|
||||
"ThreeFold main domain".to_string(),
|
||||
);
|
||||
|
||||
let record = Record {
|
||||
name: "www".to_string(),
|
||||
text: "ThreeFold Website".to_string(),
|
||||
category: RecordType::A,
|
||||
addr: vec!["192.168.1.1".to_string()],
|
||||
};
|
||||
|
||||
domain.add_record(record);
|
||||
domain.add_admin("alice_pubkey".to_string());
|
||||
|
||||
println!("Created domain: {:?}", domain);
|
||||
|
||||
// Create wallets
|
||||
let mut alice_wallet = Wallet::new(
|
||||
1,
|
||||
"Alice's TFT Wallet".to_string(),
|
||||
"Main TFT wallet".to_string(),
|
||||
"Stellar".to_string(),
|
||||
"GALICE...".to_string(),
|
||||
);
|
||||
|
||||
alice_wallet.set_asset("TFT".to_string(), 1000.0);
|
||||
alice_wallet.set_asset("XLM".to_string(), 100.0);
|
||||
|
||||
let mut bob_wallet = Wallet::new(
|
||||
2,
|
||||
"Bob's TFT Wallet".to_string(),
|
||||
"Main TFT wallet".to_string(),
|
||||
"Stellar".to_string(),
|
||||
"GBOB...".to_string(),
|
||||
);
|
||||
|
||||
bob_wallet.set_asset("TFT".to_string(), 500.0);
|
||||
|
||||
println!("Created wallets: {:?}, {:?}", alice_wallet, bob_wallet);
|
||||
|
||||
// Link wallets to members
|
||||
alice.link_wallet(alice_wallet.id);
|
||||
bob.link_wallet(bob_wallet.id);
|
||||
|
||||
println!("Linked wallets to members");
|
||||
|
||||
// Demonstrate wallet operations
|
||||
println!("\nDemonstrating wallet operations:");
|
||||
|
||||
println!("Alice's wallet before transfer: {:?}", alice_wallet);
|
||||
println!("Alice's wallet total value: {}", alice_wallet.total_value());
|
||||
|
||||
println!("Bob's wallet before transfer: {:?}", bob_wallet);
|
||||
println!("Bob's wallet total value: {}", bob_wallet.total_value());
|
||||
|
||||
// Simulate a transfer of 100 TFT from Alice to Bob
|
||||
alice_wallet.set_asset("TFT".to_string(), 900.0); // Decrease Alice's TFT by 100
|
||||
bob_wallet.set_asset("TFT".to_string(), 600.0); // Increase Bob's TFT by 100
|
||||
|
||||
println!("Alice's wallet after transfer: {:?}", alice_wallet);
|
||||
println!("Alice's wallet total value: {}", alice_wallet.total_value());
|
||||
|
||||
println!("Bob's wallet after transfer: {:?}", bob_wallet);
|
||||
println!("Bob's wallet total value: {}", bob_wallet.total_value());
|
||||
|
||||
println!("\nCircle basic demo completed successfully!");
|
||||
|
||||
Ok(())
|
||||
}
|
151
herodb/examples/circle_models_demo.rs
Normal file
151
herodb/examples/circle_models_demo.rs
Normal file
@@ -0,0 +1,151 @@
|
||||
use herodb::db::{DB, DBBuilder};
|
||||
use herodb::models::circle::{Circle, Member, Name, Wallet, Asset, Role, Record, RecordType};
|
||||
use std::path::Path;
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
// Create a temporary directory for the database
|
||||
let db_path = Path::new("./tmp/circle_demo");
|
||||
if db_path.exists() {
|
||||
std::fs::remove_dir_all(db_path)?;
|
||||
}
|
||||
std::fs::create_dir_all(db_path)?;
|
||||
|
||||
println!("Creating database at {:?}", db_path);
|
||||
|
||||
// Create a database with all circle models registered
|
||||
let db = DBBuilder::new(db_path)
|
||||
.register_model::<Circle>()
|
||||
.register_model::<Member>()
|
||||
.register_model::<Name>()
|
||||
.register_model::<Wallet>()
|
||||
.build()?;
|
||||
|
||||
// Create a circle
|
||||
let mut circle = Circle::new(
|
||||
1,
|
||||
"ThreeFold Community".to_string(),
|
||||
"A circle for ThreeFold community members".to_string(),
|
||||
);
|
||||
|
||||
println!("Created circle: {:?}", circle);
|
||||
db.set(&circle)?;
|
||||
|
||||
// Create members
|
||||
let mut alice = Member::new(
|
||||
1,
|
||||
"Alice".to_string(),
|
||||
"Core contributor".to_string(),
|
||||
Role::Admin,
|
||||
);
|
||||
alice.add_email("alice@example.com".to_string());
|
||||
|
||||
let mut bob = Member::new(
|
||||
2,
|
||||
"Bob".to_string(),
|
||||
"Community member".to_string(),
|
||||
Role::Member,
|
||||
);
|
||||
bob.add_email("bob@example.com".to_string());
|
||||
|
||||
println!("Created members: {:?}, {:?}", alice, bob);
|
||||
db.set(&alice)?;
|
||||
db.set(&bob)?;
|
||||
|
||||
// Create a domain name
|
||||
let mut domain = Name::new(
|
||||
1,
|
||||
"threefold.io".to_string(),
|
||||
"ThreeFold main domain".to_string(),
|
||||
);
|
||||
|
||||
let record = Record {
|
||||
name: "www".to_string(),
|
||||
text: "ThreeFold Website".to_string(),
|
||||
category: RecordType::A,
|
||||
addr: vec!["192.168.1.1".to_string()],
|
||||
};
|
||||
|
||||
domain.add_record(record);
|
||||
domain.add_admin("alice_pubkey".to_string());
|
||||
|
||||
println!("Created domain: {:?}", domain);
|
||||
db.set(&domain)?;
|
||||
|
||||
// Create wallets
|
||||
let mut alice_wallet = Wallet::new(
|
||||
1,
|
||||
"Alice's TFT Wallet".to_string(),
|
||||
"Main TFT wallet".to_string(),
|
||||
"Stellar".to_string(),
|
||||
"GALICE...".to_string(),
|
||||
);
|
||||
|
||||
alice_wallet.set_asset("TFT".to_string(), 1000.0);
|
||||
alice_wallet.set_asset("XLM".to_string(), 100.0);
|
||||
|
||||
let mut bob_wallet = Wallet::new(
|
||||
2,
|
||||
"Bob's TFT Wallet".to_string(),
|
||||
"Main TFT wallet".to_string(),
|
||||
"Stellar".to_string(),
|
||||
"GBOB...".to_string(),
|
||||
);
|
||||
|
||||
bob_wallet.set_asset("TFT".to_string(), 500.0);
|
||||
|
||||
println!("Created wallets: {:?}, {:?}", alice_wallet, bob_wallet);
|
||||
db.set(&alice_wallet)?;
|
||||
db.set(&bob_wallet)?;
|
||||
|
||||
// Link wallets to members
|
||||
alice.link_wallet(alice_wallet.id);
|
||||
bob.link_wallet(bob_wallet.id);
|
||||
|
||||
db.set(&alice)?;
|
||||
db.set(&bob)?;
|
||||
|
||||
// Retrieve and display all data
|
||||
println!("\nRetrieving data from database:");
|
||||
|
||||
let circles = db.list_circles()?;
|
||||
println!("Circles: {:#?}", circles);
|
||||
|
||||
let members = db.list_members()?;
|
||||
println!("Members: {:#?}", members);
|
||||
|
||||
let names = db.list_names()?;
|
||||
println!("Names: {:#?}", names);
|
||||
|
||||
let wallets = db.list_wallets()?;
|
||||
println!("Wallets: {:#?}", wallets);
|
||||
|
||||
// Demonstrate wallet operations
|
||||
println!("\nDemonstrating wallet operations:");
|
||||
|
||||
let mut alice_wallet = db.get_wallet(1)?;
|
||||
println!("Alice's wallet before transfer: {:?}", alice_wallet);
|
||||
println!("Alice's wallet total value: {}", alice_wallet.total_value());
|
||||
|
||||
let mut bob_wallet = db.get_wallet(2)?;
|
||||
println!("Bob's wallet before transfer: {:?}", bob_wallet);
|
||||
println!("Bob's wallet total value: {}", bob_wallet.total_value());
|
||||
|
||||
// Simulate a transfer of 100 TFT from Alice to Bob
|
||||
alice_wallet.set_asset("TFT".to_string(), 900.0); // Decrease Alice's TFT by 100
|
||||
bob_wallet.set_asset("TFT".to_string(), 600.0); // Increase Bob's TFT by 100
|
||||
|
||||
db.set(&alice_wallet)?;
|
||||
db.set(&bob_wallet)?;
|
||||
|
||||
let alice_wallet = db.get_wallet(1)?;
|
||||
println!("Alice's wallet after transfer: {:?}", alice_wallet);
|
||||
println!("Alice's wallet total value: {}", alice_wallet.total_value());
|
||||
|
||||
let bob_wallet = db.get_wallet(2)?;
|
||||
println!("Bob's wallet after transfer: {:?}", bob_wallet);
|
||||
println!("Bob's wallet total value: {}", bob_wallet.total_value());
|
||||
|
||||
println!("\nCircle models demo completed successfully!");
|
||||
|
||||
Ok(())
|
||||
}
|
106
herodb/examples/circle_standalone.rs
Normal file
106
herodb/examples/circle_standalone.rs
Normal file
@@ -0,0 +1,106 @@
|
||||
//! This is a standalone example that demonstrates the circle models
|
||||
//! without using any database functionality.
|
||||
|
||||
use herodb::models::circle::{Circle, Member, Name, Wallet, Role, Record, RecordType};
|
||||
|
||||
fn main() {
|
||||
println!("Circle Module Standalone Demo");
|
||||
|
||||
// Create a circle
|
||||
let circle = Circle::new(
|
||||
1,
|
||||
"ThreeFold Community".to_string(),
|
||||
"A circle for ThreeFold community members".to_string(),
|
||||
);
|
||||
|
||||
println!("Created circle: {:?}", circle);
|
||||
|
||||
// Create members
|
||||
let mut alice = Member::new(
|
||||
1,
|
||||
"Alice".to_string(),
|
||||
"Core contributor".to_string(),
|
||||
Role::Admin,
|
||||
);
|
||||
alice.add_email("alice@example.com".to_string());
|
||||
|
||||
let mut bob = Member::new(
|
||||
2,
|
||||
"Bob".to_string(),
|
||||
"Community member".to_string(),
|
||||
Role::Member,
|
||||
);
|
||||
bob.add_email("bob@example.com".to_string());
|
||||
|
||||
println!("Created members: {:?}, {:?}", alice, bob);
|
||||
|
||||
// Create a domain name
|
||||
let mut domain = Name::new(
|
||||
1,
|
||||
"threefold.io".to_string(),
|
||||
"ThreeFold main domain".to_string(),
|
||||
);
|
||||
|
||||
let record = Record {
|
||||
name: "www".to_string(),
|
||||
text: "ThreeFold Website".to_string(),
|
||||
category: RecordType::A,
|
||||
addr: vec!["192.168.1.1".to_string()],
|
||||
};
|
||||
|
||||
domain.add_record(record);
|
||||
domain.add_admin("alice_pubkey".to_string());
|
||||
|
||||
println!("Created domain: {:?}", domain);
|
||||
|
||||
// Create wallets
|
||||
let mut alice_wallet = Wallet::new(
|
||||
1,
|
||||
"Alice's TFT Wallet".to_string(),
|
||||
"Main TFT wallet".to_string(),
|
||||
"Stellar".to_string(),
|
||||
"GALICE...".to_string(),
|
||||
);
|
||||
|
||||
alice_wallet.set_asset("TFT".to_string(), 1000.0);
|
||||
alice_wallet.set_asset("XLM".to_string(), 100.0);
|
||||
|
||||
let mut bob_wallet = Wallet::new(
|
||||
2,
|
||||
"Bob's TFT Wallet".to_string(),
|
||||
"Main TFT wallet".to_string(),
|
||||
"Stellar".to_string(),
|
||||
"GBOB...".to_string(),
|
||||
);
|
||||
|
||||
bob_wallet.set_asset("TFT".to_string(), 500.0);
|
||||
|
||||
println!("Created wallets: {:?}, {:?}", alice_wallet, bob_wallet);
|
||||
|
||||
// Link wallets to members
|
||||
alice.link_wallet(alice_wallet.id);
|
||||
bob.link_wallet(bob_wallet.id);
|
||||
|
||||
println!("Linked wallets to members");
|
||||
|
||||
// Demonstrate wallet operations
|
||||
println!("\nDemonstrating wallet operations:");
|
||||
|
||||
println!("Alice's wallet before transfer: {:?}", alice_wallet);
|
||||
println!("Alice's wallet total value: {}", alice_wallet.total_value());
|
||||
|
||||
println!("Bob's wallet before transfer: {:?}", bob_wallet);
|
||||
println!("Bob's wallet total value: {}", bob_wallet.total_value());
|
||||
|
||||
// Simulate a transfer of 100 TFT from Alice to Bob
|
||||
alice_wallet.set_asset("TFT".to_string(), 900.0); // Decrease Alice's TFT by 100
|
||||
bob_wallet.set_asset("TFT".to_string(), 600.0); // Increase Bob's TFT by 100
|
||||
|
||||
println!("Alice's wallet after transfer: {:?}", alice_wallet);
|
||||
println!("Alice's wallet total value: {}", alice_wallet.total_value());
|
||||
|
||||
println!("Bob's wallet after transfer: {:?}", bob_wallet);
|
||||
println!("Bob's wallet total value: {}", bob_wallet.total_value());
|
||||
|
||||
println!("\nCircle standalone demo completed successfully!");
|
||||
}
|
Reference in New Issue
Block a user