Skip to content

Commit

Permalink
Try generics in resource #45
Browse files Browse the repository at this point in the history
  • Loading branch information
joepio committed Dec 31, 2020
1 parent d5230c8 commit 8afb90e
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 19 deletions.
6 changes: 3 additions & 3 deletions lib/src/collections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ impl Collection {
pub fn to_resource<'a>(
&self,
store: &'a impl crate::Storelike,
) -> AtomicResult<crate::Resource<'a>> {
) -> AtomicResult<crate::Resource<'a, impl Storelike>> {
// TODO: Should not persist, because now it is spammimg the store!
// let mut resource = crate::Resource::new_instance(crate::urls::COLLECTION, store)?;
let mut resource = crate::Resource::new(self.subject.clone(), store);
Expand Down Expand Up @@ -167,8 +167,8 @@ impl Collection {
pub fn construct_collection<'a>(
store: &'a impl Storelike,
query_params: url::form_urlencoded::Parse,
resource: Resource,
) -> AtomicResult<Resource<'a>> {
resource: Resource<impl Storelike>,
) -> AtomicResult<Resource<'a, impl Storelike>> {
let mut sort_by = None;
let mut sort_desc = false;
let mut current_page = 0;
Expand Down
2 changes: 1 addition & 1 deletion lib/src/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub struct Commit {
impl Commit {
/// Converts the Commit into a Resource with Atomic Values.
/// Creates an identifier using the base_url or a default.
pub fn into_resource<'a>(self, store: &'a impl Storelike) -> AtomicResult<Resource<'a>> {
pub fn into_resource<'a>(self, store: &'a impl Storelike) -> AtomicResult<Resource<'a, impl Storelike>> {
let subject = match self.signature.as_ref() {
Some(sig) => format!("{}commits/{}", store.get_base_url(), sig),
None => {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl Storelike for Db {
Ok(())
}

fn add_resource(&self, resource: &Resource) -> AtomicResult<()> {
fn add_resource(&self, resource: &Resource<Db>) -> AtomicResult<()> {
self.set_propvals(resource.get_subject(), &resource.get_propvals())?;
Ok(())
}
Expand Down
14 changes: 7 additions & 7 deletions lib/src/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use std::collections::HashMap;
/// All changes to the Resource are applied after committing them (e.g. by using).
// #[derive(Clone, Debug, Serialize, Deserialize)]
#[derive(Clone)]
pub struct Resource<'a> {
pub struct Resource<'a, S: Storelike> {
/// A hashMap of all the Property Value combinations
propvals: PropVals,
subject: String,
Expand All @@ -24,15 +24,15 @@ pub struct Resource<'a> {
// Should be an empty vector if it's checked, should be None if unknown
classes: Option<Vec<Class>>,
/// A reference to the store
store: &'a impl Storelike,
store: &'a S,
commit: CommitBuilder,
}

/// Maps Property URLs to their values
/// Similar to ResourceString, but uses Values instead of Strings
pub type PropVals = HashMap<String, Value>;

impl<'a> Resource<'a> {
impl<'a, S: Storelike> Resource<'a, S> {
/// Fetches all 'required' properties. Fails is any are missing in this Resource.
pub fn check_required_props(&mut self) -> AtomicResult<()> {
let classvec = self.get_classes()?;
Expand Down Expand Up @@ -105,7 +105,7 @@ impl<'a> Resource<'a> {
}

/// Create a new, empty Resource.
pub fn new(subject: String, store: &'a impl Storelike) -> Resource<'a> {
pub fn new(subject: String, store: &'a S) -> Resource<'a, S> {
let propvals: PropVals = HashMap::new();
Resource {
propvals,
Expand All @@ -118,7 +118,7 @@ impl<'a> Resource<'a> {

/// Create a new instance of some Class.
/// The subject is generated, but can be changed.
pub fn new_instance(class_url: &str, store: &'a impl Storelike) -> AtomicResult<Resource<'a>> {
pub fn new_instance(class_url: &str, store: &'a S) -> AtomicResult<Resource<'a, S>> {
let propvals: PropVals = HashMap::new();
let mut classes_vec = Vec::new();
classes_vec.push(store.get_class(class_url)?);
Expand Down Expand Up @@ -150,8 +150,8 @@ impl<'a> Resource<'a> {
pub fn new_from_resource_string(
subject: String,
resource_string: &ResourceString,
store: &'a impl Storelike,
) -> AtomicResult<Resource<'a>> {
store: &'a S,
) -> AtomicResult<Resource<'a, S>> {
let mut res = Resource::new(subject, store);
for (prop_string, val_string) in resource_string {
let propertyfull = store.get_property(prop_string).expect("Prop not found");
Expand Down
14 changes: 7 additions & 7 deletions lib/src/storelike.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub trait Storelike: Sized {
/// Adds a Resource to the store.
/// Replaces existing resource with the contents.
/// In most cases, you should use `.commit()` instead.
fn add_resource(&self, resource: &Resource) -> AtomicResult<()> {
fn add_resource(&self, resource: &Resource<Self>) -> AtomicResult<()> {
self.add_resource_string(resource.get_subject().clone(), &resource.to_plain())?;
Ok(())
}
Expand Down Expand Up @@ -85,7 +85,7 @@ pub trait Storelike: Sized {
/// Creates, edits or destroys a resource.
/// Checks if the signature is created by the Agent.
/// Should check if the Agent has the correct rights.
fn commit(&self, commit: crate::Commit) -> AtomicResult<Resource> {
fn commit(&self, commit: crate::Commit) -> AtomicResult<Resource<Self>> {
let signature = match commit.signature.as_ref() {
Some(sig) => sig,
None => return Err("No signature set".into()),
Expand Down Expand Up @@ -138,7 +138,7 @@ pub trait Storelike: Sized {
}
resource.check_required_props()?;
// TOOD: Persist delta to store, use hash as ID
let commit_resource: Resource = commit.into_resource(self)?;
let commit_resource: Resource<Self> = commit.into_resource(self)?;
self.add_resource(&commit_resource)?;
Ok(commit_resource)
}
Expand All @@ -147,7 +147,7 @@ pub trait Storelike: Sized {
/// Signs the Commit using the Default Agent.
/// Does not send it to an Atomic Server.
/// Fails if no Default Agent is set.
fn commit_resource_changes_locally(&self, resource: &mut Resource) -> AtomicResult<()> {
fn commit_resource_changes_locally(&self, resource: &mut Resource<Self>) -> AtomicResult<()> {
let agent = self.get_default_agent()?;
let commit = resource.get_commit_and_reset().sign(&agent)?;
self.commit(commit)?;
Expand All @@ -158,7 +158,7 @@ pub trait Storelike: Sized {
/// Signs the Commit using the Default Agent.
/// Sends the Commit to the Atomic Server of the Subject.
/// Fails if no Default Agent is set.
fn commit_resource_changes_externally(&self, resource: &mut Resource) -> AtomicResult<()> {
fn commit_resource_changes_externally(&self, resource: &mut Resource<Self>) -> AtomicResult<()> {
let agent = self.get_default_agent()?;
let commit = resource.get_commit_and_reset().sign(&agent)?;
crate::client::post_commit(&commit)?;
Expand Down Expand Up @@ -197,7 +197,7 @@ pub trait Storelike: Sized {
/// Returns a full Resource with native Values.
/// Note that this does _not_ construct dynamic Resources, such as collections.
/// If you're not sure what to use, use `get_resource_extended`.
fn get_resource(&self, subject: &str) -> AtomicResult<Resource> {
fn get_resource(&self, subject: &str) -> AtomicResult<Resource<Self>> {
let resource_string = self.get_resource_string(subject)?;
let mut res = Resource::new(subject.into(), self);
for (prop_string, val_string) in resource_string {
Expand Down Expand Up @@ -309,7 +309,7 @@ pub trait Storelike: Sized {

/// Get's the resource, parses the Query parameters and calculates dynamic properties.
/// Currently only used for constructing Collections.
fn get_resource_extended(&self, subject: &str) -> AtomicResult<Resource> {
fn get_resource_extended(&self, subject: &str) -> AtomicResult<Resource<Self>> {
let mut url = url::Url::parse(subject)?;
let clone = url.clone();
let query_params = clone.query_pairs();
Expand Down

0 comments on commit 8afb90e

Please sign in to comment.