use crate::manufacture::*;
use crate::object::*;
#[cfg(feature = "once_cell")]
use once_cell::sync::Lazy;
#[cfg(not(feature = "once_cell"))]
use std::sync::LazyLock;
#[cfg(feature = "once_cell")]
static SHARED_STANDARD_DATABASE: Lazy<StandardDatabase> = Lazy::new(|| StandardDatabase::new());
#[cfg(not(feature = "once_cell"))]
static SHARED_STANDARD_DATABASE: LazyLock<StandardDatabase> =
LazyLock::new(|| StandardDatabase::new());
pub struct StandardDatabase {
manufactures: Vec<Manufacture>,
objects: Vec<Object>,
}
impl StandardDatabase {
pub fn new() -> StandardDatabase {
let mut db = StandardDatabase {
manufactures: Vec::new(),
objects: Vec::new(),
};
db.init_manufactures();
db.init_objects();
db
}
#[cfg(feature = "once_cell")]
pub fn shared() -> &'static Lazy<StandardDatabase> {
&SHARED_STANDARD_DATABASE
}
#[cfg(not(feature = "once_cell"))]
pub fn shared() -> &'static LazyLock<StandardDatabase> {
&SHARED_STANDARD_DATABASE
}
pub fn add_manufacture(&mut self, man: Manufacture) -> bool {
self.manufactures.push(man);
true
}
pub fn find_manufacture(&self, code: ManufactureCode) -> Option<&Manufacture> {
for n in 0..self.manufactures.len() {
if self.manufactures[n].code() == code {
return Some(&self.manufactures[n]);
}
}
None
}
pub fn add_object(&mut self, obj: Object) -> bool {
self.objects.push(obj);
true
}
pub fn find_object(&self, code: ObjectCode) -> Option<&Object> {
let class_group_code = code & 0xFFFF00;
for n in 0..self.objects.len() {
if self.objects[n].code() == class_group_code {
return Some(&self.objects[n]);
}
}
None
}
}