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
use crate::types::connection::{EmptyEdgeFields, QueryOperation}; use crate::{Connection, Context, DataSource, FieldResult}; use byteorder::{ReadBytesExt, BE}; #[async_trait::async_trait] impl<'a, T: Sync> DataSource for &'a [T] { type Element = &'a T; type EdgeFieldsObj = EmptyEdgeFields; async fn query_operation( &mut self, _ctx: &Context<'_>, operation: &QueryOperation, ) -> FieldResult<Connection<Self::Element, Self::EdgeFieldsObj>> { let (start, end) = match operation { QueryOperation::None => { let start = 0; let end = self.len(); (start, end) } QueryOperation::After { after } => { let start = base64::decode(after.to_string()) .ok() .and_then(|data| data.as_slice().read_u32::<BE>().ok()) .map(|idx| (idx + 1) as usize) .unwrap_or(0); let end = self.len(); (start, end) } QueryOperation::Before { before } => { let end = base64::decode(before.to_string()) .ok() .and_then(|data| data.as_slice().read_u32::<BE>().ok()) .map(|idx| idx as usize) .unwrap_or_else(|| self.len()); let start = 0; (start, end) } QueryOperation::Between { after, before } => { let start = base64::decode(after.to_string()) .ok() .and_then(|data| data.as_slice().read_u32::<BE>().ok()) .map(|idx| (idx + 1) as usize) .unwrap_or(0); let end = base64::decode(before.to_string()) .ok() .and_then(|data| data.as_slice().read_u32::<BE>().ok()) .map(|idx| idx as usize) .unwrap_or_else(|| self.len()); (start, end) } QueryOperation::First { limit } => { let start = 0; let end = (start + *limit).min(self.len()); (start, end) } QueryOperation::FirstAfter { after, limit } => { let start = base64::decode(after.to_string()) .ok() .and_then(|data| data.as_slice().read_u32::<BE>().ok()) .map(|idx| (idx + 1) as usize) .unwrap_or(0); let end = (start + *limit).min(self.len()); (start, end) } QueryOperation::FirstBefore { before, limit } => { let end_cursor = base64::decode(before.to_string()) .ok() .and_then(|data| data.as_slice().read_u32::<BE>().ok()) .map(|idx| idx as usize) .unwrap_or_else(|| self.len()); let start = (end_cursor - *limit).max(0); let end = (start + *limit).min(end_cursor); (start, end) } QueryOperation::FirstBetween { after, before, limit, } => { let start = base64::decode(after.to_string()) .ok() .and_then(|data| data.as_slice().read_u32::<BE>().ok()) .map(|idx| (idx + 1) as usize) .unwrap_or(0); let end_cursor = base64::decode(before.to_string()) .ok() .and_then(|data| data.as_slice().read_u32::<BE>().ok()) .map(|idx| idx as usize) .unwrap_or_else(|| self.len()); let end = (start + *limit).min(end_cursor); (start, end) } QueryOperation::Last { limit } => { let end = self.len(); let start = (end - *limit).max(0); (start, end) } QueryOperation::LastAfter { after, limit } => { let end = self.len(); let start_cursor = base64::decode(after.to_string()) .ok() .and_then(|data| data.as_slice().read_u32::<BE>().ok()) .map(|idx| (idx + 1) as usize) .unwrap_or(0); let start = (end - *limit).max(start_cursor); (start, end) } QueryOperation::LastBefore { before, limit } => { let end = base64::decode(before.to_string()) .ok() .and_then(|data| data.as_slice().read_u32::<BE>().ok()) .map(|idx| idx as usize) .unwrap_or_else(|| self.len()); let start = (end - *limit).max(0); (start, end) } QueryOperation::LastBetween { after, before, limit, } => { let start_cursor = base64::decode(after.to_string()) .ok() .and_then(|data| data.as_slice().read_u32::<BE>().ok()) .map(|idx| (idx + 1) as usize) .unwrap_or(0); let end = base64::decode(before.to_string()) .ok() .and_then(|data| data.as_slice().read_u32::<BE>().ok()) .map(|idx| idx as usize) .unwrap_or_else(|| self.len()); let start = (end - *limit).max(start_cursor); (start, end) } QueryOperation::Invalid => { let start = 0; let end = 0; (start, end) } }; let mut nodes = Vec::with_capacity(end - start); if nodes.capacity() != 0 { for (idx, item) in self[start..end].iter().enumerate() { nodes.push(( base64::encode((idx as u32).to_be_bytes()).into(), EmptyEdgeFields, item, )); } } Ok(Connection::new(None, start > 0, end < self.len(), nodes)) } }