rustyphoenixlecture 1.10.0

This project aims to provide a simple a powerfull lecture compilation to generate html web sites
/***************************************
	Auteur : Pierre Aubert
	Mail : pierre.aubert@lapp.in2p3.fr
	Licence : CeCILL-C
****************************************/

use std::path::PathBuf;

use crate::pstrbuffer::{phoenix_create_output_test_dir, PStrBuffer};

///Convert an input time (HH:MM) into a calendar time
/// # Parameters
/// - `date` : date to be conveted
/// - `input_time` : time to be converted
/// # Returns
/// Corresponding calendar time
fn phoenix_time_to_calendar_time(date: &String, input_time: &String) -> String{
	let str_calendar: String = String::from(format!("{}T{}", date.replace("-", ""), input_time.replace(":", "")));
	return str_calendar;
}

///Event of an invitation
#[derive(Debug, Clone, PartialEq)]
pub struct PEvent{
	///Title of the event
	p_title: String,
	///Invitation of the current event
	p_invitation: String,
	///Date of the event (YYMMDD)
	p_date: String,
	///Time when the event begins
	p_begin_time: String,
	///Time when the event ends
	p_end_time: String,
	///Location of the event
	p_location: String,
	///Id of the current PEvent
	p_id: usize,
	///Current time stamp when this PEvent is created
	p_current_time_stamp: String,
}

impl PEvent {
	///Constructor of the PEvent
	/// # Parameters
	/// - `title` : title of the PEvent
	/// - `invitation` : invitation of the current PEvent
	/// - `date` : date of the PEvent
	/// - `begin_time` : time when the event begins
	/// - `end_time` : time when the event ends
	/// - `location` : location of the PEvent
	/// - `id` : id of the current PEvent
	/// - `current_time_stamp` : current time stamp when this PEvent is created
	/// # Retunrs
	/// Initialised PEvent
	pub fn new(title: &String, invitation: &String, date: &String, begin_time: &String, end_time: &String, location: &String, id: usize, current_time_stamp: &String) -> Self{
		PEvent {
			p_title: title.clone(),
			p_invitation: invitation.clone(),
			p_date: date.clone(),
			p_begin_time: begin_time.clone(),
			p_end_time: end_time.clone(),
			p_location: location.clone(),
			p_id: id,
			p_current_time_stamp: current_time_stamp.clone(),
		}
	}
	///Write the PEvent in a file buffer
	/// # Parameters
	/// - `buffer` : PStrBuffer to be used
	/// - `main_url` : main url of the website this calendar is from
	pub fn write(&self, buffer: &mut PStrBuffer, main_url: &String){
		buffer.write(&String::from("BEGIN:VEVENT\n"));
		buffer.write(&String::from("CLASS:PUBLIC\n"));
		buffer.write(&String::from(format!("DTSTAMP:{}\n", self.p_current_time_stamp)));
		buffer.write(&String::from(format!("UID:TH8WMR_PNR{:04}_{}\n", self.p_id, self.p_current_time_stamp)));
		buffer.write(&String::from(format!("DTSTART;TZID=Europe/Paris:{}\n", phoenix_time_to_calendar_time(&self.p_date, &self.p_begin_time))));
		buffer.write(&String::from(format!("DTEND;TZID=Europe/Paris:{}\n", phoenix_time_to_calendar_time(&self.p_date, &self.p_end_time))));
		buffer.write(&String::from(format!("SUMMARY:{}\n", self.p_title)));
		buffer.write(&String::from(format!("LOCATION:{}\n", self.p_location)));
		if main_url.is_empty() {
			buffer.write(&String::from(format!("DESCRIPTION:{}\n", self.p_title)));
		}else{
			buffer.write(&String::from(format!("DESCRIPTION:{} <a href=\"{}redirect.html?label=sec_{}\">web site</a>\n", self.p_title, main_url, self.p_invitation)));
		}
		buffer.write(&String::from("BEGIN:VALARM\n"));
		buffer.write(&String::from("TRIGGER:-PT10M\n"));
		buffer.write(&String::from("ACTION:DISPLAY\n"));
		buffer.write(&String::from("DESCRIPTION:Reminder\n"));
		buffer.write(&String::from("END:VALARM\n"));
		buffer.write(&String::from("END:VEVENT\n"));
	}
}

///Manage an invitation
#[derive(Debug, Clone, PartialEq)]
pub struct PInvitation{
	///Name of the invitation (name of the .ics)
	p_name: String,
	///Vector of all events of the invitation
	p_vec_event: Vec<PEvent>,
}

impl PInvitation {
	///Constructor of a PInvitation
	/// # Parameters
	/// - `invitation` : name of the invitation of the PInvitation
	/// # Returns
	/// Initialised PInvitation
	pub fn new(invitation: &String) -> Self{
		PInvitation {
			p_name: invitation.clone(),
			p_vec_event: Default::default(),
		}
	}
	///Add a PEvent in the PInvitation
	/// # Parameters
	/// - `event` : PEvent to be added into the PInvitation
	pub fn add_event(&mut self, event: &PEvent){
		self.p_vec_event.push(event.clone());
	}
	///Write the invitation
	/// # Parameters
	/// - `output_dir` : output directory to write the invitation
	/// - `main_url` : main url of the website this calendar is from
	pub fn write(&self, output_dir: &PathBuf, main_url: &String){
		let output_file: PathBuf = phoenix_create_output_test_dir(output_dir, &PathBuf::from(String::from(format!("{}.ics", self.p_name))));
		let mut buffer = PStrBuffer::new(&output_file);
		buffer.write(&String::from("BEGIN:VCALENDAR\n"));
		buffer.write(&String::from("VERSION:2.0\n"));
		buffer.write(&String::from(format!("PRODID:-//RustyPhoenixLecture//{}/\n", self.p_name)));
		for event in self.p_vec_event.iter() {
			event.write(&mut buffer, main_url);
		}
		buffer.write(&String::from("END:VCALENDAR\n\n"));
	}
}


#[cfg(test)]
mod tests{
	use super::*;
	
	///Test the phoenix_time_to_calendar_time
	#[test]
	fn test_invitation_time_conversion(){
		assert_eq!(phoenix_time_to_calendar_time(&String::from("20260622"), &String::from("140000")), String::from("20260622T140000"));
		assert_eq!(phoenix_time_to_calendar_time(&String::from("2026-06-22"), &String::from("14:00:00")), String::from("20260622T140000"));
	}
	///Test a PInvitation
	#[test]
	fn test_pinvitation(){
		let mut invitation = PInvitation::new(&String::from("some_invitation"));
		let event = PEvent::new(&String::from("Some event"), &String::from("some_invitation"),
			&String::from("2026-06-07"), &String::from("09:00"), &String::from("10:00"), &String::from("somewhere"),
			1, &String::from("20260622T140000"));
		invitation.add_event(&event);
		
		invitation.write(&PathBuf::from("target/test_pinvitation"), &String::from("https://some/main/url"));
		invitation.p_name = String::from("no_url_invitation");
		invitation.write(&PathBuf::from("target/test_pinvitation"), &String::from(""));
		
	}
}