Skip to content

Commit

Permalink
Make PageHtml generic over PageContent type (#1123)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored Dec 30, 2022
1 parent b4a2fba commit 8895f62
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
18 changes: 9 additions & 9 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ impl Server {
Extension(chain): Extension<Chain>,
Extension(index): Extension<Arc<Index>>,
Path(DeserializeFromStr(sat)): Path<DeserializeFromStr<Sat>>,
) -> ServerResult<PageHtml> {
) -> ServerResult<PageHtml<SatHtml>> {
let satpoint = index.rare_sat_satpoint(sat).map_err(|err| {
ServerError::Internal(anyhow!(
"failed to satpoint for sat {sat} from index: {err}"
Expand Down Expand Up @@ -348,7 +348,7 @@ impl Server {
Extension(chain): Extension<Chain>,
Extension(index): Extension<Arc<Index>>,
Path(outpoint): Path<OutPoint>,
) -> ServerResult<PageHtml> {
) -> ServerResult<PageHtml<OutputHtml>> {
let output = index
.get_transaction(outpoint.txid)
.map_err(ServerError::Internal)?
Expand Down Expand Up @@ -388,7 +388,7 @@ impl Server {
DeserializeFromStr<Sat>,
DeserializeFromStr<Sat>,
)>,
) -> ServerResult<PageHtml> {
) -> ServerResult<PageHtml<RangeHtml>> {
match start.cmp(&end) {
Ordering::Equal => Err(ServerError::BadRequest("empty range".to_string())),
Ordering::Greater => Err(ServerError::BadRequest(
Expand Down Expand Up @@ -417,7 +417,7 @@ impl Server {
async fn home(
Extension(chain): Extension<Chain>,
Extension(index): Extension<Arc<Index>>,
) -> ServerResult<PageHtml> {
) -> ServerResult<PageHtml<HomeHtml>> {
Ok(
HomeHtml::new(
index
Expand All @@ -442,7 +442,7 @@ impl Server {
Extension(chain): Extension<Chain>,
Extension(index): Extension<Arc<Index>>,
Path(DeserializeFromStr(query)): Path<DeserializeFromStr<BlockQuery>>,
) -> ServerResult<PageHtml> {
) -> ServerResult<PageHtml<BlockHtml>> {
let (block, height) = match query {
BlockQuery::Height(height) => {
let block = index
Expand Down Expand Up @@ -491,7 +491,7 @@ impl Server {
Extension(index): Extension<Arc<Index>>,
Extension(chain): Extension<Chain>,
Path(txid): Path<Txid>,
) -> ServerResult<PageHtml> {
) -> ServerResult<PageHtml<TransactionHtml>> {
let inscription = index
.get_inscription_by_inscription_id(txid)
.map_err(|err| {
Expand Down Expand Up @@ -618,7 +618,7 @@ impl Server {
Extension(chain): Extension<Chain>,
Extension(index): Extension<Arc<Index>>,
Path(path): Path<(u64, usize, usize)>,
) -> Result<PageHtml, ServerError> {
) -> Result<PageHtml<InputHtml>, ServerError> {
let not_found =
|| ServerError::NotFound(format!("input /{}/{}/{} unknown", path.0, path.1, path.2));

Expand Down Expand Up @@ -696,7 +696,7 @@ impl Server {
Extension(chain): Extension<Chain>,
Extension(index): Extension<Arc<Index>>,
Path(inscription_id): Path<InscriptionId>,
) -> ServerResult<PageHtml> {
) -> ServerResult<PageHtml<InscriptionHtml>> {
let (inscription, satpoint) = index
.get_inscription_by_inscription_id(inscription_id)
.map_err(|err| {
Expand Down Expand Up @@ -731,7 +731,7 @@ impl Server {
async fn inscriptions(
Extension(chain): Extension<Chain>,
Extension(index): Extension<Arc<Index>>,
) -> ServerResult<PageHtml> {
) -> ServerResult<PageHtml<InscriptionsHtml>> {
Ok(
InscriptionsHtml {
inscriptions: index
Expand Down
19 changes: 9 additions & 10 deletions src/templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,19 @@ mod sat;
mod transaction;

#[derive(Boilerplate)]
pub(crate) struct PageHtml {
pub(crate) struct PageHtml<T: PageContent> {
chain: Chain,
content: Box<dyn PageContent>,
content: T,
has_satoshi_index: bool,
}

impl PageHtml {
pub(crate) fn new<T: PageContent + 'static>(
content: T,
chain: Chain,
has_satoshi_index: bool,
) -> Self {
impl<T> PageHtml<T>
where
T: PageContent,
{
pub(crate) fn new(content: T, chain: Chain, has_satoshi_index: bool) -> Self {
Self {
content: Box::new(content),
content,
has_satoshi_index,
chain,
}
Expand All @@ -43,7 +42,7 @@ impl PageHtml {
pub(crate) trait PageContent: Display + 'static {
fn title(&self) -> String;

fn page(self, chain: Chain, has_satoshi_index: bool) -> PageHtml
fn page(self, chain: Chain, has_satoshi_index: bool) -> PageHtml<Self>
where
Self: Sized,
{
Expand Down

0 comments on commit 8895f62

Please sign in to comment.