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
use crate::{
client::{prepare_command, PreparedCommand},
resp::{
cmd, CollectionResponse, CommandArgs, MultipleArgsCollection, PrimitiveResponse, SingleArg,
SingleArgCollection, ToArgs,
},
};
use serde::{
de::{
self,
value::{BytesDeserializer, SeqAccessDeserializer},
DeserializeOwned, Unexpected, Visitor,
},
Deserialize, Deserializer,
};
use std::{fmt, marker::PhantomData};
/// A group of Redis commands related to [`Geospatial`](https://redis.io/docs/data-types/geospatial/) indices
///
/// # See Also
/// [Redis Geospatial Commands](https://redis.io/commands/?group=geo)
pub trait GeoCommands<'a> {
/// Adds the specified geospatial items (longitude, latitude, name) to the specified key.
///
/// # Return
/// * When used without optional arguments, the number of elements added to the sorted set (excluding score updates).
/// * If the CH option is specified, the number of elements that were changed (added or updated).
///
/// # See Also
/// [<https://redis.io/commands/geoadd/>](https://redis.io/commands/geoadd/)
#[must_use]
fn geoadd<K, M, I>(
self,
key: K,
condition: GeoAddCondition,
change: bool,
items: I,
) -> PreparedCommand<'a, Self, usize>
where
Self: Sized,
K: SingleArg,
M: SingleArg,
I: MultipleArgsCollection<(f64, f64, M)>,
{
prepare_command(
self,
cmd("GEOADD")
.arg(key)
.arg(condition)
.arg_if(change, "CH")
.arg(items),
)
}
/// Return the distance between two members in the geospatial index
/// represented by the sorted set.
///
/// # Return
/// The distance in the specified unit, or None if one or both the elements are missing.
///
/// # See Also
/// [<https://redis.io/commands/geodist/>](https://redis.io/commands/geodist/)
#[must_use]
fn geodist<K, M>(
self,
key: K,
member1: M,
member2: M,
unit: GeoUnit,
) -> PreparedCommand<'a, Self, Option<f64>>
where
Self: Sized,
K: SingleArg,
M: SingleArg,
{
prepare_command(
self,
cmd("GEODIST").arg(key).arg(member1).arg(member2).arg(unit),
)
}
/// Return valid [Geohash](https://en.wikipedia.org/wiki/Geohash) strings representing the position of one or more elements
/// in a sorted set value representing a geospatial index (where elements were added using [geoadd](GeoCommands::geoadd)).
///
/// # Return
/// An array where each element is the Geohash corresponding to each member name passed as argument to the command.
///
/// # See Also
/// [<https://redis.io/commands/geohash/>](https://redis.io/commands/geohash/)
#[must_use]
fn geohash<K, M, C>(self, key: K, members: C) -> PreparedCommand<'a, Self, Vec<String>>
where
Self: Sized,
K: SingleArg,
M: SingleArg,
C: SingleArgCollection<M>,
{
prepare_command(self, cmd("GEOHASH").arg(key).arg(members))
}
/// Return the positions (longitude,latitude) of all the specified members
/// of the geospatial index represented by the sorted set at key.
///
/// # Return
/// n array where each element is a two elements array representing longitude and latitude
/// (x,y) of each member name passed as argument to the command.
/// Non existing elements are reported as NULL elements of the array.
///
/// # See Also
/// [<https://redis.io/commands/geopos/>](https://redis.io/commands/geopos/)
#[must_use]
fn geopos<K, M, C>(
self,
key: K,
members: C,
) -> PreparedCommand<'a, Self, Vec<Option<(f64, f64)>>>
where
Self: Sized,
K: SingleArg,
M: SingleArg,
C: SingleArgCollection<M>,
{
prepare_command(self, cmd("GEOPOS").arg(key).arg(members))
}
/// Return the members of a sorted set populated with geospatial information using [geoadd](GeoCommands::geoadd),
/// which are within the borders of the area specified by a given shape.
///
/// # Return
/// An array of members + additional information depending
/// on which `with_xyz` options have been selected
///
/// # See Also
/// [<https://redis.io/commands/geosearch/>](https://redis.io/commands/geosearch/)
#[must_use]
fn geosearch<K, M1, M2, A>(
self,
key: K,
from: GeoSearchFrom<M1>,
by: GeoSearchBy,
options: GeoSearchOptions,
) -> PreparedCommand<'a, Self, A>
where
Self: Sized,
K: SingleArg,
M1: SingleArg,
M2: PrimitiveResponse + DeserializeOwned,
A: CollectionResponse<GeoSearchResult<M2>> + DeserializeOwned,
{
prepare_command(
self,
cmd("GEOSEARCH").arg(key).arg(from).arg(by).arg(options),
)
}
/// This command is like [geosearch](GeoCommands::geosearch), but stores the result in destination key.
///
/// # Return
/// the number of elements in the resulting set.
///
/// # See Also
/// [<https://redis.io/commands/geosearchstore/>](https://redis.io/commands/geosearchstore/)
#[must_use]
fn geosearchstore<D, S, M>(
self,
destination: D,
source: S,
from: GeoSearchFrom<M>,
by: GeoSearchBy,
options: GeoSearchStoreOptions,
) -> PreparedCommand<'a, Self, usize>
where
Self: Sized,
D: SingleArg,
S: SingleArg,
M: SingleArg,
{
prepare_command(
self,
cmd("GEOSEARCHSTORE")
.arg(destination)
.arg(source)
.arg(from)
.arg(by)
.arg(options),
)
}
}
/// Condition for the [`geoadd`](GeoCommands::geoadd) command
#[derive(Default)]
pub enum GeoAddCondition {
/// No option
#[default]
None,
/// Don't update already existing elements. Always add new elements.
NX,
/// Only update elements that already exist. Never add elements.
XX,
}
impl ToArgs for GeoAddCondition {
fn write_args(&self, args: &mut CommandArgs) {
match self {
GeoAddCondition::None => {}
GeoAddCondition::NX => {
args.arg("NX");
}
GeoAddCondition::XX => {
args.arg("XX");
}
}
}
}
/// Distance Unit
pub enum GeoUnit {
Meters,
Kilometers,
Miles,
Feet,
}
impl ToArgs for GeoUnit {
fn write_args(&self, args: &mut CommandArgs) {
args.arg(match self {
GeoUnit::Meters => "m",
GeoUnit::Kilometers => "km",
GeoUnit::Miles => "mi",
GeoUnit::Feet => "ft",
});
}
}
/// The query's center point is provided by one of these mandatory options:
pub enum GeoSearchFrom<M>
where
M: SingleArg,
{
/// Use the position of the given existing `member` in the sorted set.
FromMember { member: M },
/// Use the given `longitude` and `latitude` position.
FromLonLat { longitude: f64, latitude: f64 },
}
impl<M> ToArgs for GeoSearchFrom<M>
where
M: SingleArg,
{
fn write_args(&self, args: &mut CommandArgs) {
match self {
GeoSearchFrom::FromMember { member } => args.arg("FROMMEMBER").arg_ref(member),
GeoSearchFrom::FromLonLat {
longitude,
latitude,
} => args.arg("FROMLONLAT").arg(*longitude).arg(*latitude),
};
}
}
/// The query's shape is provided by one of these mandatory options:
pub enum GeoSearchBy {
/// Search inside circular area according to given `radius` in the specified `unit`.
ByRadius { radius: f64, unit: GeoUnit },
/// Search inside an axis-aligned rectangle, determined by `height` and `width` in the specified `unit`.
ByBox {
width: f64,
height: f64,
unit: GeoUnit,
},
}
impl ToArgs for GeoSearchBy {
fn write_args(&self, args: &mut CommandArgs) {
match self {
GeoSearchBy::ByRadius { radius, unit } => {
args.arg("BYRADIUS").arg_ref(radius).arg_ref(unit)
}
GeoSearchBy::ByBox {
width,
height,
unit,
} => args
.arg("BYBOX")
.arg_ref(width)
.arg_ref(height)
.arg_ref(unit),
};
}
}
/// Matching items are returned unsorted by default.
/// To sort them, use one of the following two options:
pub enum GeoSearchOrder {
/// Sort returned items from the nearest to the farthest, relative to the center point.
Asc,
/// Sort returned items from the farthest to the nearest, relative to the center point.
Desc,
}
impl ToArgs for GeoSearchOrder {
fn write_args(&self, args: &mut CommandArgs) {
match self {
GeoSearchOrder::Asc => args.arg("ASC"),
GeoSearchOrder::Desc => args.arg("DESC"),
};
}
}
/// Options for the [`geosearch`](GeoCommands::geosearch) command
#[derive(Default)]
pub struct GeoSearchOptions {
command_args: CommandArgs,
}
impl GeoSearchOptions {
#[must_use]
pub fn order(mut self, order: GeoSearchOrder) -> Self {
Self {
command_args: self.command_args.arg(order).build(),
}
}
#[must_use]
pub fn count(mut self, count: usize, any: bool) -> Self {
Self {
command_args: self
.command_args
.arg("COUNT")
.arg(count)
.arg_if(any, "ANY")
.build(),
}
}
#[must_use]
pub fn with_coord(mut self) -> Self {
Self {
command_args: self.command_args.arg("WITHCOORD").build(),
}
}
#[must_use]
pub fn with_dist(mut self) -> Self {
Self {
command_args: self.command_args.arg("WITHDIST").build(),
}
}
#[must_use]
pub fn with_hash(mut self) -> Self {
Self {
command_args: self.command_args.arg("WITHHASH").build(),
}
}
}
impl ToArgs for GeoSearchOptions {
fn write_args(&self, args: &mut CommandArgs) {
args.arg(&self.command_args);
}
}
/// Result of the [`geosearch`](GeoCommands::geosearch) command.
#[derive(Debug)]
pub struct GeoSearchResult<M>
where
M: PrimitiveResponse,
{
/// The matched member.
pub member: M,
/// The distance of the matched member from the specified center.
pub distance: Option<f64>,
/// The geohash integer of the matched member
pub geo_hash: Option<i64>,
/// The coordinates (longitude, latitude) of the matched member
pub coordinates: Option<(f64, f64)>,
}
impl<'de, M> Deserialize<'de> for GeoSearchResult<M>
where
M: PrimitiveResponse + DeserializeOwned,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
pub enum GeoSearchResultField {
Distance(f64),
GeoHash(i64),
Coordinates((f64, f64)),
}
impl<'de> Deserialize<'de> for GeoSearchResultField {
#[inline]
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct GeoSearchResultFieldVisitor;
impl<'de> Visitor<'de> for GeoSearchResultFieldVisitor {
type Value = GeoSearchResultField;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("GeoSearchResultField")
}
fn visit_borrowed_bytes<E>(self, v: &'de [u8]) -> Result<Self::Value, E>
where
E: de::Error,
{
let Ok(distance) = std::str::from_utf8(v) else {
return Err(de::Error::invalid_value(Unexpected::Bytes(v), &"A valid f64 encoded in an UTF8 string"));
};
let Ok(distance) = distance.parse::<f64>() else {
return Err(de::Error::invalid_value(Unexpected::Bytes(v), &"A valid f64 encoded in an UTF8 string"));
};
Ok(GeoSearchResultField::Distance(distance))
}
fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(GeoSearchResultField::GeoHash(v))
}
fn visit_seq<A>(self, seq: A) -> Result<Self::Value, A::Error>
where
A: de::SeqAccess<'de>,
{
let coordinates =
<(f64, f64)>::deserialize(SeqAccessDeserializer::new(seq))?;
Ok(GeoSearchResultField::Coordinates(coordinates))
}
}
deserializer.deserialize_any(GeoSearchResultFieldVisitor)
}
}
pub struct GeoSearchResultVisitor<M>
where
M: PrimitiveResponse,
{
phantom: PhantomData<M>,
}
impl<'de, M> Visitor<'de> for GeoSearchResultVisitor<M>
where
M: PrimitiveResponse + DeserializeOwned,
{
type Value = GeoSearchResult<M>;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("GeoSearchResult<M>")
}
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: de::SeqAccess<'de>,
{
let Some(member) = seq.next_element::<M>().map_err(de::Error::custom)? else {
return Err(de::Error::invalid_length(0, &"more elements in sequence"));
};
let mut distance: Option<f64> = None;
let mut geo_hash: Option<i64> = None;
let mut coordinates: Option<(f64, f64)> = None;
while let Some(field) = seq.next_element::<GeoSearchResultField>()? {
match field {
GeoSearchResultField::Distance(d) => distance = Some(d),
GeoSearchResultField::GeoHash(gh) => geo_hash = Some(gh),
GeoSearchResultField::Coordinates(c) => coordinates = Some(c),
}
}
Ok(GeoSearchResult {
member,
distance,
geo_hash,
coordinates,
})
}
fn visit_borrowed_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
where
E: de::Error,
{
let member = M::deserialize(BytesDeserializer::new(v))?;
Ok(GeoSearchResult {
member,
distance: None,
geo_hash: None,
coordinates: None,
})
}
}
deserializer.deserialize_any(GeoSearchResultVisitor::<M> {
phantom: PhantomData,
})
}
}
/// Options for the [`geosearchstore`](GeoCommands::geosearchstore) command
#[derive(Default)]
pub struct GeoSearchStoreOptions {
command_args: CommandArgs,
}
impl GeoSearchStoreOptions {
#[must_use]
pub fn order(mut self, order: GeoSearchOrder) -> Self {
Self {
command_args: self.command_args.arg(order).build(),
}
}
#[must_use]
pub fn count(mut self, count: usize, any: bool) -> Self {
Self {
command_args: self
.command_args
.arg("COUNT")
.arg(count)
.arg_if(any, "ANY")
.build(),
}
}
#[must_use]
pub fn store_dist(mut self, store_dist: bool) -> Self {
Self {
command_args: self.command_args.arg_if(store_dist, "STOREDIST").build(),
}
}
}
impl ToArgs for GeoSearchStoreOptions {
fn write_args(&self, args: &mut CommandArgs) {
args.arg(&self.command_args);
}
}