use std::collections::HashMap;
use crate::lexicon::error::LexiconError;
use crate::lexicon::schema::Schema;
pub struct Catalog {
schemas: HashMap<String, Schema>,
}
impl Catalog {
pub fn new() -> Self {
Catalog {
schemas: HashMap::new(),
}
}
pub fn add_schema(&mut self, json: &[u8]) -> Result<(), LexiconError> {
let schema: Schema = serde_json::from_slice(json)?;
if schema.lexicon != 1 {
return Err(LexiconError::InvalidSchema(format!(
"unsupported lexicon version {}",
schema.lexicon
)));
}
if schema.id.is_empty() {
return Err(LexiconError::InvalidSchema("missing id".to_owned()));
}
self.schemas.insert(schema.id.clone(), schema);
Ok(())
}
pub fn get(&self, nsid: &str) -> Option<&Schema> {
self.schemas.get(nsid)
}
}
impl Default for Catalog {
fn default() -> Self {
Self::new()
}
}