ligature/lib.rs
1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5//! This module contains Ligature's data model in Rust.
6
7#![deny(missing_docs)]
8
9use hashbag::HashBag;
10use std::collections::{BTreeMap, BTreeSet};
11use serde::{Deserialize, Serialize};
12
13/// An Element that is identified by a unique String value.
14#[derive(Debug, Clone, PartialOrd, Ord, PartialEq, Eq, Deserialize, Serialize, Hash)]
15pub struct Element(pub String);
16
17/// A single entry in a Network.
18#[derive(Debug, Clone, PartialOrd, Ord, PartialEq, Eq, Deserialize, Serialize, Hash)]
19pub enum Entry {
20 /// A relationship between two Elements.
21 Role {
22 /// The first Element.
23 first: Element,
24 /// The second Element.
25 second: Element,
26 /// The Role between the two Elements.
27 role: Element,
28 },
29 /// Represens an Element extending a Concept.
30 Extends {
31 /// The Element.
32 element: Element,
33 /// The Concept the Element extends.
34 concept: Element,
35 },
36 /// Represens an Element not extending a Concept.
37 NotExtends {
38 /// The Element.
39 element: Element,
40 /// The Concept the Element does not extend.
41 concept: Element,
42 },
43}
44
45/// A simple error message.
46pub struct LigatureError(pub String);
47
48/// A trait that defines all the actions a Ligature instance can perform.
49/// The API used for storing triples.
50pub trait Ligature {
51 /// Get all Collections.
52 fn collections(&self) -> Result<Vec<Element>, LigatureError>;
53
54 /// Add a new Dataset.
55 /// Does nothing if Dataset already exists.
56 fn add_collection(&mut self, collection: Element) -> Result<(), LigatureError>;
57
58 /// Remove a Dataset.
59 /// Does nothing if Dataset doesn't exist.
60 fn remove_collection(&mut self, collection: Element) -> Result<(), LigatureError>;
61
62 /// Get all Statements in a given Dataset.
63 fn entries(&self, collection: Element) -> Result<BTreeSet<Entry>, LigatureError>;
64
65 /// Add Statements to a given Dataset.
66 /// Returns Error if Dataset doesn't exist.
67 /// Does nothing if Statement already exists in Dataset.
68 fn add_entries(&mut self, collection: Element, entries: &mut BTreeSet<Entry>) -> Result<(), LigatureError>;
69 /// Remove Statements from a given Dataset.
70 /// Returns Error if Dataset doesn't exist.
71 /// Does nothing if Statement doesn't exist in Dataset.
72 fn remove_entries(
73 &mut self,
74 collection: Element,
75 entries: &mut BTreeSet<Entry>,
76 ) -> Result<(), LigatureError>;
77 /// Run a query against the given Dataset.
78 fn query(
79 &self,
80 collection: Element,
81 pattern: BTreeSet<Entry>,
82 ) -> Result<HashBag<BTreeMap<String, String>>, LigatureError>;
83}