use jbk::creator::EntryStoreTrait;
use jubako as jbk;
use jubako::creator::schema;
use std::collections::HashMap;
use std::error::Error;
use std::sync::Arc;
const VENDOR_ID: jbk::VendorId = jbk::VendorId::new([1, 2, 3, 4]);
type PropertyName = &'static str;
type VariantName = &'static str;
type EntryType = jbk::creator::BasicEntry<PropertyName, VariantName>;
type EntryStore = jbk::creator::EntryStore<PropertyName, VariantName, EntryType>;
struct CustomEntryStore {
value_store: jbk::creator::StoreHandle,
entry_store: Box<EntryStore>,
}
impl CustomEntryStore {
fn new() -> Self {
let value_store = jbk::creator::ValueStore::new_plain(None);
let schema = schema::Schema::new(
schema::CommonProperties::new(vec![
schema::Property::new_array(0, value_store.clone(), "AString"), schema::Property::new_uint("AInteger"), ]),
vec![
(
"FirstVariant",
schema::VariantProperties::new(vec![
schema::Property::new_content_address("TheContent"), ]),
),
(
"SecondVariant",
schema::VariantProperties::new(vec![schema::Property::new_uint("AnotherInt")]),
),
],
None,
);
let entry_store = Box::new(jbk::creator::EntryStore::new(schema, None));
Self {
value_store,
entry_store,
}
}
fn add_entry(
&mut self,
variant_name: Option<VariantName>,
values: HashMap<PropertyName, jbk::Value>,
) {
let entry = EntryType::new_from_schema(&self.entry_store.schema, variant_name, values);
self.entry_store.add_entry(entry);
}
}
impl EntryStoreTrait for CustomEntryStore {
fn finalize(self: Box<Self>, directory_pack: &mut jbk::creator::DirectoryPackCreator) {
directory_pack.add_value_store(self.value_store);
let entry_store_id = directory_pack.add_entry_store(self.entry_store);
directory_pack.create_index(
"My own index", Default::default(),
0.into(), entry_store_id,
3.into(), jubako::EntryIdx::from(0).into(), );
}
}
fn main() -> Result<(), Box<dyn Error>> {
let mut creator = jbk::creator::BasicCreator::new(
"test.jbk",
jbk::creator::ConcatMode::OneFile, VENDOR_ID,
jbk::creator::Compression::default(),
Arc::new(()),
)?;
let mut entry_store = Box::new(CustomEntryStore::new());
let content: Vec<u8> = "A super content prime quality for our test container".into();
let content_address =
creator.add_content(Box::new(std::io::Cursor::new(content)), Default::default())?;
entry_store.add_entry(
Some("FirstVariant"),
HashMap::from([
("AString", jbk::Value::Array("Super".into())),
("AInteger", jbk::Value::Unsigned(50)),
("TheContent", jbk::Value::Content(content_address)),
]),
);
entry_store.add_entry(
Some("SecondVariant"),
HashMap::from([
("AString", jbk::Value::Array("Mega".into())),
("AInteger", jbk::Value::Unsigned(42)),
("AnotherInt", jbk::Value::Unsigned(5)),
]),
);
entry_store.add_entry(
Some("SecondVariant"),
HashMap::from([
("AString", jbk::Value::Array("Hyper".into())),
("AInteger", jbk::Value::Unsigned(45)),
("AnotherInt", jbk::Value::Unsigned(2)),
]),
);
Ok(creator.finalize(entry_store, vec![])?)
}