From c7d035390723ff724438858b5e3340e8b9c59330 Mon Sep 17 00:00:00 2001 From: Hicham Azimani Date: Thu, 11 Apr 2024 11:44:40 +0200 Subject: [PATCH] feat: Open file with custom filename cache (#136) --- cpp/philipsslide.cc | 5 +++-- cpp/philipsslide.hpp | 2 +- src/bindings.rs | 2 +- src/facade.rs | 11 ++++++++--- src/lib.rs | 2 ++ src/pixel_engine.rs | 15 ++++++++++++++- tests/test_facade.rs | 22 +++++++++++++++++++++- 7 files changed, 50 insertions(+), 9 deletions(-) diff --git a/cpp/philipsslide.cc b/cpp/philipsslide.cc index 5758e89..669a5b4 100644 --- a/cpp/philipsslide.cc +++ b/cpp/philipsslide.cc @@ -46,10 +46,11 @@ std::unique_ptr 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(); } diff --git a/cpp/philipsslide.hpp b/cpp/philipsslide.hpp index 86eef88..ec3fb46 100644 --- a/cpp/philipsslide.hpp +++ b/cpp/philipsslide.hpp @@ -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; diff --git a/src/bindings.rs b/src/bindings.rs index 09c9298..e251f73 100644 --- a/src/bindings.rs +++ b/src/bindings.rs @@ -59,7 +59,7 @@ pub(crate) mod ffi { fn facade(self: &PhilipsEngine, input: &CxxString) -> Result>; // 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; fn iSyntaxFileVersion(self: &Facade) -> Result<&CxxString>; diff --git a/src/facade.rs b/src/facade.rs index e120cc2..9bfa9d6 100644 --- a/src/facade.rs +++ b/src/facade.rs @@ -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>( + /// Open an ISyntax file through a facade and specify a cache file + /// if the container allows it + pub(crate) fn open_with_cache_file, R: AsRef>( &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 diff --git a/src/lib.rs b/src/lib.rs index 94dde6f..c14dbfd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -35,12 +35,14 @@ pub struct View<'a> { _lifetime: PhantomData<&'a ()>, // Note: Represent Image Lifetime } +#[derive(Debug, Clone)] pub enum ImageType { WSI, MacroImage, LabelImage, } +#[derive(Debug, Clone)] pub enum ContainerName { Default, Ficom, diff --git a/src/pixel_engine.rs b/src/pixel_engine.rs index a76c0f9..de83f12 100644 --- a/src/pixel_engine.rs +++ b/src/pixel_engine.rs @@ -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>(&self, filename: P, container: &ContainerName) -> Result { + 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, R: AsRef>( + &self, + filename: P, + container: &ContainerName, + cache_filename: R, + ) -> Result { let facade_id = rand::thread_rng().gen::().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) } diff --git a/tests/test_facade.rs b/tests/test_facade.rs index 8db2995..acce009 100644 --- a/tests/test_facade.rs +++ b/tests/test_facade.rs @@ -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; @@ -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()); +}