Skip to content

Commit a6c4236

Browse files
authored
Merge pull request #1 from Rafael24595/dev
[FEATURE] 0.1.0 release
2 parents fc143c3 + 1a00b8d commit a6c4236

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+2660
-823
lines changed

Cargo.lock

Lines changed: 153 additions & 43 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
[package]
2-
name = "rust-db-manager"
2+
name = "rust_db_manager_core"
33
version = "0.1.0"
44
edition = "2021"
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

88
[dependencies]
99
tokio = { version = "1", features = ["full"] }
10+
lazy_static = "1.4.0"
1011
async-trait = "0.1.80"
11-
serde_json = "1.0.115"
1212
futures-util = "0.3.30"
1313
mongodb = "2.8.2"
14-
15-
crossterm = "0.27.0"
14+
crossterm = "0.27.0"
15+
uuid = "1.8.0"
16+
cargo_metadata = "0.18.1"
17+
argon2 = "0.5.3"
18+
strum = {version = "0.26.2", features = ["derive"]}
19+
chrono = {version = "0.4.38", features = ["clock"]}
20+
serde = { version = "1.0", features = ["derive"] }
21+
serde_json = "1.0"
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
use std::{collections::HashMap, process::Command, sync::Mutex, time::{SystemTime, UNIX_EPOCH}};
2+
3+
use cargo_metadata::{CargoOpt, MetadataCommand};
4+
use lazy_static::lazy_static;
5+
use uuid::Uuid;
6+
7+
use crate::{commons::exception::connect_exception::ConnectException, infrastructure::{db_service::DBService, db_service_lite::DBServiceLite}};
8+
9+
lazy_static! {
10+
static ref INSTANCE: Mutex<Option<Configuration>> = Mutex::new(None);
11+
}
12+
13+
#[derive(Clone)]
14+
pub struct Configuration {
15+
rustc_version: String,
16+
cargo_version: String,
17+
app_name: String,
18+
app_version: String,
19+
session_id: String,
20+
timestamp: u128,
21+
services: HashMap<String, DBService>
22+
}
23+
24+
impl Configuration {
25+
26+
pub fn initialize() -> Configuration {
27+
let mut instance = INSTANCE.lock().expect("Could not lock mutex");
28+
if instance.is_some() {
29+
//TODO: Log.
30+
panic!("Configuration is already initialized.");
31+
}
32+
33+
let rustc_version = Configuration::command_rustc_version();
34+
let cargo_version = Configuration::command_cargo_version();
35+
36+
let metadata = MetadataCommand::new()
37+
.features(CargoOpt::AllFeatures)
38+
.exec()
39+
.unwrap();
40+
41+
let root: &cargo_metadata::Package = metadata.packages.iter()
42+
.find(|i| i.name == "rust_db_manager_core").unwrap();
43+
44+
let app_name = root.name.clone();
45+
let app_version = root.version.clone().to_string();
46+
47+
let session_id = Uuid::new_v4().to_string();
48+
let timestamp = SystemTime::now()
49+
.duration_since(UNIX_EPOCH)
50+
.expect("Cannot read actual date.")
51+
.as_millis();
52+
let services = HashMap::new();
53+
54+
let config = Configuration {
55+
rustc_version, cargo_version, app_name, app_version, session_id, timestamp, services
56+
};
57+
58+
*instance = Some(config);
59+
60+
return instance.as_ref().unwrap().clone();
61+
}
62+
63+
fn command_cargo_version() -> String {
64+
Configuration::command_lang_version("cargo")
65+
}
66+
67+
fn command_rustc_version() -> String {
68+
Configuration::command_lang_version("rustc")
69+
}
70+
71+
fn command_lang_version(resource: &str) -> String {
72+
let output = Command::new(resource)
73+
.arg("--version")
74+
.output()
75+
.expect("Failed to execute command");
76+
if output.status.success() {
77+
return String::from_utf8_lossy(&output.stdout).to_string();
78+
} else {
79+
//TODO: Log.
80+
panic!("Failed to get {} version", resource);
81+
}
82+
}
83+
84+
fn instance() -> Configuration {
85+
let instance = INSTANCE.lock().expect("Could not lock mutex");
86+
if instance.is_none() {
87+
//TODO: Log.
88+
panic!("Configuration is not initialized.");
89+
}
90+
91+
return instance.as_ref().unwrap().clone();
92+
}
93+
94+
pub fn rustc_version() -> String {
95+
Configuration::instance().rustc_version
96+
}
97+
98+
pub fn cargo_version() -> String {
99+
Configuration::instance().cargo_version
100+
}
101+
102+
pub fn name() -> String {
103+
Configuration::instance().app_name
104+
}
105+
106+
pub fn version() -> String {
107+
Configuration::instance().app_version
108+
}
109+
110+
pub fn session_id() -> String {
111+
Configuration::instance().session_id
112+
}
113+
114+
pub fn timestamp() -> u128 {
115+
Configuration::instance().timestamp
116+
}
117+
118+
pub fn find_services() -> Vec<DBServiceLite> {
119+
let mut instance = INSTANCE.lock().expect("Could not lock mutex");
120+
121+
let config = match instance.as_mut() {
122+
Some(config) => config,
123+
None => panic!("Configuration is not initialized."),
124+
};
125+
126+
config.services.iter().map(|s| DBServiceLite::new(s.1.name(), s.1.category())).collect()
127+
}
128+
129+
pub fn find_service(key: &str) -> Option<DBService> {
130+
let mut instance = INSTANCE.lock().expect("Could not lock mutex");
131+
132+
let config = match instance.as_mut() {
133+
Some(config) => config,
134+
None => panic!("Configuration is not initialized."),
135+
};
136+
137+
config.services.get(key).cloned()
138+
}
139+
140+
pub fn push_service(service: &DBService) -> Result<&DBService, ConnectException> {
141+
let mut instance = INSTANCE.lock().expect("Could not lock mutex");
142+
143+
let config = match instance.as_mut() {
144+
Some(config) => config,
145+
None => panic!("Configuration is not initialized."),
146+
};
147+
148+
if config.services.contains_key(&service.name()) {
149+
let exception = ConnectException::new(String::from("Service already exists."));
150+
return Err(exception);
151+
}
152+
153+
config.services.insert(service.name(), service.clone());
154+
155+
return Ok(service);
156+
}
157+
158+
pub fn put_service(service: DBService) -> DBService {
159+
let mut instance = INSTANCE.lock().expect("Could not lock mutex");
160+
161+
let config = match instance.as_mut() {
162+
Some(config) => config,
163+
None => panic!("Configuration is not initialized."),
164+
};
165+
166+
config.services.insert(service.name(), service.clone());
167+
168+
return service;
169+
}
170+
171+
pub fn remove_service(service: DBService) -> Option<DBService> {
172+
let mut instance = INSTANCE.lock().expect("Could not lock mutex");
173+
174+
let config = match instance.as_mut() {
175+
Some(config) => config,
176+
None => panic!("Configuration is not initialized."),
177+
};
178+
179+
return config.services.remove(&service.name());
180+
}
181+
182+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
use serde_json::json;
2+
3+
//TODO: Reconsider configuration logic.
4+
pub fn mongo_db() -> String {
5+
json!(
6+
{
7+
"swrelational": false,
8+
"definition": [
9+
{
10+
"order": 0,
11+
"name": "Index",
12+
"code": "INDEXED",
13+
"swsize": false,
14+
"multiple": true,
15+
"attributes": [
16+
{
17+
"name": "Unique",
18+
"code": "UNIQUE",
19+
"values": [
20+
{
21+
"key": "True",
22+
"value": "true"
23+
},
24+
{
25+
"key": "False",
26+
"value": "false"
27+
}
28+
]
29+
},
30+
{
31+
"name": "Direction",
32+
"code": "DIRECTION",
33+
"values": [
34+
{
35+
"key": "ASC",
36+
"value": "1"
37+
},
38+
{
39+
"key": "DESC",
40+
"value": "-1"
41+
}
42+
]
43+
}
44+
]
45+
}
46+
],
47+
"defaults": [
48+
{
49+
"order": 0,
50+
"code": "INDEXED",
51+
"value": "_id",
52+
"swsize": false,
53+
"size": 0,
54+
"mutable": false,
55+
"attributes": [
56+
{
57+
"key": "UNIQUE",
58+
"value": "true"
59+
},
60+
{
61+
"key": "DIRECTION",
62+
"value": "1"
63+
}
64+
],
65+
"reference": []
66+
}
67+
]
68+
}
69+
).to_string()
70+
}

src/commons/exception/connect_exception.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ pub struct ConnectException {
77
}
88

99
impl fmt::Display for ConnectException {
10+
1011
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
11-
write!(f, "CustomError: {}", self.message)
12+
write!(f, "ConnectException: {}", self.message)
1213
}
14+
1315
}
1416

1517
impl Error for ConnectException {}

0 commit comments

Comments
 (0)