flow-lib 0.2.0

Library for implementing Space Operator nodes.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
//! Providing services and information for nodes to use.
//!
//! Services are abstracted with [`tower::Service`] trait, using our
//! [`TowerClient`][crate::utils::TowerClient] utility to make it easier to use.
//!
//! Each service is defined is a separated module:
//! - [`get_jwt`]
//! - [`execute`]
//! - [`signer`]

use crate::{
    ContextConfig, FlowRunId, HttpClientConfig, NodeId, SolanaClientConfig, UserId, ValueSet,
    config::{Endpoints, client::FlowRunOrigin},
    flow_run_events::{self, Event, NodeLogContent, NodeLogSender},
    solana::Instructions,
    utils::{Extensions, tower_client::unimplemented_svc},
};
use bytes::Bytes;
use chrono::Utc;
use futures::channel::mpsc;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use solana_pubkey::Pubkey;
use solana_rpc_client::nonblocking::rpc_client::RpcClient as SolanaClient;
use std::{any::Any, collections::HashMap, sync::Arc, time::Duration};
use tower::{Service, ServiceExt};

pub mod env {
    pub const RUST_LOG: &str = "RUST_LOG";
    pub const OVERWRITE_FEEPAYER: &str = "OVERWRITE_FEEPAYER";
    pub const COMPUTE_BUDGET: &str = "COMPUTE_BUDGET";
    pub const FALLBACK_COMPUTE_BUDGET: &str = "FALLBACK_COMPUTE_BUDGET";
    pub const PRIORITY_FEE: &str = "PRIORITY_FEE";
    pub const TX_COMMITMENT_LEVEL: &str = "TX_COMMITMENT_LEVEL";
    pub const WAIT_COMMITMENT_LEVEL: &str = "WAIT_COMMITMENT_LEVEL";
    pub const EXECUTE_ON: &str = "EXECUTE_ON";
    pub const DEVNET_LOOKUP_TABLE: &str = "DEVNET_LOOKUP_TABLE";
    pub const MAINNET_LOOKUP_TABLE: &str = "MAINNET_LOOKUP_TABLE";
}

pub mod api_input {
    use std::time::Duration;

    use crate::{
        FlowRunId, NodeId,
        utils::{TowerClient, tower_client::CommonError},
    };
    use thiserror::Error as ThisError;
    use value::Value;

    pub struct Request {
        pub flow_run_id: FlowRunId,
        pub node_id: NodeId,
        pub times: u32,
        pub timeout: Duration,
    }

    pub struct Response {
        pub value: Value,
    }

    #[derive(ThisError, Debug, Clone)]
    pub enum Error {
        #[error("canceled by user")]
        Canceled,
        #[error("timeout")]
        Timeout,
        #[error(transparent)]
        Common(#[from] CommonError),
    }

    pub type Svc = TowerClient<Request, Response, Error>;
}

/// Get user's JWT, require
/// [`user_token`][crate::config::node::Permissions::user_tokens] permission.
pub mod get_jwt {
    use crate::{UserId, utils::TowerClient, utils::tower_client::CommonError};
    use std::future::Ready;
    use thiserror::Error as ThisError;

    #[derive(Clone, Copy)]
    pub struct Request {
        pub user_id: UserId,
    }

    #[derive(Clone, Debug)]
    pub struct Response {
        pub access_token: String,
    }

    #[derive(ThisError, Debug, Clone)]
    pub enum Error {
        #[error("not allowed")]
        NotAllowed,
        #[error("user not found")]
        UserNotFound,
        #[error("wrong recipient")]
        WrongRecipient,
        #[error("{}: {}", error, error_description)]
        Supabase {
            error: String,
            error_description: String,
        },
        #[error(transparent)]
        Common(#[from] CommonError),
    }

    impl From<actix::MailboxError> for Error {
        fn from(value: actix::MailboxError) -> Self {
            CommonError::from(value).into()
        }
    }

    impl actix::Message for Request {
        type Result = Result<Response, Error>;
    }

    pub type Svc = TowerClient<Request, Response, Error>;

    pub fn not_allowed() -> Svc {
        Svc::new(tower::service_fn(|_| {
            std::future::ready(Result::<Response, _>::Err(Error::NotAllowed))
        }))
    }

    #[derive(Clone, Copy, Debug)]
    pub struct RetryPolicy(pub usize);

