openai-core 0.1.1

Rust SDK for OpenAI-compatible ecosystem
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
//! Shared request-builder primitives used across resource namespaces.

use std::collections::BTreeMap;
use std::marker::PhantomData;
use std::time::Duration;

use bytes::Bytes;
use http::Method;
use percent_encoding::{AsciiSet, CONTROLS, utf8_percent_encode};
use serde::Serialize;
use serde_json::Value;
use tokio_util::sync::CancellationToken;

use crate::Client;
use crate::config::RequestOptions;
use crate::error::{Error, Result};
use crate::files::{MultipartField, UploadSource};
use crate::json_payload::JsonPayload;
use crate::pagination::{CursorPage, ListEnvelope};
use crate::response_meta::ApiResponse;
use crate::stream::{RawSseStream, SseStream};
use crate::transport::{RequestSpec, merge_json_body};

/// URL path encoding set used for dynamic path segments.
const PATH_SEGMENT_ENCODE_SET: &AsciiSet = &CONTROLS
    .add(b'/')
    .add(b'?')
    .add(b'#')
    .add(b'%')
    .add(b'&')
    .add(b'+')
    .add(b'=');

pub(crate) fn value_from<T>(value: &T) -> Result<Value>
where
    T: Serialize,
{
    serde_json::to_value(value).map_err(|error| {
        crate::error::Error::Serialization(crate::SerializationError::new(error.to_string()))
    })
}

/// 对单个路径参数做安全编码,避免动态 ID 改写 URL 结构。
pub(crate) fn encode_path_segment(segment: impl AsRef<str>) -> String {
    utf8_percent_encode(segment.as_ref(), PATH_SEGMENT_ENCODE_SET).to_string()
}

pub(crate) fn metadata_is_empty(metadata: &BTreeMap<String, String>) -> bool {
    metadata.is_empty()
}

/// Shared state for typed JSON request builders defined in longtail namespaces.
#[derive(Debug, Clone)]
pub(crate) struct TypedJsonRequestState<P> {
    pub(crate) client: Option<Client>,
    pub(crate) params: P,
    pub(crate) body_override: Option<Value>,
    pub(crate) options: RequestOptions,
    pub(crate) extra_body: BTreeMap<String, Value>,
    pub(crate) provider_options: BTreeMap<String, Value>,
}

impl<P> TypedJsonRequestState<P> {
    pub(crate) fn new(client: Client, params: P) -> Self {
        Self {
            client: Some(client),
            params,
            body_override: None,
            options: RequestOptions::default(),
            extra_body: BTreeMap::new(),
            provider_options: BTreeMap::new(),
        }
    }

    pub(crate) fn body_value(mut self, body: impl Into<JsonPayload>) -> Self {
        self.body_override = Some(body.into().into_raw());
        self
    }

    pub(crate) fn extra_header(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.options.insert_header(key, value);
        self
    }

    pub(crate) fn extra_query(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.options.insert_query(key, value);
        self
    }

    pub(crate) fn extra_body(
        mut self,
        key: impl Into<String>,
        value: impl Into<JsonPayload>,
    ) -> Self {
        self.extra_body.insert(key.into(), value.into().into_raw());
        self
    }

    pub(crate) fn provider_option(
        mut self,
        key: impl Into<String>,
        value: impl Into<JsonPayload>,
    ) -> Self {
        self.provider_options
            .insert(key.into(), value.into().into_raw());
        self
    }

    pub(crate) fn timeout(mut self, timeout: Duration) -> Self {
        self.options.timeout = Some(timeout);
        self
    }

    pub(crate) fn max_retries(mut self, max_retries: u32) -> Self {
        self.options.max_retries = Some(max_retries);
        self
    }

    pub(crate) fn cancellation_token(mut self, token: CancellationToken) -> Self {
        self.options.cancellation_token = Some(token);
        self
    }
}

impl<P> TypedJsonRequestState<P>
where
    P: Serialize,
{
    pub(crate) fn build_spec(
        mut self,
        endpoint_id: &'static str,
        path: &'static str,
    ) -> Result<(Client, RequestSpec)> {
        let client = self
            .client
            .take()
            .ok_or_else(|| Error::InvalidConfig("请求构建器缺少客户端".into()))?;
        let provider_key = client.provider().kind().as_key();
        let body = merge_json_body(
            Some(
                self.body_override
                    .take()
                    .unwrap_or(value_from(&self.params)?),
            ),
            &self.extra_body,
            provider_key,
            &self.provider_options,
        );
        let mut spec = RequestSpec::new(endpoint_id, Method::POST, path);
        spec.body = Some(body);
        spec.options = self.options;
        Ok((client, spec))
    }
}

/// 表示通用 JSON 请求构建器。
#[derive(Debug, Clone)]
pub struct JsonRequestBuilder<T> {
    pub(crate) client: Client,
    pub(crate) spec: RequestSpec,
    pub(crate) extra_body: BTreeMap<String, Value>,
    pub(crate) provider_options: BTreeMap<String, Value>,
    pub(crate) _marker: PhantomData<T>,
}

impl<T> JsonRequestBuilder<T> {
    pub(crate) fn new(
        client: Client,
        endpoint_id: &'static str,
        method: Method,
        path: impl Into<String>,
    ) -> Self {
        Self {
            client,
            spec: RequestSpec::new(endpoint_id, method, path),
            extra_body: BTreeMap::new(),
            provider_options: BTreeMap::new(),
            _marker: PhantomData,
        }
    }

    /// 设置整个请求体为一个 `serde_json::Value`。
    pub fn body_value(mut self, body: impl Into<JsonPayload>) -> Self {
        self.spec.body = Some(body.into().into_raw());
        self
    }

    /// 使用任意可序列化对象设置请求体。
    ///
    /// # Errors
    ///
    /// 当序列化失败时返回错误。
    pub fn json_body<U>(mut self, body: &U) -> Result<Self>
    where
        U: Serialize,
    {
        self.spec.body = Some(value_from(body)?);
        Ok(self)
    }

    /// 添加一个额外请求头。
    pub fn extra_header(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.spec.options.insert_header(key, value);
        self
    }

    /// 删除一个默认请求头。
    pub fn remove_header(mut self, key: impl Into<String>) -> Self {
        self.spec.options.remove_header(key);
        self
    }

    /// 添加一个额外查询参数。
    pub fn extra_query(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.spec.options.insert_query(key, value);
        self
    }

    /// 在 JSON 根对象中追加字段。
    pub fn extra_body(mut self, key: impl Into<String>, value: impl Into<JsonPayload>) -> Self {
        self.extra_body.insert(key.into(), value.into().into_raw());
        self
    }

    /// 在 provider 对应的 `provider_options` 下追加字段。
    pub fn provider_option(
        mut self,
        key: impl Into<String>,
        value: impl Into<JsonPayload>,
    ) -> Self {
        self.provider_options
            .insert(key.into(), value.into().into_raw());
        self
    }

    /// 覆盖请求超时时间。
    pub fn timeout(mut self, timeout: Duration) -> Self {
        self.spec.options.timeout = Some(timeout);
        self
    }

    /// 覆盖最大重试次数。
    pub fn max_retries(mut self, max_retries: u32) -> Self {
        self.spec.options.max_retries = Some(max_retries);
        self
    }

    /// 设置取消令牌。
    pub fn cancellation_token(mut self, token: CancellationToken) -> Self {
        self.spec.options.cancellation_token = Some(token);
        self
    }

    /// 添加 Multipart 文本字段。
    pub fn multipart_text(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
        let multipart = self.spec.multipart.get_or_insert_default();
        multipart.fields.push(MultipartField {
            name: name.into(),
            value: value.into(),
        });
        self
    }

    /// 添加 Multipart 文件字段。
    pub fn multipart_file(mut self, name: impl Into<String>, file: UploadSource) -> Self {
        let multipart = self.spec.multipart.get_or_insert_default();
        multipart.files.push((name.into(), file));
        self
    }

    pub(crate) fn into_spec(mut self) -> RequestSpec {
        let provider_key = self.client.provider().kind().as_key();
        self.spec.body = Some(merge_json_body(
            self.spec.body.take(),
            &self.extra_body,
            provider_key,
            &self.provider_options,
        ));
        self.spec
    }
}

impl<T> JsonRequestBuilder<T>
where
    T: serde::de::DeserializeOwned,
{
    /// 发送请求并返回业务对象。
    ///
    /// # Errors
    ///
    /// 当请求失败或反序列化失败时返回错误。
    pub async fn send(self) -> Result<T> {
        Ok(self.send_with_meta().await?.data)
    }

    /// 发送请求并保留响应元信息。
    ///
    /// # Errors
    ///
    /// 当请求失败或反序列化失败时返回错误。
    pub async fn send_with_meta(self) -> Result<ApiResponse<T>> {
        let client = self.client.clone();
        client.execute_json(self.into_spec()).await
    }

    /// 发送请求并返回原始 `http::Response<Bytes>`。
    ///
    /// # Errors
    ///
    /// 当请求失败时返回错误。
    pub async fn send_raw(self) -> Result<http::Response<Bytes>> {
        let client = self.client.clone();
        client.execute_raw_http(self.into_spec()).await
    }

    /// 发送请求并返回原始 SSE 事件流。
    ///
    /// 该方法会自动追加 `Accept: text/event-stream` 请求头。
    ///
    /// # Errors
    ///
    /// 当请求失败时返回错误。
    pub async fn send_raw_sse(self) -> Result<RawSseStream> {
        let client = self.client.clone();
        let mut spec = self.into_spec();
        spec.options.insert_header("accept", "text/event-stream");
        client.execute_raw_sse(spec).await
    }
}

impl<T> JsonRequestBuilder<T>
where
    T: serde::de::DeserializeOwned + Send + 'static,
{
    /// 发送请求并把 SSE 数据流解析为指定类型。
    ///
    /// 该方法会自动追加 `Accept: text/event-stream` 请求头。
    ///
    /// # Errors
    ///
    /// 当请求失败或 SSE 事件反序列化失败时返回错误。
    pub async fn send_sse(self) -> Result<SseStream<T>> {
        let client = self.client.clone();
        let mut spec = self.into_spec();
        spec.options.insert_header("accept", "text/event-stream");
        client.execute_sse(spec).await
    }
}

/// 表示二进制响应请求构建器。
#[derive(Debug, Clone)]
pub struct BytesRequestBuilder {
    pub(crate) inner: JsonRequestBuilder<Bytes>,
}

/// 表示不关心响应体的请求构建器。
#[derive(Debug, Clone)]
pub struct NoContentRequestBuilder {
    pub(crate) inner: JsonRequestBuilder<Bytes>,
}

impl BytesRequestBuilder {
    pub(crate) fn new(
        client: Client,
        endpoint_id: &'static str,
        method: Method,
        path: impl Into<String>,
    ) -> Self {
        Self {
            inner: JsonRequestBuilder::new(client, endpoint_id, method, path),
        }
    }

    /// 设置 JSON 请求体。
    pub fn body_value(mut self, body: impl Into<JsonPayload>) -> Self {
        self.inner = self.inner.body_value(body);
        self
    }

    /// 设置可序列化请求体。
    ///
    /// # Errors
    ///
    /// 当序列化失败时返回错误。
    pub fn json_body<U>(mut self, body: &U) -> Result<Self>
    where
        U: Serialize,
    {
        self.inner = self.inner.json_body(body)?;
        Ok(self)
    }

    /// 追加请求头。
    pub fn extra_header(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.inner = self.inner.extra_header(key, value);
        self
    }

    /// 追加查询参数。
    pub fn extra_query(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.inner = self.inner.extra_query(key, value);
        self
    }

    /// 在 provider 对应的 `provider_options` 下追加字段。
    pub fn provider_option(
        mut self,
        key: impl Into<String>,
        value: impl Into<JsonPayload>,
    ) -> Self {
        self.inner = self.inner.provider_option(key, value);
        self
    }

    /// 覆盖请求超时时间。
    pub fn timeout(mut self, timeout: Duration) -> Self {
        self.inner = self.inner.timeout(timeout);
        self
    }

    /// 覆盖最大重试次数。
    pub fn max_retries(mut self, max_retries: u32) -> Self {
        self.inner = self.inner.max_retries(max_retries);
        self
    }

    /// 设置取消令牌。
    pub fn cancellation_token(mut self, token: CancellationToken) -> Self {
        self.inner = self.inner.cancellation_token(token);
        self
    }

    /// 添加 Multipart 文本字段。
    pub fn multipart_text(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
        self.inner = self.inner.multipart_text(name, value);
        self
    }

    /// 添加 Multipart 文件字段。
    pub fn multipart_file(mut self, name: impl Into<String>, file: UploadSource) -> Self {
        self.inner = self.inner.multipart_file(name, file);
        self
    }

    /// 在 JSON 根对象中追加字段。
    pub fn extra_body(mut self, key: impl Into<String>, value: impl Into<JsonPayload>) -> Self {
        self.inner = self.inner.extra_body(key, value);
        self
    }

    /// 发送请求并返回原始字节。
    ///
    /// # Errors
    ///
    /// 当请求失败时返回错误。
    pub async fn send(self) -> Result<Bytes> {
        Ok(self.send_with_meta().await?.data)
    }

    /// 发送请求并保留响应元信息。
    ///
    /// # Errors
    ///
    /// 当请求失败时返回错误。
    pub async fn send_with_meta(self) -> Result<ApiResponse<Bytes>> {
        let client = self.inner.client.clone();
        client.execute_bytes(self.inner.into_spec()).await
    }

    /// 发送请求并返回原始 HTTP 响应。
    ///
    /// # Errors
    ///
    /// 当请求失败时返回错误。
    pub async fn send_raw(self) -> Result<http::Response<Bytes>> {
        let client = self.inner.client.clone();
        client.execute_raw_http(self.inner.into_spec()).await
    }

    /// 发送请求并返回原始 SSE 事件流。
    ///
    /// 该方法会自动追加 `Accept: text/event-stream` 请求头。
    ///
    /// # Errors
    ///
    /// 当请求失败时返回错误。
    pub async fn send_raw_sse(self) -> Result<RawSseStream> {
        let client = self.inner.client.clone();
        let mut spec = self.inner.into_spec();
        spec.options.insert_header("accept", "text/event-stream");
        client.execute_raw_sse(spec).await
    }

    /// 发送请求并把 SSE 数据流解析为指定类型。
    ///
    /// 该方法会自动追加 `Accept: text/event-stream` 请求头。
    ///
    /// # Errors
    ///
    /// 当请求失败或 SSE 事件反序列化失败时返回错误。
    pub async fn send_sse<T>(self) -> Result<SseStream<T>>
    where
        T: serde::de::DeserializeOwned + Send + 'static,
    {
        let client = self.inner.client.clone();
        let mut spec = self.inner.into_spec();
        spec.options.insert_header("accept", "text/event-stream");
        client.execute_sse(spec).await
    }
}

impl NoContentRequestBuilder {
    pub(crate) fn new(
        client: Client,
        endpoint_id: &'static str,
        method: Method,
        path: impl Into<String>,
    ) -> Self {
        Self {
            inner: JsonRequestBuilder::new(client, endpoint_id, method, path),
        }
    }

    /// 设置 JSON 请求体。
    pub fn body_value(mut self, body: impl Into<JsonPayload>) -> Self {
        self.inner = self.inner.body_value(body);
        self
    }

    /// 设置可序列化请求体。
    ///
    /// # Errors
    ///
    /// 当序列化失败时返回错误。
    pub fn json_body<U>(mut self, body: &U) -> Result<Self>
    where
        U: Serialize,
    {
        self.inner = self.inner.json_body(body)?;
        Ok(self)
    }

    /// 追加请求头。
    pub fn extra_header(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.inner = self.inner.extra_header(key, value);
        self
    }

    /// 删除一个默认请求头。
    pub fn remove_header(mut self, key: impl Into<String>) -> Self {
        self.inner = self.inner.remove_header(key);
        self
    }

    /// 追加查询参数。
    pub fn extra_query(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.inner = self.inner.extra_query(key, value);
        self
    }

    /// 在 JSON 根对象中追加字段。
    pub fn extra_body(mut self, key: impl Into<String>, value: impl Into<JsonPayload>) -> Self {
        self.inner = self.inner.extra_body(key, value);
        self
    }

    /// 在 provider 对应的 `provider_options` 下追加字段。
    pub fn provider_option(
        mut self,
        key: impl Into<String>,
        value: impl Into<JsonPayload>,
    ) -> Self {
        self.inner = self.inner.provider_option(key, value);
        self
    }

    /// 覆盖请求超时时间。
    pub fn timeout(mut self, timeout: Duration) -> Self {
        self.inner = self.inner.timeout(timeout);
        self
    }

    /// 覆盖最大重试次数。
    pub fn max_retries(mut self, max_retries: u32) -> Self {
        self.inner = self.inner.max_retries(max_retries);
        self
    }

