Skip to content

Commit

Permalink
Add Common library
Browse files Browse the repository at this point in the history
  • Loading branch information
MuhammedIrfan committed May 31, 2021
1 parent 35e08b8 commit c2f781d
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[workspace]
members = [
"actions/common"
]
29 changes: 29 additions & 0 deletions actions/common/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
[package]
name = "actions-common"
version = "0.1.0"
authors = ["HugoByte <[email protected]>"]
repository = "https://github.com/hugobyte/aurras"
license = "Apache-2.0"
edition = "2018"

[dependencies]
serde_json = "1.0"
serde = "1.0"
serde_derive = "1.0"
chesterfield = "0.0.1"
2 changes: 2 additions & 0 deletions actions/common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod types;
pub use types::Context;
65 changes: 65 additions & 0 deletions actions/common/src/types/context.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use chesterfield::sync::Database;
use serde_json::{Value, Error};
use std::env;

pub struct Context {
pub host: String,
db: Database,
}


#[cfg(test)]
impl Context {
pub fn new(db: Database) -> Self {
let host = if env::var("__OW_API_HOST").is_ok() {
env::var("__OW_API_HOST").unwrap()
} else {
"host.docker.internal".to_string()
};
Context { host, db }
}

pub fn insert_document(&mut self, mut doc: Value) -> Result<String, String> {
match self.db.insert(&mut doc, None).send() {
Ok(r) => {
return Ok(r.id)
}
Err(err) => return Err(format!("error creating document {}: {:?}", doc, err)),
};
}

pub fn get_document(&self, id: &str) -> Result<Value, Error> {
match self.db.get(id).send::<Value>() {
Ok(v) => return Ok(v.into_inner().unwrap()),
Err(err) => return Err(format!("error fetching document {}: {:?}", id, err)).map_err(serde::de::Error::custom),
}
}
}

#[cfg(not(test))]
impl Context {
pub fn new(db: Database) -> Self {
let host = if env::var("__OW_API_HOST").is_ok() {
env::var("__OW_API_HOST").unwrap()
} else {
"host.docker.internal".to_string()
};
Context { host, db }
}

pub fn insert_document(&mut self, mut doc: Value) -> Result<String, String> {
match self.db.insert(&mut doc, None).send() {
Ok(r) => {
return Ok(r.id)
}
Err(err) => return Err(format!("error creating document {}: {:?}", doc, err)),
};
}

pub fn get_document(&self, id: &str) -> Result<Value, Error> {
match self.db.get(id).send::<Value>() {
Ok(v) => return Ok(v.into_inner().unwrap()),
Err(err) => return Err(format!("error fetching document {}: {:?}", id, err)).map_err(serde::de::Error::custom),
}
}
}
2 changes: 2 additions & 0 deletions actions/common/src/types/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod context;
pub use context::Context;

0 comments on commit c2f781d

Please sign in to comment.