Skip to content

Commit

Permalink
edit <ID> -> edit <IDLike>
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzuya committed Sep 5, 2020
1 parent 69746ba commit 7c5e0cb
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/command/edit.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::helpers::edit_file;

pub fn edit(id_as_string: &str) -> Result<(), Box<dyn std::error::Error>> {
let (old, new) = edit_file(id_as_string)?;
pub fn edit(id_like_string: &str) -> Result<(), Box<dyn std::error::Error>> {
let (old, new) = edit_file(id_like_string)?;
println!("{} -> {}", old, new);
Ok(())
}
4 changes: 2 additions & 2 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ pub fn create_new_file(content: &str) -> Result<String, Box<dyn std::error::Erro
Ok(file_name)
}

pub fn edit_file(id_as_string: &str) -> Result<(String, String), Box<dyn std::error::Error>> {
let page_id = PageId::from_str(id_as_string).expect("invalid ID format");
pub fn edit_file(id_like_string: &str) -> Result<(String, String), Box<dyn std::error::Error>> {
let page_id = PageId::from_like_str(id_like_string).expect("invalid ID format");
let old_file_name = to_file_name(&page_id);
let mut content = std::fs::read_to_string(&old_file_name)?;
if let Some(index) = content.find("\n## Obsoletes") {
Expand Down
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
clap::SubCommand::with_name("edit")
.about("Creates a new memo that obsoletes the specified memo")
.arg(
clap::Arg::with_name("ID")
clap::Arg::with_name("IDLike")
.help("the id of the memo to edit")
.required(true),
),
Expand All @@ -21,8 +21,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
match matches.subcommand() {
("new", _) => crate::command::new::new()?,
("edit", Some(sub_matches)) => {
let id_as_string: &str = sub_matches.value_of("ID").expect("ID required");
crate::command::edit::edit(id_as_string)?
let id_like_string: &str = sub_matches.value_of("IDLike").expect("IDLike required");
crate::command::edit::edit(id_like_string)?
}
("server", _) => crate::command::server::server()?,
_ => {}
Expand Down
20 changes: 20 additions & 0 deletions src/page_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ impl PageId {
Self::from_timestamp(Utc::now().timestamp())
}

// "YYYYMMDDTHHMMSSZ.md"
// "http://localhost:3000/pages/YYYYMMDDTHHMMSSZ"
pub fn from_like_str(s: &str) -> Option<Self> {
use regex::Regex;
let re = Regex::new(r"^(?:.*)(\d{4}\d{2}\d{2}T\d{2}\d{2}\d{2}Z)(?:.*)$").unwrap();
re.captures(s)
.and_then(|captures| Self::from_str(captures.get(1).unwrap().as_str()))
}

pub fn from_str(s: &str) -> Option<Self> {
NaiveDateTime::parse_from_str(s, "%Y%m%dT%H%M%SZ")
.ok()
Expand Down Expand Up @@ -52,4 +61,15 @@ mod test {
assert_eq!(super::PageId::from_timestamp(32503680000), None);
assert_eq!(super::PageId::from_str("30000101T000000Z"), None);
}

#[test]
fn from_like_str_test() {
let s = "20200808T002147Z";
let from_s = super::PageId::from_str(s).unwrap();
let like1 = super::PageId::from_like_str("20200808T002147Z.md").unwrap();
let like2 =
super::PageId::from_like_str("http://localhost:3000/pages/20200808T002147Z").unwrap();
assert_eq!(from_s, like1);
assert_eq!(from_s, like2);
}
}

0 comments on commit 7c5e0cb

Please sign in to comment.