Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
168 commits
Select commit Hold shift + click to select a range
1f81738
Optimize code (#249)
mxsrm Dec 17, 2024
48b864b
style: enable more lints
mxsrm Dec 21, 2024
a317cce
refactor(crypt): replace `getrandom` with `rand` for random byte gene…
mxsrm Dec 21, 2024
30e3e9a
refactor: reimplement and cleanup `helper/crypt.rs`
mxsrm Dec 21, 2024
71f0572
refactor: fix typos, clippy lints
mxsrm Dec 21, 2024
f3de769
refactor(helper/coordinate.rs): remove lazy_static, add comments
mxsrm Dec 21, 2024
aaaec5f
refactor: replace `lazy_static` with built-in `std::sync::OnceLock`
mxsrm Dec 21, 2024
714bf07
refactor: replace `ThinVec` with rust-lang `Vec`
mxsrm Dec 21, 2024
cb80120
refactor(worksheet): rename cell_collection to cells, row_dimensions …
mxsrm Dec 21, 2024
12926d6
refactor(deps): replace ahash with std::hash::DefaultHasher
mxsrm Dec 22, 2024
b65b035
refactor(crypt): replace OsRng with thread_rng for random byte genera…
mxsrm Dec 22, 2024
739446e
refactor(tests): replace hex decoding/encoding with hex-literal
mxsrm Dec 22, 2024
517cf8c
chore(deps): sort dependencies in Cargo.toml
mxsrm Dec 22, 2024
6b2c88e
chore(lints): move lint configurations to lib.rs
mxsrm Dec 22, 2024
9511418
refactor: replace `is_none_or` with match statements
mxsrm Dec 22, 2024
f76be7a
chore: set minimum supported Rust version to 1.79.0
mxsrm Dec 22, 2024
995f40d
chore: remove unused `js` feature from Cargo.toml
mxsrm Dec 22, 2024
92a1af9
feat(helper/date): enhance Excel timestamp conversion with additional…
mxsrm Dec 22, 2024
b1916f7
refactor(helper/crypt): abstract random byte generation with macro an…
mxsrm Dec 22, 2024
98fcf7f
style: add modern rustfmt.toml and apply it
mxsrm Dec 22, 2024
26e7021
refactor(helper/crypt): centralize constants into a dedicated module
mxsrm Dec 22, 2024
4dcea0e
refactor: remove leading underscores from method names and allow dead…
mxsrm Dec 22, 2024
72faf4f
refactor(writer/xlsx): simplify make_buffer function and improve work…
mxsrm Dec 22, 2024
f8d9659
docs(helper/crypt): enhance documentation for encryption-related func…
mxsrm Dec 22, 2024
8d41bc1
refactor: clippy lints
mxsrm Dec 22, 2024
de9f38f
chore(clippy): allow multiple versions of `thiserror`
mxsrm Dec 22, 2024
37d1139
chore(clippy): add categories to Cargo.toml
mxsrm Dec 22, 2024
505d766
refactor(writer/xlsx): remove unnecessary qualification
mxsrm Dec 22, 2024
4eab5d7
refactor(helper/address): update is_address function for regex initia…
mxsrm Dec 22, 2024
ea1d8a2
chore(deps): update zip dependency to version 2.2.2
mxsrm Dec 22, 2024
6b3c774
style: fix formatting and improve check_sheet_name logic
mxsrm Dec 22, 2024
e60fa40
style(clippy): fix many clippy lints
mxsrm Dec 22, 2024
0ba6df7
feat(helper/binary): refactor binary data handling and improve error …
mxsrm Dec 22, 2024
6e5e819
style: update rustfmt.toml to use `imports_granularity = "Crate"`
mxsrm Dec 22, 2024
b04a004
refactor(crypt): split large module into smaller submodules for impro…
mxsrm Dec 23, 2024
21e85d3
style: rustfmt
mxsrm Dec 23, 2024
1a93e48
style(rustfmt): use_small_heuristics = Default
mxsrm Dec 23, 2024
95edb1d
docs: add documentation for `helper/crypt/utils_other.rs`
mxsrm Dec 23, 2024
a022f93
refactor(helper/crypt/utils_algo): optimize build_encryption_info fn
mxsrm Dec 23, 2024
4ee3a1c
style(rustfmt): update rustfmt
mxsrm Dec 23, 2024
f696c55
refactor: rename Spreadsheet structures to Workbook for MS-XLSX compl…
mxsrm Dec 23, 2024
a352d70
refactor(helper/crypt): rename sub-modules
mxsrm Dec 23, 2024
a8ff796
docs(helper/crypt): add rustdoc for modules
mxsrm Dec 23, 2024
a4239e9
style(helper/const_str): use macro to clear up code
mxsrm Dec 23, 2024
4c7b43b
style(helper/const_str): sort entries
mxsrm Dec 23, 2024
6caa4d5
test: re-enable previously disabled test cases in helper/crypt
mxsrm Dec 23, 2024
1f239db
test(helper/crypt): update verifier hash test vectors
mxsrm Dec 23, 2024
a476696
test(helper/crypt): add test data `build_encryption_info`
mxsrm Dec 23, 2024
c147f09
test(helper/crypt): remove superfluous test
mxsrm Dec 24, 2024
3d451d5
test(helper/crypt): split large test into multiple smaller ones
mxsrm Dec 24, 2024
9aab88e
refactor(helper/formula): introduce token! macro and refactor token p…
mxsrm Dec 24, 2024
240141e
refactor(helper/crypt): update random byte generation macro
mxsrm Dec 24, 2024
87a6e3b
refactor(helper/binary.rs): remove redundant function and simplify me…
mxsrm Dec 25, 2024
a371f4d
feat(writer/xlsx): refactor worksheet writer with improved modularity
mxsrm Dec 25, 2024
fa4a490
feat(src/version.rs): add version constants
mxsrm Dec 25, 2024
d276b63
refactor(helper): refactor regex usage with `once_cell`
mxsrm Dec 26, 2024
a497311
refactor(numbering_format): use `phf` to build static HashMap at comp…
mxsrm Dec 26, 2024
d32cbbd
style(helper/number_format): fix linebreak in comment
mxsrm Dec 26, 2024
be6510b
chore: add assert_sha256 and print_hex macros to utils
mxsrm Dec 26, 2024
731fd71
refactor(utils): replace `once_cell` with `std::sync::OnceLock`
mxsrm Dec 26, 2024
6df0cf9
docs(lib.rs): update library documentation
mxsrm Dec 27, 2024
f07c890
refactor(structs): simplify module structure with macro
mxsrm Dec 27, 2024
03dc659
refactor(structs): use AttrPair for XML attributes
mxsrm Dec 28, 2024
89016c7
refactor(AttrPair): implement From<AttrPair> for (&str, Cow<str>) and…
mxsrm Dec 28, 2024
4ebb9b1
feat(color): update Color struct to use Option<ARGB8> for argb field
mxsrm Dec 29, 2024
3c26246
refactor(border): update Border struct to use Option<Box<Color>>
mxsrm Dec 29, 2024
dd0d524
perf: remove manual inline markers
mxsrm Dec 29, 2024
d9f9d7a
refactor: use compile_time HashMap phf::Map
mxsrm Dec 29, 2024
694c1d6
Revert "perf: remove manual inline markers"
mxsrm Dec 31, 2024
a496cbe
fix(color): support for abbreviated hex colors
mxsrm Dec 31, 2024
80d99ec
refactor(structs/attributes): inline short functions
mxsrm Dec 31, 2024
e39700d
fix(tests): update file paths in integration tests
mxsrm Dec 31, 2024
8b8945c
build: add justfile
mxsrm Dec 31, 2024
11694d5
refactor(workbook): change sheet collection return type
mxsrm Dec 31, 2024
4ea76bd
Dependency Updates, Documentation Enhancements, New Features, and Cod…
mxsrm Jan 8, 2025
37f2463
Revert "Dependency Updates, Documentation Enhancements, New Features,…
MathNya Jan 10, 2025
f46c583
Merge pull request #253 from mxsrm/nextgen
MathNya Jan 10, 2025
df4e21e
fmt and clippy
Jan 14, 2025
16e3bb8
fmt and clippy
Jan 14, 2025
476e113
fmt and clippy
Jan 17, 2025
508c6b8
#254 to ver3.0.0
agentjill Jan 17, 2025
9629565
fmt & clippy
Jan 20, 2025
208cfc9
#209
Jan 21, 2025
64514c5
#257
Jan 22, 2025
f379d82
#209
Jan 22, 2025
afb69a3
#209
Jan 24, 2025
524f3b3
#209
Jan 27, 2025
61d6304
#209
Jan 27, 2025
a563c49
#209
Jan 28, 2025
7b7077e
fix
agentjill Jan 30, 2025
f0900ab
#209
Feb 3, 2025
1d6ad54
#209
Feb 3, 2025
b28181f
#209
Feb 4, 2025
26222ea
#209
Feb 5, 2025
a05aefd
Fix copy col/row styling (#262)
schungx Feb 9, 2025
bebbe15
Fix flipped get_highest_column_and_row values (#264)
DaimoniX Feb 9, 2025
3c33e97
#209
Feb 10, 2025
9984bc1
#209
Feb 14, 2025
6581f00
#265
Feb 12, 2025
fa63d66
#258
Feb 14, 2025
0935968
small fix
Feb 14, 2025
e54ea9c
small fix
Feb 14, 2025
cebacf8
#209
Feb 15, 2025
6d4fc33
small fix
Feb 16, 2025
b1b9d46
small fix
Feb 17, 2025
49784c1
small fix
Feb 17, 2025
3186802
#209
Feb 17, 2025
4e9bfd3
#209
Feb 26, 2025
69ef76e
#209
Mar 3, 2025
4bb7766
minor changes
agentjill Mar 4, 2025
d866595
no message
Mar 4, 2025
d92bb03
small fix
Mar 4, 2025
6ef6a35
bug fix
Mar 4, 2025
0c1be1a
changes for optimisation #270
agentjill Mar 7, 2025
8d12f0c
#209
Mar 11, 2025
6173784
small fix
Mar 11, 2025
a753c25
#209
Mar 14, 2025
84ec2ea
Add indices to Cells. (#273)
schungx Mar 17, 2025
2f36d70
#209
Mar 18, 2025
404d170
Withdraw deprecated
Mar 21, 2025
aa8eabf
#271
Mar 21, 2025
509a162
#209
Mar 21, 2025
9ead3d2
#209
Mar 24, 2025
9679c43
#209
Mar 25, 2025
7c15fdc
#209
Mar 28, 2025
2b99f3d
clippy fix
Mar 28, 2025
a05fde4
#209
Mar 30, 2025
797f0f3
#209
Mar 30, 2025
38418e7
#209
Mar 31, 2025
27b8627
#209
Apr 2, 2025
1726400
#209
Apr 7, 2025
3c2b307
#209
Apr 16, 2025
627ef02
Change image to imagesize
Apr 16, 2025
0ca0ca8
add map_merged_cell
Apr 23, 2025
36dbb67
#209
May 9, 2025
a8fe013
#209
May 13, 2025
1ea3ee0
#209
May 14, 2025
47b75cf
7e8112d to ver3.0
May 19, 2025
2fa3d29
af79398 to version3.0
Jun 11, 2025
cdb0f54
e744ca0 to version3.0
Jun 11, 2025
822354e
b19d0cc to ver3.0
Jun 20, 2025
0051a49
b19d0cc to ver3.0
Jun 20, 2025
06bf265
format fix
Jun 20, 2025
066e5f1
08df7f5 to ver3.0
Jun 24, 2025
2113f4d
bug fix
Jun 24, 2025
c1d000a
#209
Jun 24, 2025
2f65b28
#209
Jun 25, 2025
5b7d86a
#209
Jul 4, 2025
c5b5619
175c063 to ver3.0
yerlibilgin Jul 11, 2025
6496ac5
451b736 to ver3.0
Jul 11, 2025
638ce50
01a122a to ver3.0
Jul 15, 2025
f302a95
eabab42 to ver3.0
Jul 24, 2025
b64944f
b585c2b to ver3.0
Jul 25, 2025
721ffda
a2792e0 to ver3.0
Aug 5, 2025
84f6fc8
no message
Sep 2, 2025
9af56c5
cfe4574 to ver3.0
Aug 5, 2025
58e6346
d0bb7d3 to ver3.0
Sep 30, 2025
fbd6242
ebf3640 to ver3.0
Aug 28, 2025
6651eb0
54d4752 to ver.3.0
Sep 30, 2025
06014c2
#209
Sep 30, 2025
0ccc29a
#209
Oct 7, 2025
a74bba0
296 entory
Nov 19, 2025
587cbd4
297 entry
Nov 19, 2025
10ff0ae
#209 complete
Nov 19, 2025
e5363f9
dc25efd to ver3.0
Nov 25, 2025
e409015
fmt & clippy
Nov 25, 2025
34939bd
fmt & clippy
Nov 25, 2025
0e65939
bug fix
Nov 27, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
3 changes: 2 additions & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ jobs:

steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@nightly

- name: Install Rust
run: rustup component add rustfmt clippy

- name: Run Lint & Tests
run: |
cargo clippy -- -D warnings
cargo fmt --all -- --check
cargo +nightly fmt --all
cargo test
28 changes: 16 additions & 12 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,42 +1,46 @@
[package]
name = "umya-spreadsheet"
version = "2.2.0"
version = "3.0.0"
authors = ["MathNya <umya.net.info@gmail.com>"]
repository = "https://github.com/MathNya/umya-spreadsheet"
keywords = ["excel", "spreadsheet", "xlsx", "reader", "writer"]
categories = ["parser-implementations", "encoding", "text-processing"]
license = "MIT"
readme = "README.md"
description = "umya-spreadsheet is a library written in pure Rust to read and write xlsx file."
edition = "2021"
rust-version = "1.79.0"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
aes = "0.8.4"
ahash = "0.8.11"
base64 = "0.22.1"
byteorder = "1.5"
cbc = "0.1.2"
cfb = "0.10.0"
chrono = { version = "0.4.38", default-features = false, features = ["clock"] }
chrono = { version = "0.4.39", default-features = false, features = ["clock"] }
encoding_rs = "0.8.35"
fancy-regex = "0.14.0"
getrandom = { version = "0.2.15" }
hmac = "0.12.1"
html_parser = "0.7.0"
image = { version = "0.25.5", optional = true }
lazy_static = "1.5.0"
imagesize = "0.14"
md-5 = "0.10.6"
regex = "1.11.1"
num-traits = "0.2.19"
paste = "1.0.15"
phf = { version = "0.11.2", features = ["macros"] }
quick-xml = { version = "0.37.1", features = ["serialize"] }
rand = "0.8.5"
rgb = "0.8.50"
sha2 = "0.10.8"
thin-vec = "0.2.13"
thousands = "0.2.0"
quick-xml = { version = "0.37.1", features = ["serialize"] }
zip = { version = "2.2.1", default-features = false, features = ["deflate"] }
zip = { version = "2.2.2", default-features = false, features = ["deflate"] }

[dev-dependencies]
hex-literal = "0.4.1"

[lib]
doctest = false

[features]
js = ["getrandom/js"]
default = ["image"]

24 changes: 14 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ Please be aware of this.
Copies the style of the specified column or row.
```rust
let mut book = umya_spreadsheet::reader::xlsx::read(path).unwrap();
let sheet = book.get_sheet_mut(&0).unwrap();
let sheet = book.get_sheet_mut(0).unwrap();
sheet.copy_row_styling(&3, &5, None, None);
sheet.copy_col_styling(&3, &5, None, None);
```
#### * The function to create a new comment has been implemented.
```rust
let mut book = umya_spreadsheet::reader::xlsx::read(path).unwrap();
let sheet = book.get_sheet_mut(&0).unwrap();
let sheet = book.get_sheet_mut(0).unwrap();
let mut comment = Comment::default();
comment.new_comment("B2");
comment.set_text_string("TEST");
Expand Down Expand Up @@ -77,25 +77,25 @@ let mut book = umya_spreadsheet::new_file();
### Write file
```rust
let path = std::path::Path::new("./tests/result_files/bbb.xlsx");
let _ = umya_spreadsheet::writer::xlsx::write(&book, path);
let _unused = umya_spreadsheet::writer::xlsx::write(&book, path);
```
### Write file with password
```rust
let path = std::path::Path::new("./tests/result_files/bbb.xlsx");
let _ = umya_spreadsheet::writer::xlsx::write_with_password(&book, path, "password");
let _unused = umya_spreadsheet::writer::xlsx::write_with_password(&book, path, "password");
```
```rust
let from_path = std::path::Path::new("./tests/test_files/aaa.xlsx");
let to_path = std::path::Path::new("./tests/result_files/bbb.xlsx");
let _ = umya_spreadsheet::writer::xlsx::set_password(&from_path, &to_path, "password");
let _unused = umya_spreadsheet::writer::xlsx::set_password(&from_path, &to_path, "password");
```
### Read Value
```rust
let mut book = umya_spreadsheet::new_file();
book.get_sheet_by_name("Sheet1").unwrap().get_cell("A1").get_value();
book.get_sheet_by_name("Sheet1").unwrap().get_cell((1, 1)).get_value();
book.get_sheet_by_name("Sheet1").unwrap().get_cell((&1, &1)).get_value();
book.get_sheet_mut(0).unwrap().get_cell((&1, &1)).get_value();
book.get_sheet_by_name("Sheet1").unwrap().get_cell((1, 1)).get_value();
book.get_sheet_mut(0).unwrap().get_cell((1, 1)).get_value();
```
### Change Value
```rust
Expand Down Expand Up @@ -142,15 +142,15 @@ book.get_sheet_by_name_mut("Sheet1").unwrap()

### Struct

Pass the book as a ```Spreadsheet``` to modify it in other functions.
Pass the book as a ```Workbook``` to modify it in other functions.

```rust

let mut book = umya_spreadsheet::new_file();
let _ = book.new_sheet("Sheet2");
let _unused = book.new_sheet("Sheet2");
update_excel(&mut book);

fn update_excel(book: &mut Spreadsheet) {
fn update_excel(book: &mut Workbook) {
book.get_sheet_by_name_mut("Sheet2").unwrap().get_cell_mut("A1").set_value("Test");
}
```
Expand Down Expand Up @@ -184,3 +184,7 @@ Contributions by way of pull requests are welcome! Please make sure your code u

* `cargo fmt` for formatting
* [clippy](https://github.com/rust-lang/rust-clippy)
```rust
cargo +nightly fmt --all
cargo clippy -- -D warnings
```
1 change: 1 addition & 0 deletions clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
allowed-duplicate-crates = [ "thiserror", "thiserror-impl", "miniz_oxide", "syn"]
23 changes: 23 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Justfile

fmt:
cargo +nightly fmt --all

clippy:
cargo +nightly clippy

clean:
cargo clean

gitclean:
git clean -dfx

test:
#!/usr/bin/env sh
if cargo nextest --help &> /dev/null; then
# If successful, run 'cargo nextest run'
cargo nextest run
else
# If not successful, run 'cargo test'
cargo test
fi
58 changes: 58 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Edition and Feature Flags
style_edition = "2024"
unstable_features = true

# Brace and Parentheses Style
brace_style = "SameLineWhere"
remove_nested_parens = true

# Layout and Line Management
condense_wildcard_suffixes = true
error_on_line_overflow = true
fn_params_layout = "Tall"
overflow_delimited_expr = false
type_punctuation_density = "Wide"
where_single_line = false

# Comment and Documentation Formatting
normalize_comments = true
normalize_doc_attributes = true
wrap_comments = true
format_code_in_doc_comments = true

# Attribute and Shorthand Usage
inline_attribute_width = 0
use_field_init_shorthand = true
use_try_shorthand = true
use_small_heuristics = "Default"

# Match and Block Formatting
match_arm_blocks = true
match_arm_leading_pipes = "Never"
match_block_trailing_comma = false

# Code Structure and Reordering
reorder_impl_items = true
reorder_modules = true

# String and Macro Formatting
format_macro_matchers = true
format_strings = true

# Indentation Style
indent_style = "Block"

# Imports Configuration
group_imports = "StdExternalCrate"
imports_granularity = "Crate"
imports_layout = "Vertical"
reorder_imports = true

# Module and Struct Configuration
struct_field_align_threshold = 50

# Ignored Files
ignore = [
"src/helper/const_str.rs",
"src/helper/crypt/constants.rs",
]
59 changes: 47 additions & 12 deletions src/helper/address.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,59 @@
use fancy_regex::Regex;
use crate::helper::utils::compile_regex;

#[must_use]
pub fn split_address(address: &str) -> (&str, &str) {
address
.rsplit_once('!')
.map(|(sheet_name, range)| (sheet_name.trim_matches(&['\'', '"'][..]), range))
.unwrap_or(("", address))
.map_or(("", address), |(sheet_name, range)| {
(sheet_name.trim_matches(&['\'', '"'][..]), range)
})
}

#[must_use]
pub fn join_address(sheet_name: &str, address: &str) -> String {
if sheet_name == "" {
if sheet_name.is_empty() {
return address.to_string();
}
format!("{}!{}", sheet_name, address)
format!("{sheet_name}!{address}")
}

/// Checks if the given input string is a valid address format.
///
/// The address format is defined by the following regular expression:
/// `^([^\:\\\?$$$$\/\*]+\!)?(\$?[A-Z]{1,3}\$?[0-9]+)(\:\$?[A-Z]{1,3}\$?[0-9]+)?
/// $`.
///
/// # Parameters
///
/// - `input`: A string slice that can be converted to a string reference. This
/// is the input string to be checked against the address format.
///
/// # Returns
///
/// Returns `true` if the input string matches the address format, and `false`
/// otherwise.
///
/// # Panics
///
/// This function may panic if the regular expression fails to compile. However,
/// since the regular expression is initialized only once and is hardcoded, this
/// is unlikely to occur unless there is a bug in the regex itself. The panic
/// will occur during the first call to this function if the regex is invalid.
///
/// # Examples
///
/// ```
/// let valid_address = "$A1";
/// assert!(is_address(valid_address));
///
/// let invalid_address = "invalid_address";
/// assert!(!is_address(invalid_address));
/// ```
pub fn is_address<S: AsRef<str>>(input: S) -> bool {
let regex = compile_regex!(
r"^([^\:\\\?\[\]\/\*]+\!)?(\$?[A-Z]{1,3}\$?[0-9]+)(\:\$?[A-Z]{1,3}\$?[0-9]+)?$"
);
regex.is_match(input.as_ref()).unwrap()
}

#[test]
Expand All @@ -23,13 +65,6 @@ fn split_address_test() {
assert_eq!(split_address(r#"'she"et1'!A1:B2"#), (r#"she"et1"#, "A1:B2"));
}

pub fn is_address<S: AsRef<str>>(input: S) -> bool {
let re =
Regex::new(r"^([^\:\\\?\[\]\/\*]+\!)?(\$?[A-Z]{1,3}\$?[0-9]+)(\:\$?[A-Z]{1,3}\$?[0-9]+)?$")
.unwrap();
re.is_match(input.as_ref()).unwrap()
}

#[test]
fn is_address_test() {
assert!(is_address("A1"));
Expand Down
62 changes: 41 additions & 21 deletions src/helper/binary.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,47 @@
use crate::structs::MediaObject;
use base64::{engine::general_purpose::STANDARD, Engine as _};
use std::fs;
use std::fs::File;
use std::io::BufReader;
use std::io::Cursor;
use std::io::Read;
use std::{
fs,
path::Path,
};

#[inline]
pub fn get_binary_data(path: &str) -> Vec<u8> {
let path = std::path::Path::new(path);
let mut buf = Vec::new();
use crate::structs::MediaObject;

let file = File::open(path).unwrap();
BufReader::new(file).read_to_end(&mut buf).unwrap();
return buf;
}
/// Creates a `MediaObject` from the file at the specified path.
///
/// # Parameters
///
/// - `path`: A reference to a path from which to create the `MediaObject`.
///
/// # Returns
///
/// Returns a `MediaObject` populated with the image data, name, and title
/// extracted from the file at the specified path.
///
/// # Panics
///
/// This function will panic if the file cannot be read, as it calls `unwrap()`
/// on the result of `get_binary_data(path)`. Ensure that the file exists and is
/// readable before calling this function.
///
/// # Example
///
/// ```
/// let media_object = make_media_object("path/to/image.png");
/// ```
#[must_use]
pub fn make_media_object<P: AsRef<Path>>(path: P) -> MediaObject {
let path = path.as_ref();
let file_name = path
.file_name()
.and_then(|name| name.to_str())
.unwrap_or("");
let title = path
.file_stem()
.and_then(|stem| stem.to_str())
.unwrap_or("");

#[inline]
pub fn make_media_object(path: &str) -> MediaObject {
let name = path.split("/").last().unwrap();
let mut obj = MediaObject::default();
obj.set_image_data(get_binary_data(path));
obj.set_image_name(name);
obj.set_image_title(name.split(".").next().unwrap_or(""));
obj.set_image_data(fs::read(path).unwrap());
obj.set_image_name(file_name);
obj.set_image_title(title);
obj
}
Loading