Skip to content

Commit

Permalink
fix doctests
Browse files Browse the repository at this point in the history
  • Loading branch information
amitayh committed Feb 9, 2024
1 parent 5f7c9b8 commit dcc9f29
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 54 deletions.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ mod tests {
}

#[test]
fn lala() {
fn count_distinct_with_key() {
let mut sut = Sut::new();

// Insert data
Expand Down
28 changes: 5 additions & 23 deletions src/query/assignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ impl PartialAssignment {
/// use rustomic::query::pattern::*;
/// use rustomic::datom::*;
///
/// let query = Query::new().with(
/// let clauses = vec![
/// Clause::new()
/// .with_entity(Pattern::variable("foo"))
/// .with_attribute(Pattern::variable("bar"))
/// .with_value(Pattern::variable("baz")),
/// );
/// let mut assignment = Assignment::from_query(&query);
/// ];
/// let mut assignment = PartialAssignment::from_clauses(&clauses);
///
/// assignment.assign("foo", Value::U64(1));
/// assignment.assign("bar", Value::U64(2));
Expand All @@ -60,7 +60,7 @@ impl PartialAssignment {
///
/// let mut variables = HashSet::new();
/// variables.insert(Rc::from("?foo"));
/// let mut assignment = Assignment::new(variables);
/// let mut assignment = PartialAssignment::new(variables);
/// assert!(!assignment.is_complete());
///
/// assignment.assign("?foo", Value::U64(42));
Expand All @@ -70,24 +70,6 @@ impl PartialAssignment {
self.unassigned.is_empty()
}

pub fn satisfies(&self, query: &Query) -> bool {
query
.predicates
.iter()
.all(|predicate| predicate(&self.assigned))
}

pub fn project(mut self, query: &Query) -> Option<Vec<Value>> {
let mut result = Vec::with_capacity(query.find.len());
for find in &query.find {
if let Find::Variable(variable) = find {
let value = self.assigned.remove(variable)?;
result.push(value);
}
}
Some(result)
}

/// ```
/// use std::collections::HashSet;
/// use std::rc::Rc;
Expand All @@ -101,7 +83,7 @@ impl PartialAssignment {
/// variables.insert(Rc::from("?attribute"));
/// variables.insert(Rc::from("?value"));
/// variables.insert(Rc::from("?tx"));
/// let assignment = Assignment::new(variables);
/// let assignment = PartialAssignment::new(variables);
///
/// let clause = Clause::new()
/// .with_entity(Pattern::variable("?entity"))
Expand Down
61 changes: 34 additions & 27 deletions src/schema/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ pub enum ValueType {
Ref = 5,
}

impl ValueType {
impl TryFrom<u64> for ValueType {
type Error = InvalidTag;

/// ```
/// use rustomic::schema::attribute::ValueType;
/// use rustomic::schema::attribute::*;
///
/// let value_types = vec![
/// ValueType::I64,
Expand All @@ -25,18 +27,18 @@ impl ValueType {
/// ValueType::Ref,
/// ];
/// for value_type in value_types {
/// assert_eq!(Some(value_type), ValueType::from(value_type as u64));
/// assert_eq!(Ok(value_type), ValueType::try_from(value_type as u64));
/// }
/// assert_eq!(None, ValueType::from(42));
/// assert_eq!(Err(InvalidTag(42)), ValueType::try_from(42));
/// ```
pub fn from(value: u64) -> Option<Self> {
fn try_from(value: u64) -> Result<Self, Self::Error> {
match value {
1 => Some(Self::I64),
2 => Some(Self::U64),
3 => Some(Self::Decimal),
4 => Some(Self::Str),
5 => Some(Self::Ref),
_ => None,
1 => Ok(Self::I64),
2 => Ok(Self::U64),
3 => Ok(Self::Decimal),
4 => Ok(Self::Str),
5 => Ok(Self::Ref),
x => Err(InvalidTag(x)),
}
}
}
Expand All @@ -45,15 +47,15 @@ impl From<&Value> for ValueType {
/// ```
/// use std::rc::Rc;
/// use rustomic::datom::Value;
/// use rustomic::schema::attribute::ValueType;
/// use rustomic::schema::attribute::*;
/// use rust_decimal::prelude::*;
///
/// assert_eq!(Value::I64(42).value_type(), ValueType::I64);
/// assert_eq!(Value::U64(42).value_type(), ValueType::U64);
/// assert_eq!(Value::Decimal(42.into()).value_type(), ValueType::Decimal);
/// assert_eq!(Value::str("foo").value_type(), ValueType::Str);
/// assert_eq!(Value::Ref(42).value_type(), ValueType::Ref);
/// assert_ne!(Value::U64(42).value_type(), ValueType::Str);
/// assert_eq!(ValueType::from(&Value::I64(42)), ValueType::I64);
/// assert_eq!(ValueType::from(&Value::U64(42)), ValueType::U64);
/// assert_eq!(ValueType::from(&Value::Decimal(42.into())), ValueType::Decimal);
/// assert_eq!(ValueType::from(&Value::str("foo")), ValueType::Str);
/// assert_eq!(ValueType::from(&Value::Ref(42)), ValueType::Ref);
/// assert_ne!(ValueType::from(&Value::U64(42)), ValueType::Str);
/// ```
fn from(value: &Value) -> Self {
match value {
Expand All @@ -72,19 +74,21 @@ pub enum Cardinality {
Many = 1,
}

impl Cardinality {
impl TryFrom<u64> for Cardinality {
type Error = InvalidTag;

/// ```
/// use rustomic::schema::attribute::Cardinality;
/// use rustomic::schema::attribute::*;
///
/// assert_eq!(Some(Cardinality::One), Cardinality::from(0));
/// assert_eq!(Some(Cardinality::Many), Cardinality::from(1));
/// assert_eq!(None, Cardinality::from(42));
/// assert_eq!(Ok(Cardinality::One), Cardinality::try_from(0));
/// assert_eq!(Ok(Cardinality::Many), Cardinality::try_from(1));
/// assert_eq!(Err(InvalidTag(42)), Cardinality::try_from(42));
/// ```
pub fn from(value: u64) -> Option<Self> {
fn try_from(value: u64) -> Result<Self, Self::Error> {
match value {
0 => Some(Self::One),
1 => Some(Self::Many),
_ => None,
0 => Ok(Self::One),
1 => Ok(Self::Many),
x => Err(InvalidTag(x)),
}
}
}
Expand Down Expand Up @@ -147,3 +151,6 @@ impl From<AttributeDefinition> for tx::EntityOperation {
operation
}
}

#[derive(PartialEq, Debug, Clone, Copy)]
pub struct InvalidTag(pub u64);
4 changes: 2 additions & 2 deletions src/storage/attribute_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,12 @@ impl Builder {
attribute: DB_ATTR_TYPE_ID,
value: Value::U64(value_type),
..
} => self.value_type = ValueType::from(value_type),
} => self.value_type = ValueType::try_from(value_type).ok(),
Datom {
attribute: DB_ATTR_CARDINALITY_ID,
value: Value::U64(cardinality),
..
} => self.cardinality = Cardinality::from(cardinality),
} => self.cardinality = Cardinality::try_from(cardinality).ok(),
Datom {
attribute: DB_ATTR_DOC_ID,
value: Value::Str(doc),
Expand Down
2 changes: 1 addition & 1 deletion src/tx/transactor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl Transactor {
AttributeValue::TempId(temp_id) => temp_ids.get(&temp_id).map(Value::Ref),
}?;

if attribute.definition.value_type != (&value).into() {
if attribute.definition.value_type != ValueType::from(&value) {
// Value type is incompatible with attribute, reject transaction.
return Err(TransactionError::InvalidAttributeType);
}
Expand Down

0 comments on commit dcc9f29

Please sign in to comment.