    impl Default for RetryPolicy {
        fn default() -> Self {
            Self(1)
        }
    }

    impl tower::retry::Policy<Request, Response, Error> for RetryPolicy {
        type Future = Ready<()>;

        fn retry(
            &mut self,
            _: &mut Request,
            result: &mut Result<Response, Error>,
        ) -> Option<Self::Future> {
            match result {
                Err(Error::Supabase {
                    error_description, ..
                }) if error_description.contains("Refresh Token") && self.0 > 0 => {
                    tracing::error!("get_jwt error: {}, retrying", error_description);
                    self.0 -= 1;
                    Some(std::future::ready(()))
                }
                _ => None,
            }
        }

        fn clone_request(&mut self, req: &Request) -> Option<Request> {
            Some(*req)
        }
    }
}

/// Request Solana signature from external wallets.
pub mod signer {
    use crate::{
        FlowRunId,
        utils::{TowerClient, tower_client::CommonError},
    };
    use actix::MailboxError;
    use chrono::{DateTime, Utc};
    use serde::{Deserialize, Serialize};
    use serde_with::{DisplayFromStr, DurationSecondsWithFrac, base64::Base64, serde_as};
    use solana_presigner::Presigner as SdkPresigner;
    use solana_pubkey::Pubkey;
    use solana_signature::Signature;
    use std::time::Duration;
    use thiserror::Error as ThisError;

    #[derive(ThisError, Debug)]
    pub enum Error {
        #[error("can't sign for pubkey: {}", .0)]
        Pubkey(String),
        #[error("can't sign for this user")]
        User,
        #[error("timeout")]
        Timeout,
        #[error(transparent)]
        Common(#[from] CommonError),
    }

    impl From<MailboxError> for Error {
        fn from(value: MailboxError) -> Self {
            CommonError::from(value).into()
        }
    }

    pub type Svc = TowerClient<SignatureRequest, SignatureResponse, Error>;

    #[serde_as]
    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub struct Presigner {
        #[serde_as(as = "DisplayFromStr")]
        pub pubkey: Pubkey,
        #[serde_as(as = "DisplayFromStr")]
        pub signature: Signature,
    }

    impl From<Presigner> for SdkPresigner {
        fn from(value: Presigner) -> Self {
            SdkPresigner::new(&value.pubkey, &value.signature)
        }
    }

    #[serde_as]
    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub struct SignatureRequest {
        pub id: Option<i64>,
        #[serde(with = "chrono::serde::ts_milliseconds")]
        pub time: DateTime<Utc>,
        #[serde_as(as = "DisplayFromStr")]
        pub pubkey: Pubkey,
        #[serde_as(as = "Base64")]
        pub message: bytes::Bytes,
        #[serde_as(as = "DurationSecondsWithFrac<f64>")]
        pub timeout: Duration,
        pub flow_run_id: Option<FlowRunId>,
        pub signatures: Option<Vec<Presigner>>,
    }

    impl actix::Message for SignatureRequest {
        type Result = Result<SignatureResponse, Error>;
    }

    #[serde_as]
    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub struct SignatureResponse {
        #[serde_as(as = "DisplayFromStr")]
        pub signature: Signature,
        #[serde_as(as = "Option<Base64>")]
        pub new_message: Option<bytes::Bytes>,
    }

    impl bincode::Encode for SignatureResponse {
        fn encode<E: bincode::enc::Encoder>(
            &self,
            encoder: &mut E,
        ) -> Result<(), bincode::error::EncodeError> {
            self.signature.as_array().encode(encoder)?;
            self.new_message
                .as_ref()
                .map(|m| m.as_ref())
                .encode(encoder)?;
            Ok(())
        }
    }

    impl<C> bincode::Decode<C> for SignatureResponse {
        fn decode<D: bincode::de::Decoder<Context = C>>(
            decoder: &mut D,
        ) -> Result<Self, bincode::error::DecodeError> {
            let signature = Signature::from(<[u8; 64]>::decode(decoder)?);
            let new_message = Option::<Vec<u8>>::decode(decoder)?.map(Into::into);
            Ok(Self {
                signature,
                new_message,
            })
        }
    }

    impl<'de, C> bincode::BorrowDecode<'de, C> for SignatureResponse {
        fn borrow_decode<D: bincode::de::BorrowDecoder<'de, Context = C>>(
            decoder: &mut D,
        ) -> Result<Self, bincode::error::DecodeError> {
            bincode::Decode::decode(decoder)
        }
    }
}

/// Output values and Solana instructions to be executed.
pub mod execute {
    use crate::{
        solana::Instructions,
        utils::{
            TowerClient,
            tower_client::{CommonError, CommonErrorExt},
        },
    };
    use futures::channel::oneshot::Canceled;
    use serde::{Deserialize, Serialize};
    use serde_with::{DisplayFromStr, base64::Base64, serde_as};
    use solana_instruction_error::InstructionError;
    use solana_message::CompileError;
    use solana_sanitize::SanitizeError;

    use solana_rpc_client_api::client_error::Error as ClientError;
    use solana_signature::Signature;
    use solana_signer::SignerError;
    use std::sync::Arc;
    use thiserror::Error as ThisError;

    use super::signer;

    pub type Svc = TowerClient<Request, Response, Error>;

    #[derive(Deserialize)]
    #[serde(try_from = "RequestRepr")]
    pub struct Request {
        pub instructions: Instructions,
        pub output: value::Map,
    }

    impl bincode::Encode for Request {
        fn encode<E: bincode::enc::Encoder>(
            &self,
            encoder: &mut E,
        ) -> Result<(), bincode::error::EncodeError> {
            self.instructions.encode(encoder)?;
            value::bincode_impl::MapBincode::from(&self.output).encode(encoder)?;
            Ok(())
        }
    }

    impl<C> bincode::Decode<C> for Request {
        fn decode<D: bincode::de::Decoder<Context = C>>(
            decoder: &mut D,
        ) -> Result<Self, bincode::error::DecodeError> {
            Ok(Self {
                instructions: Instructions::decode(decoder)?,
                output: value::bincode_impl::MapBincode::decode(decoder)?
                    .0
                    .into_owned(),
            })
        }
    }

    #[serde_as]
    #[derive(Deserialize)]
    struct RequestRepr {
        #[serde_as(as = "Base64")]
        instructions: Vec<u8>,
        output: value::Map,
    }

    impl TryFrom<RequestRepr> for Request {
        type Error = rmp_serde::decode::Error;
        fn try_from(value: RequestRepr) -> Result<Self, Self::Error> {
            Ok(Self {
                instructions: rmp_serde::from_slice(&value.instructions)?,
                output: value.output,
            })
        }
    }

    #[serde_as]
    #[derive(Serialize, Clone, Copy)]
    pub struct Response {
        #[serde_as(as = "Option<DisplayFromStr>")]
        pub signature: Option<Signature>,
    }

    impl bincode::Encode for Response {
        fn encode<E: bincode::enc::Encoder>(
            &self,
            encoder: &mut E,
        ) -> Result<(), bincode::error::EncodeError> {
            self.signature.map(|s| *s.as_array()).encode(encoder)?;
            Ok(())
        }
    }

    impl<C> bincode::Decode<C> for Response {
        fn decode<D: bincode::de::Decoder<Context = C>>(
            decoder: &mut D,
        ) -> Result<Self, bincode::error::DecodeError> {
            let value = Option::<[u8; 64]>::decode(decoder)?;
            Ok(Self {
                signature: value.map(Signature::from),
            })
        }
    }

    fn unwrap(s: &Option<String>) -> &str {
        s.as_ref().map(|v| v.as_str()).unwrap_or_default()
    }

