handle optional tags, add warning log level

This commit is contained in:
yggverse 2025-02-11 21:07:45 +02:00
parent 2ef0c8e7ff
commit 3fefd038f1
4 changed files with 53 additions and 19 deletions

View file

@ -15,7 +15,7 @@ pub struct Argument {
#[arg(short, long, default_value_t = 60)]
pub update: u64,
/// Show output (`d` by default)
#[arg(short, long, default_value_t = String::from("d"))]
/// Show output (`dw` by default)
#[arg(short, long, default_value_t = String::from("dw"))]
pub output: String,
}

View file

@ -32,27 +32,53 @@ fn crawl(source: &str, target: &str, output: &Output) -> Result<(), Box<dyn Erro
let mut total = 0;
let mut exist = 0;
for item in Channel::read_from(&get(source)?.bytes()?[..])?.items() {
for item in Channel::read_from(&get(source)?.bytes()?[..])?
.items()
.iter()
{
total += 1;
let path = &path(target, item.pub_date().unwrap(), true)?;
// handle item data
let mut data = Vec::new();
if metadata(path).is_ok() {
exist += 1;
continue;
let path = match item.pub_date() {
Some(pub_data) => {
let path = path(target, pub_data, true)?;
if metadata(&path).is_ok() {
exist += 1;
continue; // skip existing records
}
data.push(format!("# {pub_data}"));
path
}
None => {
output.warning("item skipped as `pub_date` is required by application");
continue;
}
};
if let Some(description) = item.description() {
data.push(description.to_string());
}
let mut file = File::create(path)?;
if let Some(content) = item.content() {
data.push(content.to_string());
}
file.write_all(
format!(
"# {}\n\n{}\n\n=> {}",
item.pub_date().unwrap(),
item.description().unwrap(),
item.link().unwrap()
)
.as_bytes(),
)?;
/* @TODO local storage
if let Some(enclosure) = item.enclosure() {
match enclosure.mime_type.as_str() {
"image/jpeg" => todo!(),
_ => todo!(),
}
} */
if let Some(link) = item.link() {
data.push(format!("=> {link}"));
}
// record item to static file
File::create(&path)?.write_all(data.join("\n\n").as_bytes())?;
}
output.debug(&format!(

View file

@ -3,6 +3,7 @@ use std::time::SystemTime;
struct Level {
debug: bool,
warning: bool,
}
pub struct Output(Level);
@ -11,7 +12,7 @@ impl Output {
pub fn build(level: &str) -> Self {
Self(Level {
debug: level.contains("d"),
// @TODO other flags
warning: level.contains("w"),
})
}
@ -20,6 +21,12 @@ impl Output {
eprintln!("[debug] [{}] {message}", time());
}
}
pub fn warning(&self, message: &str) {
if self.0.warning {
eprintln!("[warning] [{}] {message}", time());
}
}
}
fn time() -> String {