This commit is contained in:
parent
d1882114c7
commit
bd589d8b45
15 changed files with 221 additions and 12 deletions
18
pkgr/src/repo/index/index_package.rs
Normal file
18
pkgr/src/repo/index/index_package.rs
Normal file
|
@ -0,0 +1,18 @@
|
|||
use std::io;
|
||||
use std::path::Path;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use url::Url;
|
||||
use pkgfile::PKGFile;
|
||||
use crate::api::Query;
|
||||
use crate::CONFIG;
|
||||
use crate::package::Package;
|
||||
use crate::repo::index::RepoIndex;
|
||||
|
||||
/// This struct solely exists for indexing and has no real functionality.
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct IndexPackage {
|
||||
name: String,
|
||||
versions: Vec<u32>,
|
||||
tags: Vec<String>,
|
||||
uri: Url
|
||||
}
|
58
pkgr/src/repo/index/mod.rs
Normal file
58
pkgr/src/repo/index/mod.rs
Normal file
|
@ -0,0 +1,58 @@
|
|||
use std::collections::HashMap;
|
||||
use std::io;
|
||||
use std::path::Path;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use crate::api::Request;
|
||||
use crate::CONFIG;
|
||||
use crate::repo::index::index_package::IndexPackage;
|
||||
use crate::repo::Repo;
|
||||
use crate::types::fetch::{Fetch, TryFetch};
|
||||
use crate::util::create_uuid;
|
||||
|
||||
pub mod index_package;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct RepoIndex {
|
||||
origin_repo: String,
|
||||
packages: HashMap<String, IndexPackage>
|
||||
}
|
||||
|
||||
impl RepoIndex {
|
||||
// /// Fetch existing index or create a new one.
|
||||
// pub fn from_repo(repo: Repo) -> Self {
|
||||
//
|
||||
// }
|
||||
//
|
||||
// /// Create new index.
|
||||
// pub fn create_with_repo(repo: Repo) -> Self {
|
||||
// }
|
||||
|
||||
/// Get repo
|
||||
pub fn get_repo(&self) -> io::Result<Repo> {
|
||||
Ok(Repo::from_name(&self.origin_repo)?)
|
||||
}
|
||||
|
||||
pub fn from_path(path: &str) -> Result<RepoIndex, String> {
|
||||
match std::fs::read_to_string(path) {
|
||||
Ok(s) => match toml::from_str(&s) {
|
||||
Ok(c) => Ok(c),
|
||||
Err(e) => Err(format!("failed to parse config: {}", e)),
|
||||
},
|
||||
Err(e) => Err(format!("failed to read config: {}", e)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFetch<Repo> for RepoIndex {
|
||||
type Error = ();
|
||||
/// Fetch
|
||||
fn try_fetch(query: Repo) -> Result<Self, Self::Error> {
|
||||
let path = CONFIG.with(|c| c.storage.index_dir.clone()) + query.uri.as_str() + ".toml";
|
||||
if Path::new(path.as_str()).exists() {
|
||||
RepoIndex::from_path(path.as_str())
|
||||
.map_err(|_| ())
|
||||
} else {
|
||||
Err(())
|
||||
}
|
||||
}
|
||||
}
|
42
pkgr/src/repo/mod.rs
Normal file
42
pkgr/src/repo/mod.rs
Normal file
|
@ -0,0 +1,42 @@
|
|||
use std::io;
|
||||
use std::io::{Error, ErrorKind};
|
||||
use std::path::Path;
|
||||
use url::Url;
|
||||
use crate::CONFIG;
|
||||
use crate::config::repos::RepoFile;
|
||||
|
||||
/// Indexed repos
|
||||
pub mod index;
|
||||
|
||||
pub struct Repo {
|
||||
name: String,
|
||||
uri: Url,
|
||||
}
|
||||
|
||||
impl Repo {
|
||||
pub fn from_name(name: &String) -> io::Result<Repo> {
|
||||
let r = RepoFile::from_path(CONFIG.with(|c| c.storage.repo_file.clone()).as_str())
|
||||
.map_err(|e| Error::new(ErrorKind::Other, e))?;
|
||||
let r = r
|
||||
.repos
|
||||
.get(name)
|
||||
.ok_or(Error::new(ErrorKind::InvalidData, "Could not get repo"))?;
|
||||
Ok(Repo {
|
||||
name: r.name.clone(),
|
||||
uri: r.uri.clone()
|
||||
})
|
||||
}
|
||||
|
||||
pub fn get_name(&self) -> String {
|
||||
self.name.clone()
|
||||
}
|
||||
|
||||
pub fn get_uri(&self) -> Url {
|
||||
self.uri.clone()
|
||||
}
|
||||
|
||||
/// Fetch indexed repo
|
||||
pub fn get_index(&self) {
|
||||
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue