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 { use {
crate::{Package, Repository, Version}, crate::{Package, Repository, Version},
serde::{Deserialize, Serialize}, serde::{Deserialize, Serialize},
std::{collections::HashMap, error::Error}, std::{collections::HashMap, error::Error, fmt, fs::{self, File}, path::PathBuf},
url::Url, url::Url,
zstd::{Decoder, Encoder},
}; };
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
@ -92,15 +93,35 @@ impl Database {
Ok(updates) Ok(updates)
} }
pub fn from_file(prefix: Option<PathBuf>) -> Result<File, Box<dyne Error>> { pub fn from_file(prefix: Option<PathBuf>) -> Result<Self, Box<dyn Error>> {
unimplemented!(); 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>> { pub fn save_to_file(&self, prefix: Option<PathBuf>) -> Result<(), Box<dyn Error>> {
unimplemented!(); 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!(); unimplemented!();
} }
} }

View File

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