    #[serde_as]
    #[derive(ThisError, Debug, Clone, Serialize, Deserialize)]
    pub enum Error {
        #[error("canceled {}", unwrap(.0))]
        Canceled(Option<String>),
        #[error("collected")]
        Collected,
        #[error("some node failed to provide instructions")]
        TxIncomplete,
        #[error("time out")]
        Timeout,
        #[error("insufficient solana balance, needed={needed}; have={balance};")]
        InsufficientSolanaBalance { needed: u64, balance: u64 },
        #[error("transaction simulation failed")]
        TxSimFailed,
        #[error("{}", crate::utils::verbose_solana_error(.error))]
        Solana {
            #[source]
            #[serde_as(as = "Arc<crate::errors::AsClientError>")]
            error: Arc<ClientError>,
            inserted: usize,
        },
        #[error(transparent)]
        Signer(
            #[from]
            #[serde_as(as = "Arc<crate::errors::AsSignerError>")]
            Arc<SignerError>,
        ),
        #[error(transparent)]
        CompileError(
            #[from]
            #[serde_as(as = "Arc<crate::errors::AsCompileError>")]
            Arc<CompileError>,
        ),
        #[error(transparent)]
        InstructionError(#[from] Arc<InstructionError>),
        #[error(transparent)]
        SanitizeError(
            #[from]
            #[serde_as(as = "Arc<crate::errors::AsSanitizeError>")]
            Arc<SanitizeError>,
        ),
        #[error(transparent)]
        ChannelClosed(
            #[from]
            #[serde_as(as = "crate::errors::AsCancelled")]
            Canceled,
        ),
        #[error(transparent)]
        Common(#[from] CommonError),
    }

    impl From<actix::MailboxError> for Error {
        fn from(value: actix::MailboxError) -> Self {
            CommonError::from(value).into()
        }
    }

    impl Error {
        pub fn solana(error: ClientError, inserted: usize) -> Self {
            Self::Solana {
                error: Arc::new(error),
                inserted,
            }
        }
    }

    impl From<signer::Error> for Error {
        fn from(value: signer::Error) -> Self {
            match value {
                e @ signer::Error::Pubkey(_) => Self::other(e),
                e @ signer::Error::User => Self::other(e),
                signer::Error::Timeout => Self::Timeout,
                signer::Error::Common(error) => Self::Common(error),
            }
        }
    }

    impl From<SignerError> for Error {
        fn from(value: SignerError) -> Self {
            Error::Signer(Arc::new(value))
        }
    }

    impl From<CompileError> for Error {
        fn from(value: CompileError) -> Self {
            Error::CompileError(Arc::new(value))
        }
    }

    impl From<InstructionError> for Error {
        fn from(value: InstructionError) -> Self {
            Error::InstructionError(Arc::new(value))
        }
    }

    impl From<SanitizeError> for Error {
        fn from(value: SanitizeError) -> Self {
            Error::SanitizeError(Arc::new(value))
        }
    }
}

#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema)]
pub struct FlowSetContextData {
    pub flow_owner: User,
    pub started_by: User,
    pub endpoints: Endpoints,
    pub solana: SolanaClientConfig,
    pub http: HttpClientConfig,
}

#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema)]
pub struct FlowContextData {
    pub flow_run_id: FlowRunId,
    pub environment: HashMap<String, String>,
    pub inputs: ValueSet,
    pub set: FlowSetContextData,
}

#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema)]
pub struct CommandContextData {
    pub node_id: NodeId,
    pub times: u32,
    pub flow: FlowContextData,
}

#[derive(Clone, bon::Builder)]
pub struct FlowSetServices {
    pub http: reqwest::Client,
    pub solana_client: Arc<SolanaClient>,
    pub extensions: Arc<Extensions>,
    pub api_input: api_input::Svc,
}

#[derive(Clone, bon::Builder)]
pub struct FlowServices {
    pub signer: signer::Svc,
    pub set: FlowSetServices,
}

#[derive(Clone, bon::Builder)]
pub struct CommandContext {
    data: CommandContextData,
    execute: execute::Svc,
    get_jwt: get_jwt::Svc,
    node_log: NodeLogSender,
    flow: FlowServices,
}

impl CommandContext {
    pub fn test_context() -> Self {
        let config = ContextConfig::default();
        let solana_client = Arc::new(config.solana_client.build_client(None));
        let node_id = NodeId::nil();
        let times = 0;
        let (tx, _) = flow_run_events::channel();
        Self {
            data: CommandContextData {
                node_id,
                times,
                flow: FlowContextData {
                    flow_run_id: FlowRunId::nil(),
                    environment: HashMap::new(),
                    inputs: ValueSet::default(),
                    set: FlowSetContextData {
                        flow_owner: User::default(),
                        started_by: User::default(),
                        endpoints: Endpoints::default(),
                        solana: config.solana_client,
                        http: config.http_client,
                    },
                },
            },
            execute: unimplemented_svc(),
            get_jwt: unimplemented_svc(),
            node_log: NodeLogSender::new(tx, node_id, times),
            flow: FlowServices {
                signer: unimplemented_svc(),
                set: FlowSetServices {
                    http: reqwest::Client::new(),
                    solana_client,
                    extensions: <_>::default(),
                    api_input: unimplemented_svc(),
                },
            },
        }
    }

    pub fn log(&self, log: NodeLogContent) -> Result<(), mpsc::TrySendError<Event>> {
        self.node_log.send(log)
    }

