Skip to content

Commit

Permalink
make parse_router public in rfs for other tool
Browse files Browse the repository at this point in the history
  • Loading branch information
rawdaGastan committed May 22, 2024
1 parent e2110d6 commit c7cf082
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 36 deletions.
37 changes: 1 addition & 36 deletions rfs/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ fn pack(opts: PackOptions) -> Result<()> {
let rt = tokio::runtime::Runtime::new()?;

rt.block_on(async move {
let store = parse_router(opts.store.as_slice()).await?;
let store = store::parse_router(opts.store.as_slice()).await?;
let meta = fungi::Writer::new(opts.meta).await?;
rfs::pack(meta, store, opts.target, !opts.no_strip_password).await?;

Expand Down Expand Up @@ -242,38 +242,3 @@ async fn get_router(meta: &fungi::Reader) -> Result<Router<Stores>> {

Ok(router)
}

async fn parse_router(urls: &[String]) -> Result<Router<Stores>> {
let mut router = Router::new();
let pattern = r"^(?P<range>[0-9a-f]{2}-[0-9a-f]{2})=(?P<url>.+)$";
let re = Regex::new(pattern)?;

for u in urls {
let ((start, end), store) = match re.captures(u) {
None => ((0x00, 0xff), store::make(u).await?),
Some(captures) => {
let url = captures.name("url").context("missing url group")?.as_str();
let rng = captures
.name("range")
.context("missing range group")?
.as_str();

let store = store::make(url).await?;
let range = match rng.split_once('-') {
None => anyhow::bail!("invalid range format"),
Some((low, high)) => (
u8::from_str_radix(low, 16)
.with_context(|| format!("failed to parse low range '{}'", low))?,
u8::from_str_radix(high, 16)
.with_context(|| format!("failed to parse high range '{}'", high))?,
),
};
(range, store)
}
};

router.add(start, end, store);
}

Ok(router)
}
38 changes: 38 additions & 0 deletions rfs/src/store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ mod router;
pub mod s3store;
pub mod zdb;

use anyhow::Context;
use rand::seq::SliceRandom;

pub use bs::BlockStore;
use regex::Regex;

pub use self::router::Router;

Expand Down Expand Up @@ -161,6 +163,42 @@ where
routes
}
}

pub async fn parse_router(urls: &[String]) -> anyhow::Result<Router<Stores>> {
let mut router = Router::new();
let pattern = r"^(?P<range>[0-9a-f]{2}-[0-9a-f]{2})=(?P<url>.+)$";
let re = Regex::new(pattern)?;

for u in urls {
let ((start, end), store) = match re.captures(u) {
None => ((0x00, 0xff), make(u).await?),
Some(captures) => {
let url = captures.name("url").context("missing url group")?.as_str();
let rng = captures
.name("range")
.context("missing range group")?
.as_str();

let store = make(url).await?;
let range = match rng.split_once('-') {
None => anyhow::bail!("invalid range format"),
Some((low, high)) => (
u8::from_str_radix(low, 16)
.with_context(|| format!("failed to parse low range '{}'", low))?,
u8::from_str_radix(high, 16)
.with_context(|| format!("failed to parse high range '{}'", high))?,
),
};
(range, store)
}
};

router.add(start, end, store);
}

Ok(router)
}

pub enum Stores {
S3(s3store::S3Store),
Dir(dir::DirStore),
Expand Down

0 comments on commit c7cf082

Please sign in to comment.