Allow specifying how many items go into the feed

Signed-off-by: Jacob Kiers <code@kiers.eu>
This commit is contained in:
Jacob Kiers 2024-06-27 17:46:49 +02:00
parent b7e284aba0
commit 48caa99423
2 changed files with 10 additions and 5 deletions

View File

@ -31,6 +31,9 @@ pub(crate) struct FeedBuilderSettings {
/// Create an HTML file for each message
#[clap(short, long, value_parser, default_value_t = false)]
pub include_html: bool,
/// Maximum number of items in the feed (default: unlimited)
#[clap(short = 'n', long, value_parser)]
pub max_items: Option<usize>,
}
#[derive(Subcommand)]

View File

@ -28,7 +28,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let data_directory = "data";
let result = match &cli.command {
match &cli.command {
cli::Command::FetchFromImap(imap_settings) => fetch_from_imap(
data_directory,
imap_settings.server.to_owned(),
@ -41,14 +41,13 @@ fn main() -> Result<(), Box<dyn Error>> {
&settings.filename,
&settings.hostname,
settings.include_html,
settings.max_items
),
cli::Command::Update => command::update::self_update(),
_ => unimplemented!("This method is not yet implemented."),
};
result
}
}
fn create_directory<P: AsRef<Path>>(dir: P) -> Result<(), std::io::Error> {
@ -63,6 +62,7 @@ fn build_feed(
filename: &PathBuf,
hostname: &String,
include_html: bool,
max_items_in_feed: Option<usize>
) -> Result<(), Box<dyn Error>> {
let dir = filename.parent().ok_or(format!(
"Could not get parent directory of {}",
@ -87,7 +87,9 @@ fn build_feed(
let mut reader = DataDirectoryMessageReader::new(Path::new("data").to_path_buf());
for msg in reader.read_rfc822_messages() {
let max_items_in_feed = max_items_in_feed.unwrap_or_else(|| usize::MAX);
for msg in reader.read_rfc822_messages().take(max_items_in_feed) {
let parsed = msg.get_parsed().expect("A parsed messsage.");
let date = parsed.date().ok_or(format!(