refactor: ♻️ package
This commit is contained in:
parent
ecc8781a4d
commit
2b15169bc1
6 changed files with 59 additions and 103 deletions
|
@ -7,5 +7,7 @@ edition = "2021"
|
|||
axum = "0.7.7"
|
||||
serde = { version = "1.0.210", features = ["derive"] }
|
||||
serde_json = "1.0.128"
|
||||
sha3 = "0.10.8"
|
||||
surrealdb = "2.0.4"
|
||||
tokio = { version = "1.40.0", features = ["full"] }
|
||||
tower-http = "0.6.1"
|
||||
|
|
45
src/lib.rs
45
src/lib.rs
|
@ -1,41 +1,4 @@
|
|||
pub mod package;
|
||||
pub mod utils;
|
||||
|
||||
pub struct PackageList {
|
||||
list: Vec<Package>,
|
||||
}
|
||||
|
||||
impl PackageList {
|
||||
pub fn new() -> Self {
|
||||
PackageList { list: vec![] }
|
||||
}
|
||||
|
||||
pub fn get_list(&self) -> Vec<&Package> {
|
||||
let mut list = vec![];
|
||||
self.list.iter().for_each(|package| list.push(package));
|
||||
list
|
||||
}
|
||||
|
||||
pub fn insert(&mut self, package: Package) {
|
||||
self.list.push(package);
|
||||
self.sort();
|
||||
}
|
||||
|
||||
fn sort(&mut self) {
|
||||
self.list.sort_by(|x,y|x.name.cmp(&y.name));
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Package {
|
||||
name: String,
|
||||
version: Version,
|
||||
location: String,
|
||||
size: usize,
|
||||
hash: String,
|
||||
}
|
||||
|
||||
struct Version {
|
||||
first: usize,
|
||||
second: usize,
|
||||
third: usize,
|
||||
}
|
||||
pub(crate) mod database;
|
||||
pub(crate) mod http;
|
||||
pub(crate) mod package;
|
||||
pub(crate) mod utils;
|
||||
|
|
103
src/package.rs
103
src/package.rs
|
@ -1,62 +1,65 @@
|
|||
use crate::{Package, Version};
|
||||
use std::fmt::Display;
|
||||
|
||||
pub mod download;
|
||||
pub mod install;
|
||||
pub mod validation;
|
||||
use sha3::Sha3_512;
|
||||
use surrealdb::Datetime;
|
||||
|
||||
impl Package {
|
||||
pub fn new(name: String, version: String, location: String) -> Package {
|
||||
let version = Version::new(version).unwrap();
|
||||
let hash = Package::calculate_hash(location.to_string());
|
||||
Package {
|
||||
name,
|
||||
version,
|
||||
location,
|
||||
size: 0,
|
||||
hash,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_name(&self) -> String {
|
||||
self.name.to_string()
|
||||
}
|
||||
pub fn get_version(&self) -> String {
|
||||
self.version.to_string()
|
||||
}
|
||||
pub fn get_location(&self) -> String {
|
||||
self.location.to_string()
|
||||
}
|
||||
pub fn get_size(&self) -> String {
|
||||
self.size.to_string()
|
||||
}
|
||||
pub fn get_hash(&self) -> String {
|
||||
self.hash.to_string()
|
||||
}
|
||||
pub(crate) struct Package {
|
||||
name: String,
|
||||
author: Publisher,
|
||||
version: Version,
|
||||
size: u64,
|
||||
publish_date_time: Datetime,
|
||||
last_update_date_time: Datetime,
|
||||
hash: Sha3_512,
|
||||
location: String,
|
||||
}
|
||||
|
||||
pub(crate) struct Version {
|
||||
first: u8,
|
||||
second: u8,
|
||||
third: u8,
|
||||
}
|
||||
|
||||
impl Version {
|
||||
fn new(version: String) -> Option<Self> {
|
||||
let splitted_input = version.split('.').collect::<Vec<&str>>();
|
||||
if splitted_input.len() == 3 {
|
||||
if let Ok(first) = splitted_input[0].parse::<usize>() {
|
||||
if let Ok(second) = splitted_input[1].parse::<usize>() {
|
||||
if let Ok(third) = splitted_input[2].parse::<usize>() {
|
||||
return Some(Version {
|
||||
first,
|
||||
second,
|
||||
third,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
fn new(first:u8, second:u8, third: u8) -> Self {
|
||||
Version {
|
||||
first,
|
||||
second,
|
||||
third,
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn update(&mut self, first:u8, second:u8, third: u8) -> &Self {
|
||||
self.first = first;
|
||||
self.second = second;
|
||||
self.third = third;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl ToString for Version {
|
||||
fn to_string(&self) -> String {
|
||||
format!("{}.{}.{}", self.first, self.second, self.third)
|
||||
impl Display for Version {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{}.{}.{}", self.first, self.second, self.third)
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct Publisher {
|
||||
name: String,
|
||||
}
|
||||
|
||||
impl Publisher {
|
||||
fn new(name: String) -> Self {
|
||||
Publisher {
|
||||
name,
|
||||
}
|
||||
}
|
||||
|
||||
fn get_name(&self) -> String {
|
||||
self.name.clone()
|
||||
}
|
||||
|
||||
fn update(&mut self, name: String) -> &Self {
|
||||
self.name = name;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,12 +0,0 @@
|
|||
use std::{fs::File, io::Read};
|
||||
|
||||
use crate::Package;
|
||||
|
||||
impl Package {
|
||||
pub(crate) fn calculate_hash(location: String) -> String {
|
||||
let mut file = File::open(location).unwrap();
|
||||
let mut buf = vec![];
|
||||
file.read_to_end(&mut buf).unwrap();
|
||||
String::new()
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue