Skip to content

Commit

Permalink
It works ™️
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaacMarovitz committed Nov 3, 2023
1 parent a85427b commit 732dbbc
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 117 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/target
.DS_Store
28 changes: 28 additions & 0 deletions src/header.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
pub use crate::header::types::{EnhancedSync, WhiskyVersion, WinVersion};
use iso8601::Date;
use std::fmt::{Display, Formatter};

pub mod types;

#[derive(Default)]
pub struct LogHeader {
pub whisky_version: WhiskyVersion,
pub date: Date,
pub macos_version: WhiskyVersion,
pub bottle_name: String,
pub bottle_url: String,
pub wine_version: WhiskyVersion,
pub windows_version: WinVersion,
pub enhanced_sync: EnhancedSync,
pub metal_hud: bool,
pub metal_trace: bool,
pub arguments: String,
}

impl Display for LogHeader {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "Whisky Version: {}\n", self.whisky_version.0)?;
write!(f, "Date: {}\n", self.date)?;
write!(f, "macOS Version: {}\n", self.macos_version.0)
}
}
86 changes: 86 additions & 0 deletions src/header/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
use std::fmt::{Display, Formatter};
use semver::{BuildMetadata, Prerelease, Version};
use std::str::FromStr;

#[derive(Default)]
pub enum WinVersion {
WinXP,
Win7,
Win8,
Wine81,
#[default]
Win10,
}

impl Display for WinVersion {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
WinVersion::WinXP => write!(f, "Windows XP"),
WinVersion::Win7 => write!(f, "Windows 7"),
WinVersion::Win8 => write!(f, "Windows 8"),
WinVersion::Wine81 => write!(f, "Windows 8.1"),
WinVersion::Win10 => write!(f, "Windows 10"),
}
}
}

impl FromStr for WinVersion {
type Err = ();

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"winxp64" => Ok(WinVersion::WinXP),
"win7" => Ok(WinVersion::Win7),
"win8" => Ok(WinVersion::Win8),
"win81" => Ok(WinVersion::Wine81),
"win10" => Ok(WinVersion::Win10),
_ => Err(()),
}
}
}

#[derive(Default)]
pub enum EnhancedSync {
None,
ESync,
#[default]
MSync,
}

impl Display for EnhancedSync {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
EnhancedSync::None => write!(f, "None"),
EnhancedSync::ESync => write!(f, "ESync"),
EnhancedSync::MSync => write!(f, "MSync"),
}
}
}

impl FromStr for EnhancedSync {
type Err = ();

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"none" => Ok(EnhancedSync::None),
"esync" => Ok(EnhancedSync::ESync),
"msync" => Ok(EnhancedSync::MSync),
_ => Err(()),
}
}
}

pub struct WhiskyVersion(pub Version);
impl Default for WhiskyVersion {
fn default() -> Self {
return WhiskyVersion {
0: Version {
major: 0,
minor: 0,
patch: 0,
pre: Prerelease::EMPTY,
build: BuildMetadata::EMPTY,
},
};
}
}
177 changes: 60 additions & 117 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use iso8601::date;
use semver::Version;
use serenity::model::prelude::*;
use serenity::prelude::*;
use serenity::Client;
use std::env;
use std::default::Default;
use std::fmt::{Display, Formatter, write};
use std::env;
use std::str;
use std::str::FromStr;
use semver::{Version, BuildMetadata, Prerelease};
use iso8601::{date, Date};

pub mod header;

struct Handler;

Expand All @@ -19,8 +20,25 @@ impl EventHandler for Handler {
let filename = attachment.filename.clone();

if content_type == "text/plain; charset=utf-8" && filename.ends_with(".log") {
let _ = msg.channel_id.say(&context, "Found a log! Parsing...").await;
parse_log(attachment.download().await.expect("Failed to download log")).await;
let header = parse_log(attachment.download().await.expect("Failed to download log")).await;

msg.channel_id.send_message(&context, |m| {
m.embed(|e| e
.color(0xC86432)
.field("Whisky Version", header.whisky_version.0, true)
.field("Date", header.date.to_owned(), true)
.field("macOS Version", header.macos_version.0, true)
.field("Wine Version", header.wine_version.0, true)
.field("Windows Version", header.windows_version, true)
.field("Enhanced Sync", header.enhanced_sync, true)
.field("Bottle Name", header.bottle_name.clone(), false)
.field("Bottle URL", format!("`{}`", header.bottle_url), false)
.field("Arguments", format!("`{}`", header.arguments), false)
.footer(|f| {
f.text(format!("Log uploaded by @{}", msg.author.name));
f
}))
}).await.expect("Whee");
}
}

Expand All @@ -29,7 +47,7 @@ impl EventHandler for Handler {
}
}

