From ba9c6ede3ecf211bf3981c709a60ce8005d73484 Mon Sep 17 00:00:00 2001 From: Dominic Date: Thu, 20 Jun 2024 18:30:53 +0200 Subject: [PATCH] extract stuff into lib/main --- src/cli.rs | 54 +++++++++++++++++++++++++++++++ src/iotro.rs | 5 ++- src/lib.rs | 17 ++++++++++ src/main.rs | 77 +++++--------------------------------------- src/preset.rs | 22 ++++++------- src/project.rs | 76 +++++++++++++++++++++---------------------- src/question.rs | 2 +- src/render/ffmpeg.rs | 4 +-- src/render/mod.rs | 14 ++++---- 9 files changed, 142 insertions(+), 129 deletions(-) create mode 100644 src/cli.rs create mode 100644 src/lib.rs diff --git a/src/cli.rs b/src/cli.rs new file mode 100644 index 0000000..9320c6f --- /dev/null +++ b/src/cli.rs @@ -0,0 +1,54 @@ +//! This module contains helper functions for implementing CLI/TUI. + +use crate::time::{parse_time, Time}; +use console::style; +use std::{ + fmt::Display, + io::{self, BufRead as _, Write as _} +}; + +pub fn ask(question: impl Display) -> String { + let mut stdout = io::stdout().lock(); + let mut stdin = io::stdin().lock(); + + write!( + stdout, + "{} {} ", + style(question).bold().magenta(), + style(">").cyan() + ) + .unwrap(); + stdout.flush().unwrap(); + let mut line = String::new(); + stdin.read_line(&mut line).unwrap(); + line.trim().to_owned() +} + +pub fn ask_time(question: impl Display + Copy) -> Time { + let mut stdout = io::stdout().lock(); + let mut stdin = io::stdin().lock(); + + let mut line = String::new(); + loop { + line.clear(); + write!( + stdout, + "{} {} ", + style(question).bold().magenta(), + style(">").cyan() + ) + .unwrap(); + stdout.flush().unwrap(); + stdin.read_line(&mut line).unwrap(); + let line = line.trim(); + match parse_time(line) { + Ok(time) => return time, + Err(err) => writeln!( + stdout, + "{} {line:?}: {err}", + style("Invalid Input").bold().red() + ) + .unwrap() + } + } +} diff --git a/src/iotro.rs b/src/iotro.rs index a4eeccb..e37045e 100644 --- a/src/iotro.rs +++ b/src/iotro.rs @@ -1,6 +1,9 @@ //! A module for writing intros and outros -use crate::{time::Date, ProjectLecture, Resolution}; +use crate::{ + project::{ProjectLecture, Resolution}, + time::Date +}; use anyhow::anyhow; use std::{ fmt::{self, Debug, Display, Formatter}, diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..998054a --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,17 @@ +#![allow(clippy::manual_range_contains)] +#![warn(clippy::unreadable_literal, rust_2018_idioms)] +#![forbid(elided_lifetimes_in_paths, unsafe_code)] + +pub mod cli; +pub mod iotro; +pub mod preset; +pub mod project; +pub mod question; +pub mod render; +pub mod time; + +#[cfg(feature = "mem_limit")] +use std::sync::RwLock; + +#[cfg(feature = "mem_limit")] +pub static MEM_LIMIT: RwLock = RwLock::new(String::new()); diff --git a/src/main.rs b/src/main.rs index 67d9ce0..9f895df 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,32 +2,17 @@ #![warn(clippy::unreadable_literal, rust_2018_idioms)] #![forbid(elided_lifetimes_in_paths, unsafe_code)] -mod iotro; -mod preset; -mod project; -mod question; -mod render; -mod time; - -use self::{ - project::{Project, ProjectLecture, ProjectSource, Resolution}, - render::Renderer, - time::{parse_date, parse_time, Time} -}; -use crate::preset::Preset; use camino::Utf8PathBuf as PathBuf; use clap::Parser; use console::style; -#[cfg(feature = "mem_limit")] -use std::sync::RwLock; -use std::{ - fmt::Display, - fs, - io::{self, BufRead as _, Write} +use render_video::{ + cli::{ask, ask_time}, + preset::Preset, + project::{Project, ProjectLecture, ProjectSource, Resolution}, + render::Renderer, + time::parse_date }; - -#[cfg(feature = "mem_limit")] -static MEM_LIMIT: RwLock = RwLock::new(String::new()); +use std::fs; #[derive(Debug, Parser)] struct Args { @@ -60,58 +45,12 @@ struct Args { stereo: bool } -fn ask(question: impl Display) -> String { - let mut stdout = io::stdout().lock(); - let mut stdin = io::stdin().lock(); - - write!( - stdout, - "{} {} ", - style(question).bold().magenta(), - style(">").cyan() - ) - .unwrap(); - stdout.flush().unwrap(); - let mut line = String::new(); - stdin.read_line(&mut line).unwrap(); - line.trim().to_owned() -} - -fn ask_time(question: impl Display + Copy) -> Time { - let mut stdout = io::stdout().lock(); - let mut stdin = io::stdin().lock(); - - let mut line = String::new(); - loop { - line.clear(); - write!( - stdout, - "{} {} ", - style(question).bold().magenta(), - style(">").cyan() - ) - .unwrap(); - stdout.flush().unwrap(); - stdin.read_line(&mut line).unwrap(); - let line = line.trim(); - match parse_time(line) { - Ok(time) => return time, - Err(err) => writeln!( - stdout, - "{} {line:?}: {err}", - style("Invalid Input").bold().red() - ) - .unwrap() - } - } -} - fn main() { let args = Args::parse(); #[cfg(feature = "mem_limit")] { - *(MEM_LIMIT.write().unwrap()) = args.mem_limit; + *(render_video::MEM_LIMIT.write().unwrap()) = args.mem_limit; } // process arguments diff --git a/src/preset.rs b/src/preset.rs index 85f37c6..fc19763 100644 --- a/src/preset.rs +++ b/src/preset.rs @@ -11,23 +11,23 @@ use std::{fs, io}; #[serde_as] #[derive(Deserialize, Serialize)] -pub(crate) struct Preset { +pub struct Preset { // options for the intro slide - pub(crate) course: String, - pub(crate) label: String, - pub(crate) docent: String, + pub course: String, + pub label: String, + pub docent: String, /// Course language #[serde(default = "Default::default")] #[serde_as(as = "DisplayFromStr")] - pub(crate) lang: Language<'static>, + pub lang: Language<'static>, // coding options - pub(crate) transcode_start: Resolution, - pub(crate) transcode: Option + pub transcode_start: Resolution, + pub transcode: Option } -fn preset_23ws_malo2() -> Preset { +pub fn preset_23ws_malo2() -> Preset { Preset { course: "23ws-malo2".into(), label: "Mathematische Logik II".into(), @@ -38,7 +38,7 @@ fn preset_23ws_malo2() -> Preset { } } -fn preset_24ss_algomod() -> Preset { +pub fn preset_24ss_algomod() -> Preset { Preset { course: "24ss-algomod".into(), label: "Algorithmische Modelltheorie".into(), @@ -49,7 +49,7 @@ fn preset_24ss_algomod() -> Preset { } } -fn preset_24ss_qc() -> Preset { +pub fn preset_24ss_qc() -> Preset { Preset { course: "24ss-qc".into(), label: "Introduction to Quantum Computing".into(), @@ -61,7 +61,7 @@ fn preset_24ss_qc() -> Preset { } impl Preset { - pub(crate) fn find(name: &str) -> anyhow::Result { + pub fn find(name: &str) -> anyhow::Result { match fs::read(name) { Ok(buf) => return Ok(toml::from_slice(&buf)?), Err(err) if err.kind() == io::ErrorKind::NotFound => {}, diff --git a/src/project.rs b/src/project.rs index 7dbda1f..6ea1bdd 100644 --- a/src/project.rs +++ b/src/project.rs @@ -14,7 +14,7 @@ macro_rules! resolutions { ($($res:ident: $width:literal x $height:literal at $bitrate:literal in $format:ident),+) => { #[allow(non_camel_case_types, clippy::upper_case_acronyms)] #[derive(Clone, Copy, Debug, Deserialize, Eq, Ord, PartialEq, PartialOrd, Serialize)] - pub(crate) enum Resolution { + pub enum Resolution { $( #[doc = concat!(stringify!($width), "x", stringify!($height))] $res @@ -28,29 +28,29 @@ macro_rules! resolutions { }; impl Resolution { - pub(crate) fn values() -> [Self; NUM_RESOLUTIONS] { + pub fn values() -> [Self; NUM_RESOLUTIONS] { [$(Self::$res),+] } - pub(crate) fn width(self) -> usize { + pub fn width(self) -> usize { match self { $(Self::$res => $width),+ } } - pub(crate) fn height(self) -> usize { + pub fn height(self) -> usize { match self { $(Self::$res => $height),+ } } - pub(crate) fn bitrate(self) -> u64 { + pub fn bitrate(self) -> u64 { match self { $(Self::$res => $bitrate),+ } } - pub(crate) fn format(self) -> FfmpegOutputFormat { + pub fn format(self) -> FfmpegOutputFormat { match self { $(Self::$res => FfmpegOutputFormat::$format),+ } @@ -80,85 +80,85 @@ resolutions! { } #[derive(Deserialize, Serialize)] -pub(crate) struct Project { - pub(crate) lecture: ProjectLecture, - pub(crate) source: ProjectSource, - pub(crate) progress: ProjectProgress +pub struct Project { + pub lecture: ProjectLecture, + pub source: ProjectSource, + pub progress: ProjectProgress } #[serde_as] #[derive(Deserialize, Serialize)] -pub(crate) struct ProjectLecture { - pub(crate) course: String, - pub(crate) label: String, - pub(crate) docent: String, +pub struct ProjectLecture { + pub course: String, + pub label: String, + pub docent: String, #[serde_as(as = "DisplayFromStr")] - pub(crate) date: Date, + pub date: Date, #[serde(default = "Default::default")] #[serde_as(as = "DisplayFromStr")] - pub(crate) lang: Language<'static> + pub lang: Language<'static> } #[serde_as] #[derive(Deserialize, Serialize)] -pub(crate) struct ProjectSource { - pub(crate) files: Vec, - pub(crate) stereo: bool, +pub struct ProjectSource { + pub files: Vec, + pub stereo: bool, #[serde_as(as = "Option")] - pub(crate) start: Option