Skip to content

Commit

Permalink
Merge branch 'master' into ordinal-next-prev
Browse files Browse the repository at this point in the history
  • Loading branch information
terror authored Aug 17, 2022
2 parents bab6ae5 + 9e35d31 commit 64d95e0
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ impl Index {
let height_to_hash = rtx.0.open_table(HEIGHT_TO_HASH)?;

let mut cursor = height_to_hash
.range(height.saturating_sub(take)..=height)?
.range(height.saturating_sub(take.saturating_sub(1))..=height)?
.rev();

while let Some(next) = cursor.next() {
Expand Down
21 changes: 8 additions & 13 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use {
self::{
deserialize_ordinal_from_str::DeserializeOrdinalFromStr,
templates::{
block::BlockHtml, ordinal::OrdinalHtml, output::OutputHtml, root::RootHtml,
block::BlockHtml, ordinal::OrdinalHtml, output::OutputHtml, range::RangeHtml, root::RootHtml,
transaction::TransactionHtml, Content,
},
tls_acceptor::TlsAcceptor,
Expand All @@ -16,6 +16,7 @@ use {
AcmeConfig,
},
serde::{de, Deserializer},
std::cmp::Ordering,
tokio_stream::StreamExt,
};

Expand Down Expand Up @@ -203,21 +204,15 @@ impl Server {
(DeserializeOrdinalFromStr, DeserializeOrdinalFromStr),
>,
) -> impl IntoResponse {
if start == end {
return (StatusCode::BAD_REQUEST, Html("Empty Range".to_string()));
}

if start > end {
return (
match start.cmp(&end) {
Ordering::Equal => (StatusCode::BAD_REQUEST, Html("Empty Range".to_string())).into_response(),
Ordering::Greater => (
StatusCode::BAD_REQUEST,
Html("Range Start Greater Than Range End".to_string()),
);
)
.into_response(),
Ordering::Less => RangeHtml { start, end }.page().into_response(),
}

(
StatusCode::OK,
Html(format!("<a href='/ordinal/{start}'>first</a>")),
)
}

async fn root(index: extract::Extension<Arc<Index>>) -> impl IntoResponse {
Expand Down
1 change: 1 addition & 0 deletions src/subcommand/server/templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use {super::*, boilerplate::Display};
pub(crate) mod block;
pub(crate) mod ordinal;
pub(crate) mod output;
pub(crate) mod range;
pub(crate) mod root;
pub(crate) mod transaction;

Expand Down
62 changes: 62 additions & 0 deletions src/subcommand/server/templates/range.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use super::*;

#[derive(Display)]
pub(crate) struct RangeHtml {
pub(crate) start: Ordinal,
pub(crate) end: Ordinal,
}

impl Content for RangeHtml {
fn title(&self) -> String {
format!("Ordinal range [{},{})", self.start, self.end)
}

fn page(self) -> PageHtml {
PageHtml {
content: Box::new(self),
}
}
}

#[cfg(test)]
mod tests {
use {super::*, pretty_assertions::assert_eq, unindent::Unindent};

#[test]
fn ordinal_html() {
assert_eq!(
RangeHtml {
start: Ordinal(0),
end: Ordinal(1),
}
.to_string(),
"
<h1>Ordinal range [0,1)</h1>
<dl>
<dt>size</dt><dd>1</dd>
<dt>first</dt><dd><a href=/ordinal/0>0</a></dd>
</dl>
"
.unindent()
);
}

#[test]
fn bugfix_broken_link() {
assert_eq!(
RangeHtml {
start: Ordinal(1),
end: Ordinal(10),
}
.to_string(),
"
<h1>Ordinal range [1,10)</h1>
<dl>
<dt>size</dt><dd>9</dd>
<dt>first</dt><dd><a href=/ordinal/1>1</a></dd>
</dl>
"
.unindent()
);
}
}
5 changes: 5 additions & 0 deletions templates/range.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h1>Ordinal range [{{self.start}},{{self.end}})</h1>
<dl>
<dt>size</dt><dd>{{self.end.n() - self.start.n()}}</dd>
<dt>first</dt><dd><a href=/ordinal/{{self.start.n()}}>{{self.start.n()}}</a></dd>
</dl>
14 changes: 11 additions & 3 deletions tests/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,16 @@ fn empty_range_returns_400() {
}

#[test]
fn range_links_to_first() {
State::new().request("range/0/1", 200, "<a href='/ordinal/0'>first</a>");
fn range() {
State::new().request_regex(
"range/0/1",
200,
r".*<title>Ordinal range \[0,1\)</title>.*<h1>Ordinal range \[0,1\)</h1>
<dl>
<dt>size</dt><dd>1</dd>
<dt>first</dt><dd><a href=/ordinal/0>0</a></dd>
</dl>.*",
);
}

#[test]
Expand Down Expand Up @@ -162,7 +170,7 @@ fn root_block_limit() {
state.request_regex(
"/",
200,
".*<ul>\n( <li>[[:digit:]]{3} - <a href=/block/[[:xdigit:]]{64}>[[:xdigit:]]{64}</a></li>\n){101}</ul>.*"
".*<ul>\n( <li>[[:digit:]]{3} - <a href=/block/[[:xdigit:]]{64}>[[:xdigit:]]{64}</a></li>\n){100}</ul>.*"
);
}

Expand Down

0 comments on commit 64d95e0

Please sign in to comment.