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

feat: Open file with custom filename cache #136

Merged
merged 1 commit into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 3 additions & 2 deletions cpp/philipsslide.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ std::unique_ptr<Facade> PhilipsEngine::facade(std::string const& input) const {
// File properties
Facade::Facade(ISyntaxFacade& facade) : _facade(facade) {}

void Facade::open(rust::Str url, rust::Str container) const {
void Facade::open(rust::Str url, rust::Str container, rust::Str cache_filename) const {
std::string _url(url);
std::string _container(container);
_facade.open(_url, _container, std::ios::in | std::ios::binary, "");
std::string _cache_filename(cache_filename);
_facade.open(_url, _container, std::ios::in | std::ios::binary, _cache_filename);
}

void Facade::close() const { _facade.close(); }
Expand Down
2 changes: 1 addition & 1 deletion cpp/philipsslide.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Facade {
public:
Facade(ISyntaxFacade& facade);

void open(rust::Str url, rust::Str container) const;
void open(rust::Str url, rust::Str container, rust::Str cache_filename) const;
void close() const;
size_t numImages() const;
std::string const& iSyntaxFileVersion() const;
Expand Down
2 changes: 1 addition & 1 deletion src/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub(crate) mod ffi {
fn facade(self: &PhilipsEngine, input: &CxxString) -> Result<UniquePtr<Facade>>;

// Facade properties
fn open(self: &Facade, url: &str, container: &str) -> Result<()>;
fn open(self: &Facade, url: &str, container: &str, cache_filename: &str) -> Result<()>;
fn close(self: &Facade) -> Result<()>;
fn numImages(self: &Facade) -> Result<usize>;
fn iSyntaxFileVersion(self: &Facade) -> Result<&CxxString>;
Expand Down
11 changes: 8 additions & 3 deletions src/facade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@ impl<'a> Drop for Facade<'a> {
/// The facade allow file manipulation & file information retrieval
/// NOTE: Philips Engine and all internal objects are not thread safe
impl<'a> Facade<'a> {
/// Open an ISyntax file through a facade
pub(crate) fn open<P: AsRef<Path>>(
/// Open an ISyntax file through a facade and specify a cache file
/// if the container allows it
pub(crate) fn open_with_cache_file<P: AsRef<Path>, R: AsRef<Path>>(
&self,
filename: P,
container: &ContainerName,
cache_filename: R,
) -> Result<()> {
let filename = filename.as_ref().display().to_string();
Ok(self.inner.open(&filename, container.as_str())?)
let cache_filename = cache_filename.as_ref().display().to_string();
Ok(self
.inner
.open(&filename, container.as_str(), &cache_filename)?)
}

// close close hold by the facade
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@
_lifetime: PhantomData<&'a ()>, // Note: Represent Image Lifetime
}

#[derive(Debug, Clone)]

Check warning on line 38 in src/lib.rs

View check run for this annotation

Codecov / codecov/patch

src/lib.rs#L38

Added line #L38 was not covered by tests
pub enum ImageType {
WSI,
MacroImage,
LabelImage,
}

#[derive(Debug, Clone)]

Check warning on line 45 in src/lib.rs

View check run for this annotation

Codecov / codecov/patch

src/lib.rs#L45

Added line #L45 was not covered by tests
pub enum ContainerName {
Default,
Ficom,
Expand Down
15 changes: 14 additions & 1 deletion src/pixel_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,26 @@ impl PhilipsEngine {
/// This facade is a handle to a file
/// May fail if the fail cannot be opened
pub fn facade<P: AsRef<Path>>(&self, filename: P, container: &ContainerName) -> Result<Facade> {
self.facade_with_cache_file(filename, container, "")
}

/// Create a new instance of Facade
/// A Facade is a reference to a Philips Engine internal object
/// This facade is a handle to a file
/// May fail if the fail cannot be opened
pub fn facade_with_cache_file<P: AsRef<Path>, R: AsRef<Path>>(
&self,
filename: P,
container: &ContainerName,
cache_filename: R,
) -> Result<Facade> {
let facade_id = rand::thread_rng().gen::<u64>().to_string();
let_cxx_string!(facade_id = facade_id);
let facade = Facade {
inner: self.inner.facade(&facade_id)?,
_lifetime: Default::default(),
};
facade.open(filename, container)?;
facade.open_with_cache_file(filename, container, cache_filename)?;
Ok(facade)
}

Expand Down
22 changes: 21 additions & 1 deletion tests/test_facade.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod fixture;

use fixture::{missing_file, sample, sample_i2syntax, unsupported_file};
use std::path::Path;
use std::path::{Path, PathBuf};

use philips_isyntax_rs::{ContainerName, PhilipsEngine};
use rstest::rstest;
Expand Down Expand Up @@ -157,3 +157,23 @@ fn test_multiple_file(#[case] filename: &Path) {
assert_eq!(facade2.isyntax_file_version().unwrap(), "100.5");
assert_eq!(facade2.num_images().unwrap(), 3);
}

#[rstest]
#[case(sample())]
fn test_facade_with_cache_file(#[case] filename: &Path) {
let engine = PhilipsEngine::new();

let cache_file = PathBuf::from("/tmp/sample-cache-file.fic");
assert!(!cache_file.exists());

let facade = engine
.facade_with_cache_file(
filename,
&ContainerName::CachingFicom,
"/tmp/sample-cache-file.fic",
)
.expect("Cannot open file");
assert_eq!(facade.isyntax_file_version().unwrap(), "100.5");

assert!(cache_file.exists());
}