11#[ doc( hidden) ]
22pub mod bindings;
33
4+ use std:: collections:: { HashMap , HashSet } ;
5+
46#[ doc( hidden) ]
57pub use bindings:: * ;
68
@@ -21,7 +23,7 @@ pub mod packaging;
2123#[ cfg( not( target_arch = "wasm32" ) ) ]
2224pub use packaging:: packaging_impl;
2325
24- use serde:: Deserialize ;
26+ use serde:: { Deserialize , Serialize } ;
2527use serde_json:: Value as JsonValue ;
2628
2729pub struct Resource {
@@ -65,6 +67,74 @@ pub struct Commit {
6567 pub url : Option < String > ,
6668}
6769
70+ /// Use this for creating Commits.
71+ #[ derive( Clone , Debug , Serialize , Deserialize ) ]
72+ pub struct CommitBuilder {
73+ /// The subject URL that is to be modified by this Delta.
74+ /// Not the URL of the Commit itself.
75+ /// https://atomicdata.dev/properties/subject
76+ pub subject : String ,
77+ /// The set of PropVals that need to be added.
78+ /// Overwrites existing values
79+ /// https://atomicdata.dev/properties/set
80+ set : std:: collections:: HashMap < String , JsonValue > ,
81+ /// The set of PropVals that need to be appended to resource arrays.
82+ push : std:: collections:: HashMap < String , Vec < String > > ,
83+ /// A map of Propvals containing Yjs updates to be applied to the YDocs
84+ y_update : std:: collections:: HashMap < String , JsonValue > ,
85+ /// The set of property URLs that need to be removed
86+ /// https://atomicdata.dev/properties/remove
87+ remove : HashSet < String > ,
88+ /// If set to true, deletes the entire resource
89+ /// https://atomicdata.dev/properties/destroy
90+ destroy : bool ,
91+ previous_commit : Option < String > ,
92+ }
93+
94+ impl CommitBuilder {
95+ pub fn new ( subject : String ) -> Self {
96+ Self {
97+ subject,
98+ set : HashMap :: new ( ) ,
99+ push : HashMap :: new ( ) ,
100+ y_update : HashMap :: new ( ) ,
101+ remove : HashSet :: new ( ) ,
102+ destroy : false ,
103+ previous_commit : None ,
104+ }
105+ }
106+
107+ /// Set Property / Value combinations that will either be created or overwritten.
108+ pub fn set ( & mut self , prop : String , val : JsonValue ) -> & Self {
109+ self . set . insert ( prop, val) ;
110+
111+ self
112+ }
113+
114+ /// Set Property URLs which values to be removed
115+ pub fn remove ( & mut self , prop : String ) -> & Self {
116+ self . remove . insert ( prop) ;
117+
118+ self
119+ }
120+
121+ /// Whether the resource needs to be removed fully
122+ pub fn destroy ( & mut self , destroy : bool ) {
123+ self . destroy = destroy;
124+ }
125+
126+ /// Appends a Resource subject to a ResourceArray.
127+ pub fn push ( & mut self , property : & str , value : String ) -> & Self {
128+ let Some ( vec) = self . push . get_mut ( property) else {
129+ self . push . insert ( property. to_string ( ) , vec ! [ value] ) ;
130+ return self ;
131+ } ;
132+
133+ vec. push ( value. clone ( ) ) ;
134+ self
135+ }
136+ }
137+
68138/// High-level trait for implementing a Class Extender plugin.
69139pub trait ClassExtender {
70140 fn class_url ( ) -> Vec < String > ;
@@ -149,24 +219,23 @@ macro_rules! export_plugin {
149219 } ;
150220}
151221
152- /// Gets a resource from the store, optionally uses the given agent. If no agent is provided the public agent is used .
153- pub fn get_resource ( subject : String , agent : Option < String > ) -> Result < Resource , String > {
154- host:: get_resource ( & subject, agent . as_deref ( ) )
222+ /// Gets a resource from the store by subject, the plugin's agent is used to authorize the request .
223+ pub fn get_resource ( subject : String ) -> Result < Resource , String > {
224+ host:: get_resource ( & subject, None )
155225 . map ( |json| Resource :: try_from ( json) . map_err ( |e| e. to_string ( ) ) ) ?
156226}
157227
158- pub fn query (
159- property : String ,
160- value : String ,
161- agent : Option < String > ,
162- ) -> Result < Vec < Resource > , String > {
163- host:: query ( & property, & value, agent. as_deref ( ) ) . map ( |json| {
228+ /// Queries the store for resources that match the given property and value, the plugin's agent is used to authorize the request.
229+ pub fn query ( property : String , value : String ) -> Result < Vec < Resource > , String > {
230+ host:: query ( & property, & value, None ) . map ( |json| {
164231 json. into_iter ( )
165232 . map ( |json| Resource :: try_from ( json) . map_err ( |e| e. to_string ( ) ) )
166233 . collect :: < Result < Vec < Resource > , String > > ( )
167234 } ) ?
168235}
169236
237+ /// Gets the config of the plugin deserialized to the given type.
238+ /// The user can edit this config at any time.
170239pub fn get_config < ' a , T > ( ) -> Result < T , String >
171240where
172241 T : for < ' de > Deserialize < ' de > ,
@@ -176,6 +245,13 @@ where
176245 . map_err ( |e| format ! ( "Failed to deserialize config: {}" , e) )
177246}
178247
248+ /// Creates a commit and signs it using the plugin's agent.
249+ pub fn commit ( commit : & CommitBuilder ) -> Result < ( ) , String > {
250+ let commit_str =
251+ serde_json:: to_string ( commit) . map_err ( |e| format ! ( "Failed to serialize commit: {}" , e) ) ?;
252+ host:: commit ( & commit_str) . map_err ( |e| format ! ( "Failed to commit: {}" , e) )
253+ }
254+
179255impl TryFrom < ResourceJson > for Resource {
180256 type Error = String ;
181257
0 commit comments