From c2f781d3c0bc57675678c51126725ac92b5fbe99 Mon Sep 17 00:00:00 2001 From: Muhammed Irfan K Date: Mon, 31 May 2021 09:57:08 +0530 Subject: [PATCH] Add Common library --- Cargo.toml | 4 ++ actions/common/Cargo.toml | 29 +++++++++++++ actions/common/src/lib.rs | 2 + actions/common/src/types/context.rs | 65 +++++++++++++++++++++++++++++ actions/common/src/types/mod.rs | 2 + 5 files changed, 102 insertions(+) create mode 100644 Cargo.toml create mode 100644 actions/common/Cargo.toml create mode 100644 actions/common/src/lib.rs create mode 100644 actions/common/src/types/context.rs create mode 100644 actions/common/src/types/mod.rs diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 00000000..225c3b69 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,4 @@ +[workspace] +members = [ + "actions/common" +] \ No newline at end of file diff --git a/actions/common/Cargo.toml b/actions/common/Cargo.toml new file mode 100644 index 00000000..59cb89fe --- /dev/null +++ b/actions/common/Cargo.toml @@ -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 "] +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" \ No newline at end of file diff --git a/actions/common/src/lib.rs b/actions/common/src/lib.rs new file mode 100644 index 00000000..ae3626f8 --- /dev/null +++ b/actions/common/src/lib.rs @@ -0,0 +1,2 @@ +mod types; +pub use types::Context; \ No newline at end of file diff --git a/actions/common/src/types/context.rs b/actions/common/src/types/context.rs new file mode 100644 index 00000000..91e1e3bf --- /dev/null +++ b/actions/common/src/types/context.rs @@ -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 { + 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 { + match self.db.get(id).send::() { + 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 { + 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 { + match self.db.get(id).send::() { + Ok(v) => return Ok(v.into_inner().unwrap()), + Err(err) => return Err(format!("error fetching document {}: {:?}", id, err)).map_err(serde::de::Error::custom), + } + } +} diff --git a/actions/common/src/types/mod.rs b/actions/common/src/types/mod.rs new file mode 100644 index 00000000..b7c5344c --- /dev/null +++ b/actions/common/src/types/mod.rs @@ -0,0 +1,2 @@ +mod context; +pub use context::Context;