Initial commit

This commit is contained in:
Joe Bellus 2022-09-29 03:35:54 -04:00
commit ad203cd470
7 changed files with 175 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/target
/Cargo.lock

11
Cargo.toml Normal file
View File

@ -0,0 +1,11 @@
[package]
name = "aegis"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
indradb-lib = { version = "*", features = ["rocksdb-datastore"] }
uuid = { version = "1.1.2", features = [ "v4" ] }

51
src/error.rs Normal file
View File

@ -0,0 +1,51 @@
use std::fmt::Display;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug)]
pub enum ErrorKind {
Internal,
}
#[derive(Debug)]
pub struct Error {
kind: ErrorKind,
code: u16,
message: String,
}
impl Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.message)
}
}
impl Error {
pub fn internal(code: u16, s: &str) -> Self {
Self {
kind: ErrorKind::Internal,
code,
message: s.to_string(),
}
}
}
impl From<indradb::Error> for Error {
fn from(source: indradb::Error) -> Self {
Self {
kind: ErrorKind::Internal,
code: 0,
message: format!("{}", source),
}
}
}
impl From<indradb::ValidationError> for Error {
fn from(source: indradb::ValidationError) -> Self {
Self {
kind: ErrorKind::Internal,
code: 1,
message: format!("{}", source),
}
}
}

12
src/lib.rs Normal file
View File

@ -0,0 +1,12 @@
mod error;
mod resources;
mod server;
mod users;
pub use error::{Error, Result};
pub use server::Server;
#[cfg(test)]
mod tests {
use super::*;
}

10
src/resources.rs Normal file
View File

@ -0,0 +1,10 @@
pub struct Resource {
resource_type: String,
name: String,
external_id: Option<String>,
}
pub struct Group {
name: String,
external_id: Option<String>,
}

69
src/server.rs Normal file
View File

@ -0,0 +1,69 @@
use crate::{error::Result, users::User};
use indradb::{
Datastore, EdgeKey, Identifier, MemoryDatastore, SpecificEdgeQuery, SpecificVertexQuery,
Vertex, VertexPropertyQuery, VertexQuery, VertexQueryExt,
};
// Create an in-memory datastore
// Create a couple of vertices
pub struct Server<T>
where
T: Datastore,
{
pub datastore: T,
}
impl Server<MemoryDatastore> {
pub fn memory() -> Self {
Self {
datastore: MemoryDatastore::default(),
}
}
}
impl<T: Datastore + Clone> Server<T> {
pub fn create_user(&self, external_id: Option<String>) -> Result<User<T>> {
let v = Vertex::new(Identifier::new("user")?);
self.datastore.create_vertex(&v)?;
self.datastore.set_vertex_properties(
SpecificVertexQuery::single(v.id).property(Identifier::new("external_id")?),
external_id.clone().into(),
)?;
Ok(User {
id: v.id,
external_id,
datastore: self.datastore.clone(),
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_server() -> Result<()> {
let server = Server::memory();
let out_v = Vertex::new(Identifier::new("person")?);
let in_v = Vertex::new(Identifier::new("movie")?);
server.datastore.create_vertex(&out_v)?;
server.datastore.create_vertex(&in_v)?;
// Add an edge between the vertices
let key = EdgeKey::new(out_v.id, Identifier::new("likes")?, in_v.id);
server.datastore.create_edge(&key)?;
// Query for the edge
let e = server
.datastore
.get_edges(SpecificEdgeQuery::single(key.clone()).into())?;
assert_eq!(e.len(), 1);
assert_eq!(key, e[0].key);
Ok(())
}
}

20
src/users.rs Normal file
View File

@ -0,0 +1,20 @@
use indradb::Datastore;
use uuid::Uuid;
#[derive(Clone)]
pub struct User<T>
where
T: Datastore,
{
pub datastore: T,
pub id: Uuid,
pub external_id: Option<String>,
}
impl<T: Datastore> User<T> {
pub fn property(&self, key: String) -> String {}
}
pub struct Team {
external_id: Option<String>,
}