use crate::dup::Dup;
use crate::events::EventEmitter;
use crate::graph::Graph;
use crate::state::State;
use crate::storage::Storage;
use std::sync::Arc;
pub struct GunCore {
pub graph: Arc<Graph>,
pub state: Arc<State>,
pub events: Arc<EventEmitter>,
pub storage: Option<Arc<dyn Storage>>,
pub id_counter: Arc<std::sync::atomic::AtomicU64>,
pub dup: Arc<tokio::sync::RwLock<Dup>>, }
impl GunCore {
pub fn new() -> Self {
Self {
graph: Arc::new(Graph::new()),
state: Arc::new(State::new()),
events: Arc::new(EventEmitter::new()),
storage: None,
id_counter: Arc::new(std::sync::atomic::AtomicU64::new(0)),
dup: Arc::new(tokio::sync::RwLock::new(Dup::new_default())),
}
}
pub fn with_storage(storage: Arc<dyn Storage>) -> Self {
Self {
graph: Arc::new(Graph::new()),
state: Arc::new(State::new()),
events: Arc::new(EventEmitter::new()),
storage: Some(storage),
id_counter: Arc::new(std::sync::atomic::AtomicU64::new(0)),
dup: Arc::new(tokio::sync::RwLock::new(Dup::new_default())),
}
}
pub fn uuid(&self, length: Option<usize>) -> String {
let len = length.unwrap_or(12);
let state = self.state.next();
let state_str = format!("{:x}", state as u64).replace(".", "");
use rand::Rng;
let mut rng = rand::thread_rng();
let chars: Vec<char> = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
.chars()
.collect();
let random_part: String = (0..len)
.map(|_| {
let idx = rng.gen_range(0..chars.len());
chars[idx]
})
.collect();
format!("{}{}", state_str, random_part)
}
pub fn random_id(&self, length: usize) -> String {
use rand::Rng;
let mut rng = rand::thread_rng();
let chars: Vec<char> = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
.chars()
.collect();
(0..length)
.map(|_| {
let idx = rng.gen_range(0..chars.len());
chars[idx]
})
.collect()
}
pub fn next_chain_id(&self) -> u64 {
self.id_counter
.fetch_add(1, std::sync::atomic::Ordering::SeqCst)
}
}
impl Default for GunCore {
fn default() -> Self {
Self::new()
}
}