    pub fn flow_inputs(&self) -> &value::Map {
        &self.data.flow.inputs
    }

    pub fn new_interflow_origin(&self) -> FlowRunOrigin {
        FlowRunOrigin::Interflow {
            flow_run_id: *self.flow_run_id(),
            node_id: *self.node_id(),
            times: *self.times(),
        }
    }

    pub fn flow_run_id(&self) -> &FlowRunId {
        &self.data.flow.flow_run_id
    }

    pub fn node_id(&self) -> &NodeId {
        &self.data.node_id
    }

    pub fn times(&self) -> &u32 {
        &self.data.times
    }

    pub fn environment(&self) -> &HashMap<String, String> {
        &self.data.flow.environment
    }

    pub fn endpoints(&self) -> &Endpoints {
        &self.data.flow.set.endpoints
    }

    pub fn flow_owner(&self) -> &User {
        &self.data.flow.set.flow_owner
    }

    pub fn started_by(&self) -> &User {
        &self.data.flow.set.started_by
    }

    pub fn solana_config(&self) -> &SolanaClientConfig {
        &self.data.flow.set.solana
    }

    pub fn solana_client(&self) -> &Arc<SolanaClient> {
        &self.flow.set.solana_client
    }

    pub fn http(&self) -> &reqwest::Client {
        &self.flow.set.http
    }

    pub async fn api_input(
        &mut self,
        timeout: Option<Duration>,
    ) -> Result<api_input::Response, api_input::Error> {
        let req = api_input::Request {
            flow_run_id: *self.flow_run_id(),
            node_id: *self.node_id(),
            times: *self.times(),
            timeout: timeout.unwrap_or(Duration::MAX),
        };
        self.flow.set.api_input.ready().await?.call(req).await
    }

    /// Call [`get_jwt`] service, the result will have `Bearer ` prefix.
    pub async fn get_jwt_header(&mut self) -> Result<String, get_jwt::Error> {
        let user_id = self.flow_owner().id;
        let access_token = self
            .get_jwt
            .ready()
            .await?
            .call(get_jwt::Request { user_id })
            .await?
            .access_token;
        Ok(["Bearer ", &access_token].concat())
    }

    /// Call [`execute`] service.
    pub async fn execute(
        &mut self,
        instructions: Instructions,
        output: value::Map,
    ) -> Result<execute::Response, execute::Error> {
        self.execute
            .ready()
            .await?
            .call(execute::Request {
                instructions,
                output,
            })
            .await
    }

    /// Call [`signer`] service.
    pub async fn request_signature(
        &mut self,
        pubkey: Pubkey,
        message: Bytes,
        timeout: Duration,
    ) -> Result<signer::SignatureResponse, signer::Error> {
        self.flow
            .signer
            .ready()
            .await?
            .call(signer::SignatureRequest {
                id: None,
                time: Utc::now(),
                pubkey,
                message,
                timeout,
                flow_run_id: Some(self.data.flow.flow_run_id),
                signatures: None,
            })
            .await
    }

    /// Get an extension by type.
    pub fn get<T: Any + Send + Sync + 'static>(&self) -> Option<&T> {
        self.flow.set.extensions.get::<T>()
    }

    pub fn extensions_mut(&mut self) -> Option<&mut Extensions> {
        Arc::get_mut(&mut self.flow.set.extensions)
    }

    pub fn raw(&self) -> RawContext<'_> {
        RawContext {
            data: &self.data,
            services: RawServices {
                signer: &self.flow.signer,
                execute: &self.execute,
                get_jwt: &self.get_jwt,
            },
        }
    }
}

pub struct RawServices<'a> {
    pub signer: &'a signer::Svc,
    pub execute: &'a execute::Svc,
    pub get_jwt: &'a get_jwt::Svc,
}

pub struct RawContext<'a> {
    pub data: &'a CommandContextData,
    pub services: RawServices<'a>,
}

impl Default for CommandContext {
    fn default() -> Self {
        Self::test_context()
    }
}

#[derive(Clone, Copy, Serialize, Deserialize, Debug, JsonSchema)]
pub struct User {
    pub id: UserId,
}

impl User {
    pub fn new(id: UserId) -> Self {
        Self { id }
    }
}

impl Default for User {
    /// For testing
    fn default() -> Self {
        User {
            id: uuid::Uuid::nil(),
        }
    }
}