context.rs (3664B)
1 use crate::cli::{TargetCliArgs, TargetOutputFormat}; 2 use crate::out::envelope::{EnvelopeActor, EnvelopeContext, OutputFormat}; 3 4 #[derive(Debug, Clone, Copy, PartialEq, Eq)] 5 pub enum OperationOutputFormat { 6 Human, 7 Json, 8 Ndjson, 9 } 10 11 impl Default for OperationOutputFormat { 12 fn default() -> Self { 13 Self::Human 14 } 15 } 16 17 impl From<TargetOutputFormat> for OperationOutputFormat { 18 fn from(format: TargetOutputFormat) -> Self { 19 match format { 20 TargetOutputFormat::Human => Self::Human, 21 TargetOutputFormat::Json => Self::Json, 22 TargetOutputFormat::Ndjson => Self::Ndjson, 23 } 24 } 25 } 26 27 #[derive(Debug, Clone, Copy, PartialEq, Eq)] 28 pub enum OperationNetworkMode { 29 Default, 30 Offline, 31 Online, 32 } 33 34 impl Default for OperationNetworkMode { 35 fn default() -> Self { 36 Self::Default 37 } 38 } 39 40 #[derive(Debug, Clone, Copy, PartialEq, Eq)] 41 pub enum OperationInputMode { 42 PromptingAllowed, 43 NoInput, 44 } 45 46 impl Default for OperationInputMode { 47 fn default() -> Self { 48 Self::PromptingAllowed 49 } 50 } 51 52 #[derive(Debug, Clone, PartialEq, Eq, Default)] 53 pub struct OperationContext { 54 pub output_format: OperationOutputFormat, 55 pub account_id: Option<String>, 56 pub relays: Vec<String>, 57 pub network_mode: OperationNetworkMode, 58 pub dry_run: bool, 59 pub idempotency_key: Option<String>, 60 pub correlation_id: Option<String>, 61 pub approval_token: Option<String>, 62 pub input_mode: OperationInputMode, 63 pub quiet: bool, 64 pub verbose: bool, 65 pub trace: bool, 66 pub color: bool, 67 } 68 69 impl OperationContext { 70 pub fn from_target_args(args: &TargetCliArgs) -> Self { 71 Self { 72 output_format: OperationOutputFormat::from(args.format), 73 account_id: args.account_id.clone(), 74 relays: args.relay.clone(), 75 network_mode: if args.offline { 76 OperationNetworkMode::Offline 77 } else if args.online { 78 OperationNetworkMode::Online 79 } else { 80 OperationNetworkMode::Default 81 }, 82 dry_run: args.dry_run, 83 idempotency_key: args.idempotency_key.clone(), 84 correlation_id: args.correlation_id.clone(), 85 approval_token: args.approval_token.clone(), 86 input_mode: if args.no_input { 87 OperationInputMode::NoInput 88 } else { 89 OperationInputMode::PromptingAllowed 90 }, 91 quiet: args.quiet, 92 verbose: args.verbose, 93 trace: args.trace, 94 color: !args.no_color, 95 } 96 } 97 98 pub fn envelope_context(&self, request_id: impl Into<String>) -> EnvelopeContext { 99 let mut context = EnvelopeContext::new(request_id, self.dry_run); 100 context.output_format = match self.output_format { 101 OperationOutputFormat::Human => OutputFormat::Human, 102 OperationOutputFormat::Json => OutputFormat::Json, 103 OperationOutputFormat::Ndjson => OutputFormat::Ndjson, 104 }; 105 context.correlation_id = self.correlation_id.clone(); 106 context.idempotency_key = self.idempotency_key.clone(); 107 context.actor = self.account_id.as_ref().map(|account_id| EnvelopeActor { 108 account_id: account_id.clone(), 109 role: "account".to_owned(), 110 }); 111 context 112 } 113 114 pub fn requires_approval_token(&self) -> bool { 115 !self.dry_run && !self.has_approval_token() 116 } 117 118 pub fn has_approval_token(&self) -> bool { 119 self.approval_token 120 .as_deref() 121 .is_some_and(|token| !token.trim().is_empty()) 122 } 123 }