async fn ready(&self, context: Context, ready: Ready) {
async fn ready(&self, _: Context, ready: Ready) {
println!("{} is connected!", ready.user.name);
// let _ = ChannelId(1115961098961702992).say(&context, "Its alive!").await;
}
Expand All @@ -43,100 +61,20 @@ async fn main() {
| GatewayIntents::DIRECT_MESSAGES
| GatewayIntents::MESSAGE_CONTENT;

let mut client = Client::builder(token, intents).event_handler(Handler).await.expect("Failed to create client");
let mut client = Client::builder(token, intents)
.event_handler(Handler)
.await
.expect("Failed to create client");

client.start().await.expect("Failed to start client");
}

#[derive(Default)]
enum WinVersion {
WinXP,
Win7,
Win8,
Wine81,
#[default]
Win10
}

impl FromStr for WinVersion {
type Err = ();

fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
match s {
"winxp64" => Ok(WinVersion::WinXP),
"win7" => Ok(WinVersion::Win7),
"win8" => Ok(WinVersion::Win8),
"win81" => Ok(WinVersion::Wine81),
"win10" => Ok(WinVersion::Win10),
_ => Err(()),
}
}
}

#[derive(Default)]
enum EnhancedSync {
None,
ESync,
#[default]
MSync
}

impl FromStr for EnhancedSync {
type Err = ();

fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
match s {
"none" => Ok(EnhancedSync::None),
"esync" => Ok(EnhancedSync::ESync),
"msync" => Ok(EnhancedSync::MSync),
_ => Err(()),
}
}
}

#[derive(Default)]
struct LogHeader<'a> {
whisky_version: WhiskyVersion,
date: Date,
macos_version: WhiskyVersion,
bottle_name: &'a str,
bottle_url: &'a str,
wine_version: WhiskyVersion,
windows_version: WinVersion,
enhanced_sync: EnhancedSync,
metal_hud: bool,
metal_trace: bool,
arguments: &'a str
}

impl<'a> Display for LogHeader<'a> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "Whisky Version: {}\n", self.whisky_version.0)?;
write!(f, "Date: {}\n", self.date)?;
write!(f, "macOS Version: {}\n", self.macos_version.0)
}
}

struct WhiskyVersion(Version);
impl Default for WhiskyVersion {
fn default() -> Self {
return WhiskyVersion {
0: Version {
major: 0,
minor: 0,
patch: 0,
pre: Prerelease::EMPTY,
build: BuildMetadata::EMPTY
},
};
}
}

async fn parse_log(log_data: Vec<u8>) {
// TODO: Don't use a Box for this
async fn parse_log(log_data: Vec<u8>) -> Box<header::LogHeader> {
let log_string = str::from_utf8(log_data.as_slice()).expect("Failed to get log as array");
let mut log_header: LogHeader = Default::default();
let mut log_header: header::LogHeader = Default::default();

for line in log_string.lines() {
for line in log_string.lines() {
if line.is_empty() {
continue;
}
Expand All @@ -145,42 +83,47 @@ async fn parse_log(log_data: Vec<u8>) {
let (key, value) = line.split_once(":").unwrap();
match key.trim() {
"Whisky Version" => {
log_header.whisky_version.0 = Version::parse(value.trim()).expect("Failed to parse version");
},
log_header.whisky_version.0 =
Version::parse(value.trim()).expect("Failed to parse version");
}
"Date" => {
log_header.date = date(value.trim()).unwrap();
},
}
"macOS Version" => {
log_header.macos_version.0 = Version::parse(value.trim()).expect("Failed to parse version");
},
log_header.macos_version.0 =
Version::parse(value.trim()).expect("Failed to parse version");
}
"Bottle Name" => {
log_header.bottle_name = value.trim();
},
log_header.bottle_name = value.trim().to_string();
}
"Bottle URL" => {
log_header.bottle_url = value.trim();
},
log_header.bottle_url = value.trim().to_string();
}
"Wine Version" => {
log_header.wine_version.0 = Version::parse(value.trim()).expect("Failed to parse version");
},
log_header.wine_version.0 =
Version::parse(value.trim()).expect("Failed to parse version");
}
"Windows Version" => {
log_header.windows_version = WinVersion::from_str(value.trim()).unwrap();
},
log_header.windows_version =
header::WinVersion::from_str(value.trim()).unwrap();
}
"Enhanced Sync" => {
log_header.enhanced_sync = EnhancedSync::from_str(value.trim()).unwrap();
},
log_header.enhanced_sync =
header::EnhancedSync::from_str(value.trim()).unwrap();
}
"Metal HUD" => {
log_header.metal_hud = value.trim().parse().unwrap();
},
}
"Metal Trace" => {
log_header.metal_trace = value.trim().parse().unwrap();
},
}
"Arguments" => {
log_header.arguments = value.trim();
},
_ => println!("Don't care")
log_header.arguments = value.trim().to_string();
}
_ => println!("Don't care"),
}
}
}

println!("{}", log_header);
}
return Box::from(log_header);
}

0 comments on commit 732dbbc

Please sign in to comment.