1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
use anyhow::Result;
use std::fs;
use std::path::{Path, PathBuf};
use structopt::StructOpt;
use thiserror::Error;
#[derive(Debug, StructOpt)]
#[structopt(about = "maintain a journal")]
enum JT {
Init {
#[structopt(help = "Directory where journal should be stored")]
dirname: PathBuf,
#[structopt(help = "Short name for journal")]
journalname: String,
#[structopt(help = "Short description of journal, its title")]
description: String,
},
IsJournal {
#[structopt(help = "Directory that may or may not be a journal")]
dirname: PathBuf,
},
}
#[derive(Debug, Error)]
enum JournalError {
#[error("directory {0} is not a journal")]
NotAJournal(String),
}
fn main() -> Result<()> {
let opt = JT::from_args();
match opt {
JT::Init {
dirname,
journalname,
description,
} => init(&dirname, &journalname, &description)?,
JT::IsJournal { dirname } => is_journal(&dirname)?,
}
Ok(())
}
fn init(dirname: &Path, _journalname: &str, _description: &str) -> anyhow::Result<()> {
std::fs::create_dir(dirname)?;
Ok(())
}
fn is_journal(dirname: &Path) -> anyhow::Result<()> {
let meta = fs::symlink_metadata(dirname)?;
if !meta.is_dir() {
return Err(JournalError::NotAJournal(dirname.to_string_lossy().to_string()).into());
}
Ok(())
}
|