feat: package, package list, version

This commit is contained in:
Ahmet Kaan GÜMÜŞ 2024-11-20 00:16:31 +03:00
parent 2b0aa9bcba
commit 8900dbd6a0
9 changed files with 130 additions and 0 deletions

3
.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
/target
Cargo.lock
.vscode/

6
Cargo.toml Normal file
View file

@ -0,0 +1,6 @@
[package]
name = "rust-package-manager"
version = "0.1.0"
edition = "2021"
[dependencies]

41
src/lib.rs Normal file
View file

@ -0,0 +1,41 @@
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,
}

3
src/main.rs Normal file
View file

@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}

62
src/package.rs Normal file
View file

@ -0,0 +1,62 @@
use crate::{Package, Version};
pub mod download;
pub mod install;
pub mod validation;
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()
}
}
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,
});
}
}
}
}
None
}
}
impl ToString for Version {
fn to_string(&self) -> String {
format!("{}.{}.{}", self.first, self.second, self.third)
}
}

1
src/package/download.rs Normal file
View file

@ -0,0 +1 @@

1
src/package/install.rs Normal file
View file

@ -0,0 +1 @@

12
src/package/validation.rs Normal file
View file

@ -0,0 +1,12 @@
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()
}
}

1
src/utils.rs Normal file
View file

@ -0,0 +1 @@