Can now connect to Omikron :C

Some util
This commit is contained in:
Alex Emmet 2025-08-31 14:44:56 +02:00
commit 8637be0a08
9 changed files with 391 additions and 78 deletions

View file

@ -1,5 +1,5 @@
use json::{self, Value};
use json::{self, array, object, JsonValue};
use std::fs::{self, File};
use std::io::{Read, Write};
use std::path::Path;

View file

@ -1,11 +1,14 @@
use json::{self, Value};
use std::string::String;
use json::{self, array, JsonValue};
use std::fs::{self, File};
use std::io::{Read, Write};
use std::path::Path;
use axum::Json;
use uuid::Uuid;
use crate::users::Contact::Contact; // assuming you have a Contact struct in a module
use crate::users::contact;
use crate::users::contact::Contact;
// assuming you have a Contact struct in a module
pub struct ChatsUtil;
@ -40,8 +43,8 @@ impl ChatsUtil {
};
for i in 0..contacts.len() {
if contacts[i]["userID"].as_str() == Some(&contact.user_id.to_string()) {
contacts.remove(i);
if contacts[i]["userID"].as_str() == Some(&contact.user_id.unwrap().to_string()) {
contacts.remove(stringify!("{}", i));
break;
}
}
@ -62,7 +65,7 @@ impl ChatsUtil {
for i in 0..contacts.len() {
if let Some(uid) = contacts[i]["userID"].as_str() {
if Uuid::parse_str(uid).ok()? == user_id {
return Contact::from_json(&contacts[i]);
return Option::from(Contact::from_json(&contacts[i]));
}
}
}
@ -70,7 +73,7 @@ impl ChatsUtil {
None
}
pub fn get_users(storage_owner: Uuid) -> Value {
pub fn get_users(storage_owner: Uuid) -> JsonValue {
let dir = format!("/users/{}/contacts/", storage_owner);
let file_name = "contacts.json";
let s = Self::load_file(&dir, file_name);
@ -79,9 +82,8 @@ impl ChatsUtil {
if !s.is_empty() {
if let Ok(contacts) = json::parse(&s) {
for i in 0..contacts.len() {
if let Some(c) = Contact::from_json(&contacts[i]) {
contacts_out.push(c.info()).unwrap();
}
let c = Contact::from_json(&contacts[i]);
contacts_out.push(c.to_json()).unwrap();
}
}
}

View file

@ -3,8 +3,6 @@ use std::fs::{self, File};
use std::io::{Read, Write};
use std::path::Path;
use crate::files::Files;
pub struct ConfigUtil {
pub config: JsonValue,
pub unique: bool,
@ -37,13 +35,12 @@ impl ConfigUtil {
}
pub fn load(&mut self) {
let s = Self::load_file(Files::MAIN);
let s = Self::load_file("config.json");
if !s.is_empty() {
self.config = json::parse(&s).unwrap_or(JsonValue::new_object());
}
if self.config.has("port") {
if let Some(port) = self.config["port"].as_i32() {
// Set community manager port
if self.config.has_key("ssl_port") {
if let Some(port) = self.config["ssl_port"].as_i32() {
}
}
}
@ -60,6 +57,6 @@ impl ConfigUtil {
}
pub fn save(&self) -> std::io::Result<()> {
Self::save_file(Files::MAIN, &self.config.dump())
Self::save_file("config.json", &self.config.dump())
}
}

View file

@ -1,3 +1,4 @@
use sysinfo::{System};
use std::fs::{self, File};
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
@ -142,21 +143,14 @@ impl FileUtil {
}
pub fn get_used_ram() -> String {
// Rust has no direct Runtime like Java.
// As a placeholder, read process memory usage from sysinfo.
use sysinfo::{System, SystemExt, ProcessExt};
let mut sys = System::new_all();
sys.refresh_all();
if let Some(process) = sys.process(process::id() as i32) {
let used = process.memory() * 1024; // kB to bytes
let total = sys.total_memory() * 1024;
return format!(
"{}/{}",
Self::design_byte(used),
Self::design_byte(total)
);
}
"Unknown".to_string()
let used = sys.used_memory() * 1024; // kB to bytes
let total = sys.total_memory() * 1024;
format!(
"{}/{}",
Self::design_byte(used),
Self::design_byte(total)
)
}
}