Skip to content

Commit

Permalink
refactoring of TextResourceBuilder and adding AnnotationStore::add_re…
Browse files Browse the repository at this point in the history
…source()
  • Loading branch information
proycon committed Sep 21, 2024
1 parent 5742054 commit 27e3460
Show file tree
Hide file tree
Showing 7 changed files with 207 additions and 302 deletions.
8 changes: 5 additions & 3 deletions src/annotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,10 @@ impl AnnotationStore {
}

/// Builds and adds multiple annotations
pub fn with_annotations(mut self, builders: Vec<AnnotationBuilder>) -> Result<Self, StamError> {
pub fn with_annotations<'a, I>(mut self, builders: I) -> Result<Self, StamError>
where
I: IntoIterator<Item = AnnotationBuilder<'a>>,
{
self.annotate_from_iter(builders)?;
Ok(self)
}
Expand Down Expand Up @@ -469,11 +472,10 @@ impl AnnotationStore {
/// //instantiate a store
/// let mut store = AnnotationStore::new(Config::default())
/// .with_id("example")
/// .add(
/// .with_resource(
/// TextResourceBuilder::new()
/// .with_id("myresource")
/// .with_text("Hello world")
/// .build()?,
/// )?
/// .add(
/// AnnotationDataSet::new(Config::default())
Expand Down
31 changes: 11 additions & 20 deletions src/annotationstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ use crate::datakey::DataKeyHandle;
use crate::error::*;
use crate::file::*;
use crate::json::{FromJson, ToJson};
use crate::resources::{DeserializeTextResource, TextResource, TextResourceHandle};
use crate::resources::{
DeserializeTextResource, TextResource, TextResourceBuilder, TextResourceHandle,
};
use crate::selector::{Offset, OffsetMode, Selector, SelectorBuilder};
use crate::store::*;
use crate::substore::{AnnotationSubStore, AnnotationSubStoreHandle};
Expand All @@ -59,11 +61,10 @@ use crate::types::*;
/// # fn main() -> Result<(),StamError> {
/// let store = AnnotationStore::default()
/// .with_id("example")
/// .add(TextResource::from_string(
/// "myresource",
/// "Hello world",
/// Config::default(),
/// ))?
/// .with_resource(TextResourceBuilder::new()
/// .with_id("myresource")
/// .with_text("Hello world")
/// )?
/// .add(AnnotationDataSet::new(Config::default()).with_id("mydataset"))?
/// .with_annotation(
/// AnnotationBuilder::new()
Expand All @@ -86,11 +87,10 @@ use crate::types::*;
/// # fn main() -> Result<(),StamError> {
/// let store = AnnotationStore::new(Config::default())
/// .with_id("example")
/// .add(
/// .with_resource(
/// TextResourceBuilder::new()
/// .with_id("myresource")
/// .with_text("Hello world")
/// .build()?,
/// )?
/// .add(
/// AnnotationDataSet::new(Config::default())
Expand Down Expand Up @@ -268,7 +268,7 @@ impl private::StoreCallbacks<TextResource> for AnnotationStore {
/// parameters from parent to the item
#[allow(unused_variables)]
fn preinsert(&self, item: &mut TextResource) -> Result<(), StamError> {
item.set_config(self.new_config());
item.initialize(self);
Ok(())
}

Expand Down Expand Up @@ -1004,7 +1004,7 @@ impl AnnotationStore {

/// Returns a [`Config`] instance suitable for instantiation of dependent instances like TextResource,AnnotationDataSet and
/// This will have the working directory set to the annotation store's directory
pub fn new_config(&self) -> Config {
pub(crate) fn new_config(&self) -> Config {
debug(&self.config(), || format!("AnnotationStore::new_config"));
let mut config = self.config().clone();
config.workdir = self.dirname();
Expand Down Expand Up @@ -1348,18 +1348,9 @@ impl AnnotationStore {
self
}

/// Shortcut method to load a resource from file and add it to the store. Returns a handle,
/// wrap it in a call to `self.resource()` to get the resource itself.
pub fn add_resource_from_file(
&mut self,
filename: &str,
) -> Result<TextResourceHandle, StamError> {
let resource = TextResource::from_file(filename, self.new_config())?;
self.insert(resource)
}

/// Shortcut method to load a dataset from file and add it to the store. Returns a handle,
/// wrap it in a call to `self.dataset()` to get the resource itself.
//TODO: REMOTE THIS, obsolete once we have add_dataset()
pub fn add_dataset_from_file(
&mut self,
filename: &str,
Expand Down
7 changes: 2 additions & 5 deletions src/csv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1187,15 +1187,12 @@ impl FromCsv for AnnotationStore {
record.filename
)
});
let mut resourcebuilder = TextResourceBuilder::from_txt_file(
&record.filename,
store.new_config(),
)?;
let mut resourcebuilder = TextResourceBuilder::new().with_filename(record.filename);
if record.id.is_some() {
resourcebuilder =
resourcebuilder.with_id(record.id.map(|x| x.to_string()).unwrap());
}
store.insert(resourcebuilder.build()?)?;
store.add_resource(resourcebuilder)?;
}
Type::AnnotationStore => {
return Err(StamError::CsvError(
Expand Down
Loading

0 comments on commit 27e3460

Please sign in to comment.