Rust implementation of NextGraph, a Decentralized and local-first web 3.0 ecosystem https://nextgraph.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
nextgraph-rs/ng-net/src/orm.rs

125 lines
2.9 KiB

/*
* Copyright (c) 2022-2025 Niko Bonnieure, Par le Peuple, NextGraph.org developers
* All rights reserved.
* Licensed under the Apache License, Version 2.0
* <LICENSE-APACHE2 or http://www.apache.org/licenses/LICENSE-2.0>
* or the MIT license <LICENSE-MIT or http://opensource.org/licenses/MIT>,
* at your option. All files in the project carrying such
* notice may not be copied, modified, or distributed except
* according to those terms.
*/
#![allow(non_snake_case)]
use std::{collections::HashMap, sync::Arc};
use serde::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct OrmShapeType {
pub schema: OrmSchema,
pub shape: String,
}
/* == Diff Types == */
#[derive(Clone, Debug, Serialize, Deserialize)]
#[allow(non_camel_case_types)]
pub enum OrmDiffOpType {
add,
remove,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[allow(non_camel_case_types)]
pub enum OrmDiffType {
set,
object,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct OrmDiffOp {
pub op: OrmDiffOpType,
pub valType: Option<OrmDiffType>,
pub path: String,
pub value: Option<Value>, // TODO: Improve type
}
pub type OrmDiff = Vec<OrmDiffOp>;
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct OrmUpdateBlankNodeId {
pub path: String,
pub nuri: String,
}
pub type OrmUpdateBlankNodeIds = Vec<OrmUpdateBlankNodeId>;
pub type OrmSchema = HashMap<String, Arc<OrmSchemaShape>>;
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct OrmSchemaShape {
pub iri: String,
pub predicates: Vec<Arc<OrmSchemaPredicate>>,
}
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
#[allow(non_camel_case_types)]
pub enum OrmSchemaLiteralType {
number,
string,
boolean,
iri,
literal,
shape,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[serde(untagged)]
pub enum BasicType {
Bool(bool),
Num(f64),
Str(String),
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct OrmSchemaDataType {
pub valType: OrmSchemaLiteralType,
pub literals: Option<Vec<BasicType>>,
pub shape: Option<String>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct OrmSchemaPredicate {
pub dataTypes: Vec<OrmSchemaDataType>,
pub iri: String,
pub readablePredicate: String,
/// `-1` for infinity
pub maxCardinality: i32,
pub minCardinality: i32,
pub extra: Option<bool>,
}
impl Default for OrmSchemaDataType {
fn default() -> Self {
Self {
literals: None,
shape: None,
valType: OrmSchemaLiteralType::string,
}
}
}
impl Default for OrmSchemaPredicate {
fn default() -> Self {
Self {
dataTypes: Vec::new(),
iri: String::new(),
readablePredicate: String::new(),
maxCardinality: -1,
minCardinality: 0,
extra: None,
}
}
}