use serde::Deserialize;
use serde_json::Value;
#[derive(Clone, Debug)]
pub enum Event {
Edit(EditEvent),
Log(LogEvent),
}
#[derive(Clone, Debug, Deserialize)]
pub struct EditEvent {
#[serde(rename = "$schema")]
schema: String,
meta: EventMeta,
pub id: u32,
#[serde(rename = "type")]
type_: String,
pub namespace: i32,
pub title: String,
pub comment: String,
pub parsedcomment: String,
pub timestamp: u32,
pub user: String,
pub bot: bool,
minor: Option<bool>,
patrolled: Option<bool>,
pub length: EventLength,
pub revision: EventRevision,
pub server_url: String,
pub server_name: String,
pub server_script_path: String,
pub wiki: String,
}
impl EditEvent {
pub fn is_minor(&self) -> bool {
self.minor.unwrap_or(false)
}
pub fn is_patrolled(&self) -> bool {
self.patrolled.unwrap_or(false)
}
fn endpoint(&self, path: &str) -> String {
format!(
"{}{}/{}.php",
self.server_url, self.server_script_path, path
)
}
pub fn api_url(&self) -> String {
self.endpoint("api")
}
fn title_for_url(&self) -> String {
self.title.replace(" ", "_")
}
pub fn diff_url(&self) -> String {
format!(
"{}?title={}&diff={}",
self.endpoint("index"),
self.title_for_url(),
self.revision.new
)
}
pub fn short_diff_url(&self) -> String {
format!("{}?diff={}", self.server_url, self.revision.new)
}
}
#[derive(Clone, Debug, Deserialize)]
pub struct LogEvent {
#[serde(rename = "$schema")]
schema: String,
meta: EventMeta,
#[serde(rename = "type")]
type_: String,
pub namespace: i32,
pub title: String,
pub comment: String,
pub parsedcomment: String,
pub timestamp: u32,
pub user: String,
pub bot: bool,
pub log_id: u32,
pub log_type: String,
pub log_action: String,
pub log_params: Value,
pub log_action_comment: String,
pub server_url: String,
pub server_name: String,
pub server_script_path: String,
pub wiki: String,
}
impl LogEvent {
pub fn api_url(&self) -> String {
format!("{}{}/api.php", self.server_url, self.server_script_path)
}
}
#[derive(Clone, Debug, Deserialize)]
pub struct EventLength {
pub old: Option<u32>,
pub new: u32,
}
#[derive(Clone, Debug, Deserialize)]
pub struct EventRevision {
pub old: Option<u32>,
pub new: u32,
}
#[derive(Clone, Debug, Deserialize)]
struct EventMeta {
uri: String,
request_id: String,
id: String,
dt: String,
domain: String,
stream: String,
topic: String,
partition: u32,
offset: u32,
}