Skip to content

Commit

Permalink
Merge pull request #11 from TornaxO7/fixing_json_output_html
Browse files Browse the repository at this point in the history
Fixing json output html
  • Loading branch information
TornaxO7 authored Aug 26, 2021
2 parents 04d1c76 + b79705e commit e424fe9
Show file tree
Hide file tree
Showing 4 changed files with 195 additions and 126 deletions.
1 change: 0 additions & 1 deletion src/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ impl<'a> Ctx<'a> {
mbox: S,
arg_matches: clap::ArgMatches<'a>,
) -> Self {

let mbox = mbox.to_string();

Self {
Expand Down
173 changes: 105 additions & 68 deletions src/msg/body.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use error_chain::error_chain;

use std::fmt;
use std::ops::{Deref, DerefMut};

use serde::Serialize;

// == Macros ==
error_chain! {
foreign_links {
ParseContentType(lettre::message::header::ContentTypeErr);
ParseContentType(lettre::message::header::ContentTypeErr);
}
}

Expand All @@ -24,91 +23,129 @@ error_chain! {
///
/// This part of the msg/msg would be stored in this struct.
#[derive(Clone, Serialize, Debug, PartialEq, Eq)]
pub struct Body(String);
pub struct Body {
/// The text version of a body (if available)
pub text: Option<String>,

/// The html version of a body (if available)
pub html: Option<String>,
}

impl Body {
/// This function just returns a clone of it's content. If we use the
/// example from above, than you'll get a clone of the whole text.
/// Returns a new instance of `Body` without any attributes set. (Same as `Body::default()`)
///
/// # Example
/// ```
/// # use himalaya::msg::body::Body;
/// # fn main() {
/// let body = concat![
/// "Dear Mr. Boss,\n",
/// "I like rust. It's an awesome language. *Change my mind*....\n",
/// "\n",
/// "Sincerely",
/// ];
/// ```rust
/// use himalaya::msg::body::Body;
///
/// // create a new instance of `Body`
/// let body_struct = Body::from(body);
/// fn main() {
/// let body = Body::new();
///
/// assert_eq!(body_struct.get_content(), body);
/// # }
/// let expected_body = Body {
/// text: None,
/// html: None,
/// };
///
/// assert_eq!(body, expected_body);
/// }
/// ```
pub fn get_content(&self) -> String {
self.0.clone()
pub fn new() -> Self {
Self::default()
}
}

// == Traits ==
impl Default for Body {
fn default() -> Self {
Self(String::new())
/// Returns a new instance of `Body` with `text` set.
///
/// # Example
/// ```rust
/// use himalaya::msg::body::Body;
///
/// fn main() {
/// let body = Body::new_with_text("Text body");
///
/// let expected_body = Body {
/// text: Some("Text body".to_string()),
/// html: None,
/// };
///
/// assert_eq!(body, expected_body);
/// }
/// ```
pub fn new_with_text<S: ToString>(text: S) -> Self {
Self {
text: Some(text.to_string()),
html: None,
}
}
}

impl fmt::Display for Body {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "{}", &self.0)
/// Returns a new instance of `Body` with `html` set.
///
/// # Example
/// ```rust
/// use himalaya::msg::body::Body;
///
/// fn main() {
/// let body = Body::new_with_html("Html body");
///
/// let expected_body = Body {
/// text: None,
/// html: Some("Html body".to_string()),
/// };
///
/// assert_eq!(body, expected_body);
/// }
/// ```
pub fn new_with_html<S: ToString>(html: S) -> Self {
Self {
text: None,
html: Some(html.to_string()),
}
}
}

impl Deref for Body {
type Target = String;

fn deref(&self) -> &Self::Target {
&self.0
/// Returns a new isntance of `Body` with `text` and `html` set.
///
/// # Example
/// ```rust
/// use himalaya::msg::body::Body;
///
/// fn main() {
/// let body = Body::new_with_both("Text body", "Html body");
///
/// let expected_body = Body {
/// text: Some("Text body".to_string()),
/// html: Some("Html body".to_string()),
/// };
///
/// assert_eq!(body, expected_body);
/// }
/// ```
pub fn new_with_both<S: ToString>(text: S, html: S) -> Self {
Self {
text: Some(text.to_string()),
html: Some(html.to_string()),
}
}
}

impl DerefMut for Body {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
// == Traits ==
impl Default for Body {
fn default() -> Self {
Self {
text: None,
html: None,
}
}
}

// -- From's --
/// Give in a `&str` to create a new instance of `Body`.
///
/// # Example
/// ```
/// # use himalaya::msg::body::Body;
/// # fn main() {
/// Body::from("An awesome string!");
/// # }
/// ```
impl From<&str> for Body {
fn from(string: &str) -> Self {
Self(string.to_string())
}
}
impl fmt::Display for Body {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
let content = if let Some(text) = self.text.clone() {
text
} else if let Some(html) = self.html.clone() {
html
} else {
String::new()
};

/// Give in a [`String`] to create a new instance of `Body`.
///
/// # Example
/// ```
/// # use himalaya::msg::body::Body;
/// # fn main() {
/// let body_content = String::from("A awesome content.");
/// Body::from(body_content);
/// # }
/// ```
///
/// [`String`]: https://doc.rust-lang.org/std/string/struct.String.html
impl From<String> for Body {
fn from(string: String) -> Self {
Self(string)
write!(formatter, "{}", content)
}
}
15 changes: 7 additions & 8 deletions src/msg/cli.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::body::Body;
use super::envelope::Envelope;
use super::model::{Msg, Msgs};
use super::model::{Msg, Msgs, MsgSerialized};
use url::Url;

use atty::Stream;
Expand Down Expand Up @@ -348,9 +348,8 @@ fn msg_matches_read(ctx: &Ctx, matches: &clap::ArgMatches) -> Result<bool> {
if raw {
ctx.output.print(msg.get_raw()?);
} else {
ctx.output.print(msg.body);
ctx.output.print(MsgSerialized::from(&msg));
}

imap_conn.logout();
Ok(true)
}
Expand Down Expand Up @@ -503,7 +502,7 @@ pub fn msg_matches_mailto(ctx: &Ctx, url: &Url) -> Result<()> {
};

let mut msg = Msg::new_with_envelope(&ctx, envelope);
msg.body = Body::from(body.as_ref());
msg.body = Body::new_with_text(body);
msg_interaction(&ctx, &mut msg, &mut imap_conn)?;

imap_conn.logout();
Expand Down Expand Up @@ -753,7 +752,7 @@ fn override_msg_with_args(msg: &mut Msg, matches: &clap::ArgMatches) {
}
};

let body = Body::from(body);
let body = Body::new_with_text(body);

// -- Creating and printing --
let envelope = Envelope {
Expand All @@ -779,7 +778,7 @@ fn tpl_matches_new(ctx: &Ctx, matches: &clap::ArgMatches) -> Result<bool> {
override_msg_with_args(&mut msg, &matches);

trace!("Message: {:?}", msg);
ctx.output.print(msg);
ctx.output.print(MsgSerialized::from(&msg));

Ok(true)
}
Expand All @@ -797,7 +796,7 @@ fn tpl_matches_reply(ctx: &Ctx, matches: &clap::ArgMatches) -> Result<bool> {

override_msg_with_args(&mut msg, &matches);
trace!("Message: {:?}", msg);
ctx.output.print(msg);
ctx.output.print(MsgSerialized::from(&msg));

Ok(true)
}
Expand All @@ -815,7 +814,7 @@ fn tpl_matches_forward(ctx: &Ctx, matches: &clap::ArgMatches) -> Result<bool> {
override_msg_with_args(&mut msg, &matches);

trace!("Message: {:?}", msg);
ctx.output.print(msg);
ctx.output.print(MsgSerialized::from(&msg));

Ok(true)
}
Expand Down
Loading

0 comments on commit e424fe9

Please sign in to comment.