Skip to content

Commit

Permalink
remove unwrap from entity crate
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzuya committed Nov 10, 2021
1 parent 6e782fc commit 32716ba
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions crates/entity/src/entity/page_id.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use chrono::prelude::*;
use regex::Regex;
use std::str::FromStr;
use thiserror::Error;

Expand All @@ -17,8 +18,8 @@ impl PageId {
// "YYYYMMDDTHHMMSSZ.md"
// "http://localhost:3000/pages/YYYYMMDDTHHMMSSZ"
pub fn from_like_str(s: &str) -> Result<Self, ParsePageIdError> {
use regex::Regex;
let re = Regex::new(r"^(?:.*)(\d{4}\d{2}\d{2}T\d{2}\d{2}\d{2}Z)(?:.*)$").unwrap();
let re = Regex::new(r"^(?:.*)(\d{4}\d{2}\d{2}T\d{2}\d{2}\d{2}Z)(?:.*)$")
.map_err(|_| ParsePageIdError)?;
let captures = re.captures(s).ok_or(ParsePageIdError)?;
let capture = captures.get(1).ok_or(ParsePageIdError)?;
Self::from_str(capture.as_str())
Expand Down Expand Up @@ -55,13 +56,14 @@ impl std::str::FromStr for PageId {
mod tests {
use std::str::FromStr;

use anyhow::Context;

use super::*;

#[test]
fn new_test() -> anyhow::Result<()> {
// TODO: unwrap
assert_ne!(
PageId::new().unwrap(),
PageId::new().context("not supported timestamp")?,
PageId::from_str("20200808T101010Z")?
);
Ok(())
Expand All @@ -71,8 +73,7 @@ mod tests {
fn from_test() -> anyhow::Result<()> {
let s = "20200808T002147Z";
let d = 1596846107;
// TODO: unwrap
let from_d = PageId::from_timestamp(d).unwrap();
let from_d = PageId::from_timestamp(d).context("not supported timestamp")?;
let from_s = PageId::from_str(s)?;
assert_eq!(from_d, from_s);
assert_eq!(from_d.to_string(), s);
Expand Down

0 comments on commit 32716ba

Please sign in to comment.