use uuid::Uuid;
use super::*;
use crate::Error;
#[derive(Debug, Clone)]
pub struct Index {
pub uuid: Uuid,
pub fields: Vec<i32>,
pub name: String,
pub dataset_version: u64,
}
impl Index {
pub fn new(uuid: Uuid, name: &str, fields: &[i32], dataset_version: u64) -> Self {
Self {
uuid,
name: name.to_string(),
fields: Vec::from(fields),
dataset_version,
}
}
}
impl TryFrom<&pb::IndexMetadata> for Index {
type Error = Error;
fn try_from(proto: &pb::IndexMetadata) -> Result<Self> {
Ok(Self {
uuid: proto
.uuid
.as_ref()
.map(Uuid::try_from)
.ok_or_else(|| Error::IO {
message: "uuid field does not exist in Index metadata".to_string(),
})??,
name: proto.name.clone(),
fields: proto.fields.clone(),
dataset_version: proto.dataset_version,
})
}
}
impl From<&Index> for pb::IndexMetadata {
fn from(idx: &Index) -> Self {
Self {
uuid: Some((&idx.uuid).into()),
name: idx.name.clone(),
fields: idx.fields.clone(),
dataset_version: idx.dataset_version,
}
}
}
impl From<&Vec<Index>> for pb::IndexSection {
fn from(indices: &Vec<Index>) -> Self {
Self {
indices: indices.iter().map(pb::IndexMetadata::from).collect(),
}
}
}