attribute and json

main
Ziyang Hu 2 years ago
parent 26d1718af1
commit fff799f44e

@ -238,11 +238,11 @@ pub(crate) struct Attribute {
pub(crate) with_history: bool,
}
const LARGE_VEC_SIZE: usize = 60;
const ATTR_VEC_SIZE: usize = 80;
impl Attribute {
pub(crate) fn encode(&self) -> Encoded<LARGE_VEC_SIZE> {
let mut inner = SmallVec::<[u8; LARGE_VEC_SIZE]>::new();
pub(crate) fn encode(&self) -> Encoded<ATTR_VEC_SIZE> {
let mut inner = SmallVec::<[u8; ATTR_VEC_SIZE]>::new();
self.serialize(&mut Serializer::new(&mut inner)).unwrap();
Encoded { inner }
}
@ -250,3 +250,26 @@ impl Attribute {
Ok(rmp_serde::from_slice(data)?)
}
}
#[cfg(test)]
mod tests {
use crate::data::attr::{Attribute, AttributeCardinality, AttributeIndex, AttributeTyping};
use crate::data::id::AttrId;
use crate::data::keyword::Keyword;
#[test]
fn show_sizes() {
let attr = Attribute {
id: AttrId(0),
alias: Keyword::try_from("01234567890123456789012/01234567890123456789012").unwrap(),
cardinality: AttributeCardinality::One,
val_type: AttributeTyping::Ref,
indexing: AttributeIndex::None,
with_history: false,
};
let encoded = attr.encode();
dbg!(encoded.len());
dbg!("01234567890123456789012".as_bytes().len());
dbg!(Attribute::decode(&encoded).unwrap());
}
}

@ -1,6 +1,6 @@
use serde_derive::{Deserialize, Serialize};
use smartstring::{LazyCompact, SmartString};
use std::fmt::{Display, Formatter};
use std::fmt::{Debug, Display, Formatter};
use std::str::Utf8Error;
#[derive(Debug, thiserror::Error)]
@ -12,7 +12,7 @@ pub enum KeywordError {
Utf8(#[from] Utf8Error),
}
#[derive(Clone, PartialEq, PartialOrd, Eq, Ord, Debug, Deserialize, Serialize)]
#[derive(Clone, PartialEq, PartialOrd, Eq, Ord, Deserialize, Serialize)]
pub struct Keyword {
#[serde(rename = "n")]
pub(crate) ns: SmartString<LazyCompact>,
@ -26,6 +26,12 @@ impl Display for Keyword {
}
}
impl Debug for Keyword {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, ":{}", self)
}
}
impl TryFrom<&str> for Keyword {
type Error = KeywordError;
fn try_from(value: &str) -> Result<Self, Self::Error> {

Loading…
Cancel
Save