Skip to content

Commit ed3b35e

Browse files
authored
Merge pull request #945 from rylev/simplify-query-extraction2
Simplify query extraction
2 parents 2fa79d3 + cb50dcb commit ed3b35e

File tree

1 file changed

+23
-40
lines changed

1 file changed

+23
-40
lines changed

site/src/selector.rs

Lines changed: 23 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ use database::{Commit, Crate, Index, Lookup, ProcessStatistic, QueryLabel};
3333
use std::convert::TryInto;
3434
use std::fmt;
3535
use std::ops::RangeInclusive;
36+
use std::str::FromStr;
3637
use std::sync::Arc;
3738

3839
/// Finds the most appropriate `ArtifactId` for a given bound.
@@ -361,6 +362,17 @@ impl Query {
361362
}
362363
}
363364

365+
fn extract_as<T>(&mut self, tag: Tag) -> Result<Selector<T>, String>
366+
where
367+
T: FromStr,
368+
<T as FromStr>::Err: fmt::Display,
369+
{
370+
Ok(self.extract(tag)?.raw.try_map(|p| {
371+
p.parse::<T>()
372+
.map_err(|e| format!("failed to parse query tag {:?}: {}", tag, e))
373+
})?)
374+
}
375+
364376
fn assert_empty(&self) -> Result<(), String> {
365377
if self.path.is_empty() {
366378
Ok(())
@@ -528,11 +540,6 @@ impl SiteCtxt {
528540
}
529541
}
530542

531-
#[derive(Clone)]
532-
pub struct Source<'a> {
533-
ctxt: &'a SiteCtxt,
534-
}
535-
536543
pub struct ProcessStatisticSeries {
537544
artifact_ids: ArtifactIdIter,
538545
points: std::vec::IntoIter<Option<f64>>,
@@ -549,19 +556,10 @@ impl ProcessStatisticSeries {
549556
mut query: Query,
550557
) -> Result<Vec<SeriesResponse<Self>>, String> {
551558
let dumped = format!("{:?}", query);
552-
let krate = query.extract(Tag::Crate)?.raw;
553-
let profile = query
554-
.extract(Tag::Profile)?
555-
.raw
556-
.try_map(|p| p.parse::<Profile>())?;
557-
let cache = query
558-
.extract(Tag::Cache)?
559-
.raw
560-
.try_map(|p| p.parse::<Cache>())?;
561-
let statid = query
562-
.extract(Tag::ProcessStatistic)?
563-
.raw
564-
.try_map(|p| p.parse::<ProcessStatistic>())?;
559+
let krate = query.extract_as::<String>(Tag::Crate)?;
560+
let profile = query.extract_as::<Profile>(Tag::Profile)?;
561+
let cache = query.extract_as::<Cache>(Tag::Cache)?;
562+
let statid = query.extract_as::<ProcessStatistic>(Tag::ProcessStatistic)?;
565563
query.assert_empty()?;
566564

567565
let index = ctxt.index.load();
@@ -736,15 +734,9 @@ impl SelfProfile {
736734
ctxt: &SiteCtxt,
737735
mut query: Query,
738736
) -> Result<Vec<SeriesResponse<Self>>, String> {
739-
let krate = query.extract(Tag::Crate)?.raw;
740-
let profile = query
741-
.extract(Tag::Profile)?
742-
.raw
743-
.try_map(|p| p.parse::<Profile>())?;
744-
let cache = query
745-
.extract(Tag::Cache)?
746-
.raw
747-
.try_map(|p| p.parse::<Cache>())?;
737+
let krate = query.extract_as::<String>(Tag::Crate)?;
738+
let profile = query.extract_as::<Profile>(Tag::Profile)?;
739+
let cache = query.extract_as::<Cache>(Tag::Cache)?;
748740
query.assert_empty()?;
749741

750742
let mut series = ctxt
@@ -832,19 +824,10 @@ impl SelfProfileQueryTime {
832824
ctxt: &SiteCtxt,
833825
mut query: Query,
834826
) -> Result<Vec<SeriesResponse<Self>>, String> {
835-
let krate = query.extract(Tag::Crate)?.raw;
836-
let profile = query
837-
.extract(Tag::Profile)?
838-
.raw
839-
.try_map(|p| p.parse::<Profile>())?;
840-
let cache = query
841-
.extract(Tag::Cache)?
842-
.raw
843-
.try_map(|p| p.parse::<Cache>())?;
844-
let ql = query
845-
.extract(Tag::QueryLabel)?
846-
.raw
847-
.map(|p| QueryLabel::from(p.as_str()));
827+
let krate = query.extract_as::<String>(Tag::Crate)?;
828+
let profile = query.extract_as::<Profile>(Tag::Profile)?;
829+
let cache = query.extract_as::<Cache>(Tag::Cache)?;
830+
let ql = query.extract_as::<QueryLabel>(Tag::QueryLabel)?;
848831
query.assert_empty()?;
849832

850833
let index = ctxt.index.load();

0 commit comments

Comments
 (0)