Completed 2/3 of methods added in previous commit; Added some tests;

This commit is contained in:
Nathan Fisher 2023-03-31 19:08:53 -04:00
parent 4d0dfc1df4
commit 26be99f408
4 changed files with 102 additions and 31 deletions

18
.cargo/config.toml Normal file
View File

@ -0,0 +1,18 @@
[target.aarch64-unknown-linux-musl]
linker = "aarch64-hitchhiker-linux-musleabihf-gcc"
[target.riscv64gc-unknown-linux-musl]
rustflags = [
"-C", "target-feature=-crt-static"
]
linker = "riscv64-hitchhiker-linux-musl-gcc"
ar = "riscv64-hitchhiker-linux-musl-ar"
[target.i686-unknown-linux-musl]
linker = "i686-hitchhiker-linux-musl-gcc"
[target.i586-unknown-linux-musl]
linker = "i586-hitchhiker-linux-musl-gcc"
[target.x86_64-unknown-linux-musl]
linker = "x86_64-hitchhiker-linux-musl-gcc"

View File

@ -1,10 +1,11 @@
use std::{fmt, path::PathBuf};
use ron::ser::PrettyConfig;
use {
crate::{Package, Repository, Version},
serde::{Deserialize, Serialize},
std::{collections::HashMap, error::Error},
std::{collections::HashMap, error::Error, fmt, fs::{self, File}, path::PathBuf},
url::Url,
zstd::{Decoder, Encoder},
};
#[derive(Clone, Debug)]
@ -92,15 +93,35 @@ impl Database {
Ok(updates)
}
pub fn from_file(prefix: Option<PathBuf>) -> Result<File, Box<dyne Error>> {
unimplemented!();
pub fn from_file(prefix: Option<PathBuf>) -> Result<Self, Box<dyn Error>> {
let db = crate::get_db(prefix);
if let Some(dir) = db.parent() {
if !dir.exists() {
fs::create_dir_all(dir)?;
}
}
let fd = File::open(db)?;
let reader = Decoder::new(fd)?;
let res = ron::de::from_reader(reader)?;
Ok(res)
}
pub fn save_to_file(prefix: Option<PathBuf>) -> Result<(), Box<dyn Error>> {
unimplemented!();
pub fn save_to_file(&self, prefix: Option<PathBuf>) -> Result<(), Box<dyn Error>> {
let db = crate::get_db(prefix);
if let Some(dir) = db.parent() {
if !dir.exists() {
fs::create_dir_all(dir)?;
}
}
let fd = File::create(db)?;
let mut writer = Encoder::new(fd, 0)?;
let cfg = PrettyConfig::new().struct_names(true);
ron::ser::to_writer_pretty(&mut writer, self, cfg)?;
writer.finish()?;
Ok(())
}
pub fn rebuild(prefix: Option<PathBuf>) -> Result<(), Box<dyn Error>> {
pub fn rebuild(prefix: Option<PathBuf>) -> Result<Self, Box<dyn Error>> {
unimplemented!();
}
}

View File

@ -22,8 +22,9 @@ pub use {
version::*,
};
pub const DB: [&str; 4] = ["var", "db", "hpk", "db.ron.zstd"];
pub const CACHE: [&str; 3] = ["var", "cache", "hpk"];
const DB: [&str; 4] = ["var", "db", "hpk", "db.ron.zstd"];
const DBDIR: [&str; 3] = ["var", "db", "hpk"];
const CACHE: [&str; 3] = ["var", "cache", "hpk"];
/// Gets the database file path. Takes an optional prefix variable for working
/// with an alternate filesystem root.
@ -38,19 +39,36 @@ pub const CACHE: [&str; 3] = ["var", "cache", "hpk"];
/// let db = hpk::get_db(Some(chroot));
/// ```
pub fn get_db(prefix: Option<PathBuf>) -> PathBuf {
match prefix {
Some(mut p) => {
DB.iter().for_each(|seg| p.push(seg));
p
},
None => {
let mut p = PathBuf::from("/");
DB.iter().for_each(|seg| p.push(seg));
p
},
}
let mut path = match prefix {
Some(p) => p,
None => PathBuf::from("/"),
};
DB.iter().for_each(|seg| path.push(seg));
path
}
/// Gets the database directory path. Takes anoptional prefix variable for
/// working with an alternate filesystem root.
/// ## Examples
/// Get the database path for the currently running system:
/// ```Rust
/// let db = hpk::get_dbdir(None);
/// ```
/// Get the database path for a chroot mounted at /mnt/aarch64:
/// ```Rust
/// let chroot = PathBuf::from("/mnt/aarch64");
/// let db = hpk::get_dbdir(Some(chroot));
/// ```
pub fn get_dbdir(prefix: Option<PathBuf>) -> PathBuf {
let mut path = match prefix {
Some(p) => p,
None => PathBuf::from("/"),
};
DBDIR.iter().for_each(|seg| path.push(seg));
path
}
/// Gets the cache directory path. Takes an optional prefix variable for working
/// with an alternate filesystem root.
/// ## Examples
@ -64,17 +82,12 @@ pub fn get_db(prefix: Option<PathBuf>) -> PathBuf {
/// let db = hpk::get_cache(Some(chroot));
/// ```
pub fn get_cache(prefix: Option<PathBuf>) -> PathBuf {
match prefix {
Some(mut p) => {
CACHE.iter().for_each(|seg| p.push(seg));
p
}
None => {
let mut p = PathBuf::from("/");
CACHE.iter().for_each(|seg| p.push(seg));
p
},
}
let mut path = match prefix {
Some(p) => p,
None => PathBuf::from("/"),
};
CACHE.iter().for_each(|seg| path.push(seg));
path
}
#[cfg(test)]

View File

@ -210,3 +210,22 @@ impl PartialOrd<Rapid> for SemVer {
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn cmp_semver_rapid_gt() {
let sem = "1.42.1".parse::<SemVer>().unwrap();
let rpd = "1.42".parse::<Rapid>().unwrap();
assert!(sem > rpd);
}
#[test]
fn cmp_semver_rapid_eq() {
let sem = "1.42.0".parse::<SemVer>().unwrap();
let rpd = "1.42".parse::<Rapid>().unwrap();
assert!(sem == rpd);
}
}