    /// 设置取消令牌。
    pub fn cancellation_token(mut self, token: CancellationToken) -> Self {
        self.inner = self.inner.cancellation_token(token);
        self
    }

    /// 发送请求并忽略响应体。
    ///
    /// # Errors
    ///
    /// 当请求失败时返回错误。
    pub async fn send(self) -> Result<()> {
        self.send_with_meta().await.map(|_| ())
    }

    /// 发送请求并保留响应元信息。
    ///
    /// # Errors
    ///
    /// 当请求失败时返回错误。
    pub async fn send_with_meta(self) -> Result<ApiResponse<()>> {
        let client = self.inner.client.clone();
        let response = client.execute_bytes(self.inner.into_spec()).await?;
        let (_, meta) = response.into_parts();
        Ok(ApiResponse::new((), meta))
    }

    /// 发送请求并返回原始 HTTP 响应。
    ///
    /// # Errors
    ///
    /// 当请求失败时返回错误。
    pub async fn send_raw(self) -> Result<http::Response<Bytes>> {
        let client = self.inner.client.clone();
        client.execute_raw_http(self.inner.into_spec()).await
    }
}

/// 表示列表请求构建器。
#[derive(Debug, Clone)]
pub struct ListRequestBuilder<T> {
    pub(crate) inner: JsonRequestBuilder<ListEnvelope<T>>,
}

impl<T> ListRequestBuilder<T> {
    pub(crate) fn new(client: Client, endpoint_id: &'static str, path: impl Into<String>) -> Self {
        Self {
            inner: JsonRequestBuilder::new(client, endpoint_id, Method::GET, path),
        }
    }

    /// 设置 `after` 游标。
    pub fn after(mut self, cursor: impl Into<String>) -> Self {
        self.inner = self.inner.extra_query("after", cursor);
        self
    }

    /// 设置 `before` 游标。
    pub fn before(mut self, cursor: impl Into<String>) -> Self {
        self.inner = self.inner.extra_query("before", cursor);
        self
    }

    /// 设置分页大小。
    pub fn limit(mut self, limit: u32) -> Self {
        self.inner = self.inner.extra_query("limit", limit.to_string());
        self
    }

    /// 追加请求头。
    pub fn extra_header(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.inner = self.inner.extra_header(key, value);
        self
    }

    /// 在根对象追加额外字段。
    pub fn extra_body(mut self, key: impl Into<String>, value: impl Into<JsonPayload>) -> Self {
        self.inner = self.inner.extra_body(key, value);
        self
    }
}

impl<T> ListRequestBuilder<T>
where
    T: Clone + Send + Sync + serde::de::DeserializeOwned + 'static,
{
    /// 发送列表请求并返回游标分页对象。
    ///
    /// # Errors
    ///
    /// 当请求失败或反序列化失败时返回错误。
    pub async fn send(self) -> Result<CursorPage<T>> {
        let client = self.inner.client.clone();
        let path = self.inner.spec.path.clone();
        let endpoint_id = self.inner.spec.endpoint_id;
        let response = client
            .execute_json::<ListEnvelope<T>>(self.inner.into_spec())
            .await?;
        let ListEnvelope {
            object,
            data,
            first_id,
            last_id,
            has_more,
            extra,
        } = response.data;
        let mut next_query = BTreeMap::new();
        if let Some(last_id) = &last_id {
            next_query.insert("after".into(), Some(last_id.clone()));
        }
        Ok(CursorPage::from(ListEnvelope {
            object,
            data,
            first_id,
            last_id,
            has_more,
            extra,
        })
        .with_next_request(if has_more {
            Some(crate::client::PageRequestSpec {
                client,
                endpoint_id,
                method: Method::GET,
                path,
                query: next_query,
            })
        } else {
            None
        }))
    }
}

#[cfg(test)]
mod tests {
    use percent_encoding::percent_decode_str;
    use proptest::prelude::*;

    use super::encode_path_segment;

    proptest! {
        #[test]
        fn encoded_path_segment_roundtrips_through_percent_decode(segment in any::<String>()) {
            let encoded = encode_path_segment(&segment);
            let decoded = percent_decode_str(&encoded).decode_utf8().unwrap();
            prop_assert_eq!(decoded, segment);
        }
    }
}