Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing json output html #11

Merged
merged 9 commits into from
Aug 26, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
174 changes: 106 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,130 @@ 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
/// ```rust
/// use himalaya::msg::body::Body;
///
/// fn main() {
/// let body = Body::new();
///
/// let expected_body = Body {
/// text: None,
/// html: None,
/// };
///
/// assert_eq!(body, expected_body);
/// }
/// ```
pub fn new() -> Self {
Self::default()
}

/// 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);
/// }
/// ```
/// # 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",
/// ];
pub fn new_with_text<S: ToString>(text: S) -> Self {
Self {
text: Some(text.to_string()),
html: None,
}
}

/// Returns a new instance of `Body` with `html` set.
///
/// // create a new instance of `Body`
/// let body_struct = Body::from(body);
/// # 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_struct.get_content(), body);
/// # }
/// assert_eq!(body, expected_body);
/// }
/// ```
pub fn get_content(&self) -> String {
self.0.clone()
pub fn new_with_html<S: ToString>(html: S) -> Self {
Self {
text: None,
html: Some(html.to_string()),
}
}

/// 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()),
}
}
}

// == Traits ==
impl Default for Body {
fn default() -> Self {
Self(String::new())
Self {
text: None,
html: None,
}
}
}

impl fmt::Display for Body {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "{}", &self.0)
}
}

impl Deref for Body {
type Target = String;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl DerefMut for Body {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
let content = if let Some(text) = self.text.clone() {
soywod marked this conversation as resolved.
Show resolved Hide resolved
text
} else if let Some(html) = self.html.clone() {
html
} else {
String::new()
};

// -- 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())
}
}

/// 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)
}
}
10 changes: 7 additions & 3 deletions src/msg/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,11 @@ 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);
if ctx.output.is_json() {
ctx.output.print(msg);
} else {
ctx.output.print(msg.body);
}
}

imap_conn.logout();
Expand Down Expand Up @@ -503,7 +507,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 +757,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 Down
Loading