Skip to content

Commit 27e3460

Browse files
committed
refactoring of TextResourceBuilder and adding AnnotationStore::add_resource()
1 parent 5742054 commit 27e3460

File tree

7 files changed

+207
-302
lines changed

7 files changed

+207
-302
lines changed

src/annotation.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,10 @@ impl AnnotationStore {
419419
}
420420

421421
/// Builds and adds multiple annotations
422-
pub fn with_annotations(mut self, builders: Vec<AnnotationBuilder>) -> Result<Self, StamError> {
422+
pub fn with_annotations<'a, I>(mut self, builders: I) -> Result<Self, StamError>
423+
where
424+
I: IntoIterator<Item = AnnotationBuilder<'a>>,
425+
{
423426
self.annotate_from_iter(builders)?;
424427
Ok(self)
425428
}
@@ -469,11 +472,10 @@ impl AnnotationStore {
469472
/// //instantiate a store
470473
/// let mut store = AnnotationStore::new(Config::default())
471474
/// .with_id("example")
472-
/// .add(
475+
/// .with_resource(
473476
/// TextResourceBuilder::new()
474477
/// .with_id("myresource")
475478
/// .with_text("Hello world")
476-
/// .build()?,
477479
/// )?
478480
/// .add(
479481
/// AnnotationDataSet::new(Config::default())

src/annotationstore.rs

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ use crate::datakey::DataKeyHandle;
3838
use crate::error::*;
3939
use crate::file::*;
4040
use crate::json::{FromJson, ToJson};
41-
use crate::resources::{DeserializeTextResource, TextResource, TextResourceHandle};
41+
use crate::resources::{
42+
DeserializeTextResource, TextResource, TextResourceBuilder, TextResourceHandle,
43+
};
4244
use crate::selector::{Offset, OffsetMode, Selector, SelectorBuilder};
4345
use crate::store::*;
4446
use crate::substore::{AnnotationSubStore, AnnotationSubStoreHandle};
@@ -59,11 +61,10 @@ use crate::types::*;
5961
/// # fn main() -> Result<(),StamError> {
6062
/// let store = AnnotationStore::default()
6163
/// .with_id("example")
62-
/// .add(TextResource::from_string(
63-
/// "myresource",
64-
/// "Hello world",
65-
/// Config::default(),
66-
/// ))?
64+
/// .with_resource(TextResourceBuilder::new()
65+
/// .with_id("myresource")
66+
/// .with_text("Hello world")
67+
/// )?
6768
/// .add(AnnotationDataSet::new(Config::default()).with_id("mydataset"))?
6869
/// .with_annotation(
6970
/// AnnotationBuilder::new()
@@ -86,11 +87,10 @@ use crate::types::*;
8687
/// # fn main() -> Result<(),StamError> {
8788
/// let store = AnnotationStore::new(Config::default())
8889
/// .with_id("example")
89-
/// .add(
90+
/// .with_resource(
9091
/// TextResourceBuilder::new()
9192
/// .with_id("myresource")
9293
/// .with_text("Hello world")
93-
/// .build()?,
9494
/// )?
9595
/// .add(
9696
/// AnnotationDataSet::new(Config::default())
@@ -268,7 +268,7 @@ impl private::StoreCallbacks<TextResource> for AnnotationStore {
268268
/// parameters from parent to the item
269269
#[allow(unused_variables)]
270270
fn preinsert(&self, item: &mut TextResource) -> Result<(), StamError> {
271-
item.set_config(self.new_config());
271+
item.initialize(self);
272272
Ok(())
273273
}
274274

@@ -1004,7 +1004,7 @@ impl AnnotationStore {
10041004

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

1351-
/// Shortcut method to load a resource from file and add it to the store. Returns a handle,
1352-
/// wrap it in a call to `self.resource()` to get the resource itself.
1353-
pub fn add_resource_from_file(
1354-
&mut self,
1355-
filename: &str,
1356-
) -> Result<TextResourceHandle, StamError> {
1357-
let resource = TextResource::from_file(filename, self.new_config())?;
1358-
self.insert(resource)
1359-
}
1360-
13611351
/// Shortcut method to load a dataset from file and add it to the store. Returns a handle,
13621352
/// wrap it in a call to `self.dataset()` to get the resource itself.
1353+
//TODO: REMOTE THIS, obsolete once we have add_dataset()
13631354
pub fn add_dataset_from_file(
13641355
&mut self,
13651356
filename: &str,

src/csv.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,15 +1187,12 @@ impl FromCsv for AnnotationStore {
11871187
record.filename
11881188
)
11891189
});
1190-
let mut resourcebuilder = TextResourceBuilder::from_txt_file(
1191-
&record.filename,
1192-
store.new_config(),
1193-
)?;
1190+
let mut resourcebuilder = TextResourceBuilder::new().with_filename(record.filename);
11941191
if record.id.is_some() {
11951192
resourcebuilder =
11961193
resourcebuilder.with_id(record.id.map(|x| x.to_string()).unwrap());
11971194
}
1198-
store.insert(resourcebuilder.build()?)?;
1195+
store.add_resource(resourcebuilder)?;
11991196
}
12001197
Type::AnnotationStore => {
12011198
return Err(StamError::CsvError(

0 commit comments

Comments
 (0)