Skip to content

Commit

Permalink
feat(#41): latex.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
h1alexbel committed Jun 24, 2024
1 parent f8bde4e commit 8bcbdcd
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/target
/out
### Rust template
# Generated by Cargo
# will have compiled files and executables
Expand Down
6 changes: 6 additions & 0 deletions cli/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ use clap::Parser;
// We should process the port argument and
// pass it to the server on `start` command.
// Start command should be added also with clap
// @todo #41:15min Add --report argument together with content type flags.
// Together with --report option let's add flags for XML, TeX, and Text
// reports. So user can generate report in many formats like this: --report
// --tex --html.
// @todo #41:45min Parse content type flag from stdin. Let's pick report generation
// function based on provided flags, such as: --xml, --tex, --txt.
#[derive(Parser, Debug)]
pub(crate) struct Args {
/// The port to run
Expand Down
7 changes: 7 additions & 0 deletions server/resources/report.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
\usepackage{to-be-determined}
\documentclass[12pt]{article}
\begin{document}

\section*{Fakehub report}
\tbd{History: TBD}
\end{document}
1 change: 1 addition & 0 deletions server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use crate::xml::storage::touch_storage;

mod routes;
mod xml;
pub mod report;

#[derive(Default)]
pub struct Server {
Expand Down
92 changes: 92 additions & 0 deletions server/src/report/latex.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// The MIT License (MIT)
//
// Copyright (c) 2024 Aliaksei Bialiauski
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
use std::fs;
use std::path::Path;

///
/// Read LaTeX template.
/// @todo #41:60min Add function for appending new content into the template.
/// We need to create new function that will append input into the template,
/// thus it will build a detailed report.
/// # Arguments
///
/// * `path`: Template path
///
/// returns: String
///
/// # Examples
///
/// ```
/// use crate::server::report::latex::template;
/// let content = template(None);
/// print!("{content}")
/// ```
pub fn template(path: Option<&str>) -> String {
return fs::read_to_string(
Path::new(path.unwrap_or("resources/report.tex"))
).unwrap();
}

#[cfg(test)]
mod tests {
use std::fs::File;
use std::io::Write;
use anyhow::Result;
use tempdir::TempDir;

use crate::report::latex::template;

#[test]
fn returns_default_template() -> Result<()> {
let content = template(None);
let expected = r"\usepackage{to-be-determined}
\documentclass[12pt]{article}
\begin{document}
\section*{Fakehub report}
\tbd{History: TBD}
\end{document}
";
assert_eq!(
content,
expected,
"Template content '{content}' does not match with '{expected}'"
);
Ok(())
}

#[test]
fn returns_custom_template() -> Result<()> {
let temp = TempDir::new("temp")?;
let path = &temp.path().join("custom.tex");
let expected = "$This is custom template!$";
let bytes = expected.to_string().into_bytes();
File::create(path).unwrap().write_all(bytes.as_slice())?;
let content = template(path.to_str());
assert_eq!(
content,
expected,
"Template content '{content} does not match with '{expected}'"
);
Ok(())
}
}
25 changes: 25 additions & 0 deletions server/src/report/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// The MIT License (MIT)
//
// Copyright (c) 2024 Aliaksei Bialiauski
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// @todo #41:45min Create similar to latex.rs functions for XML and TXT formats.
// Let's create similar to latex.rs functions for generating reports in XML and TXT.
// Don't forget to remove this puzzle.
pub mod latex;

0 comments on commit 8bcbdcd

Please sign in to comment.