miniconf_coap 0.2.0

CoAP interface for `miniconf`
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
752
753
754
755
756
757
758
use core::{convert::Infallible, fmt};
#[cfg(feature = "json-core")]
use fmt::Write as _;

#[cfg(any(feature = "json-core", feature = "cbor"))]
use coap_message::MutableWritableMessage;
use coap_message::{
    Code as _, MessageOption, MinimalWritableMessage, OptionNumber as _, ReadableMessage,
    error::RenderableOnMinimal,
};
use coap_numbers::{code, option};
#[cfg(feature = "cbor")]
use minicbor::{
    Encoder as CborEncoder,
    data::Int as CborInt,
    encode::{self, write::EndOfSlice},
};

#[cfg(any(feature = "json-core", feature = "cbor"))]
use crate::format;
use crate::{ChangedKey, MAX_URI_PATH_LENGTH};

const MAX_ACCEPT_OPTIONS: usize = 4;

pub(crate) type UriPath = heapless::String<MAX_URI_PATH_LENGTH>;

#[derive(Debug, Clone, Copy, Default)]
pub(crate) struct Accepts {
    values: [u16; MAX_ACCEPT_OPTIONS],
    len: usize,
    overflow: bool,
}

impl Accepts {
    const fn new() -> Self {
        Self {
            values: [0; MAX_ACCEPT_OPTIONS],
            len: 0,
            overflow: false,
        }
    }

    fn from_option(value: Option<u16>) -> Self {
        let mut accepts = Self::new();
        if let Some(value) = value {
            accepts.values[0] = value;
            accepts.len = 1;
        }
        accepts
    }

    fn push(&mut self, value: u16) {
        if let Some(slot) = self.values.get_mut(self.len) {
            *slot = value;
            self.len += 1;
        } else {
            self.overflow = true;
        }
    }

    const fn is_present(&self) -> bool {
        self.len != 0
    }

    fn first(&self) -> Option<u16> {
        self.values.first().copied().filter(|_| self.len != 0)
    }

    fn contains(&self, content_format: u16) -> bool {
        self.values[..self.len].contains(&content_format)
    }

    fn accepts(&self, content_format: u16) -> Result<(), Error> {
        if !self.is_present() || self.contains(content_format) {
            Ok(())
        } else if self.overflow {
            Err(Error::new(code::BAD_OPTION, Problem::TooManyAcceptOptions))
        } else {
            Err(Error::new(code::NOT_ACCEPTABLE, Problem::NotAcceptable))
        }
    }
}

/// Parsed CoAP request data used by cooperative handlers.
#[derive(Debug)]
pub struct RequestParts<'a> {
    pub(crate) code: u8,
    pub(crate) path: UriPath,
    pub(crate) accepts: Accepts,
    pub(crate) content_format: Option<u16>,
    pub(crate) invalid_option: Option<InvalidOption>,
    pub(crate) payload: &'a [u8],
}

impl<'a> RequestParts<'a> {
    /// Build request parts from already-decoded fields.
    pub fn new(
        code: u8,
        path: &[&str],
        accept: Option<u16>,
        content_format: Option<u16>,
        payload: &'a [u8],
    ) -> Result<Self, Error> {
        let mut request = Self {
            code,
            path: UriPath::new(),
            accepts: Accepts::from_option(accept),
            content_format,
            invalid_option: None,
            payload,
        };
        for segment in path {
            request.push_path_segment(segment)?;
        }
        Ok(request)
    }

    /// Extract method, URI path, content negotiation options, and payload from a readable message.
    pub fn from_message<M>(message: &'a M) -> Result<Self, Error>
    where
        M: ReadableMessage + ?Sized,
    {
        let mut request = Self {
            code: message.code().into(),
            path: UriPath::new(),
            accepts: Accepts::new(),
            content_format: None,
            invalid_option: None,
            payload: message.payload(),
        };

        for opt in message.options() {
            match opt.number() {
                option::URI_PATH => {
                    let Some(segment) = opt.value_str() else {
                        return Err(Error::bad_request(Problem::InvalidUriPath));
                    };
                    request.push_path_segment(segment)?;
                }
                option::ACCEPT => {
                    let Some(accept) = opt.value_uint() else {
                        return Err(Error::bad_request(Problem::InvalidAccept));
                    };
                    request.accepts.push(accept);
                }
                option::CONTENT_FORMAT => {
                    let Some(content_format) = opt.value_uint() else {
                        return Err(Error::bad_request(Problem::InvalidContentFormat));
                    };
                    if request.content_format.is_some() {
                        if request.invalid_option.is_none() {
                            request.invalid_option = Some(InvalidOption::DuplicateContentFormat);
                        }
                        continue;
                    }
                    request.content_format = Some(content_format);
                }
                option::URI_HOST | option::URI_PORT => {}
                number
                    if option::get_criticality(number) == option::Criticality::Critical
                        && request.invalid_option.is_none() =>
                {
                    request.invalid_option = Some(InvalidOption::UnknownCritical(number));
                }
                _ => {}
            }
        }

        Ok(request)
    }

    /// Request method code.
    pub const fn code(&self) -> u8 {
        self.code
    }

    /// URI path segments.
    pub fn path(&self) -> &str {
        self.path.as_str()
    }

    /// Request payload.
    pub const fn payload(&self) -> &'a [u8] {
        self.payload
    }

    pub(crate) fn accepts(&self, content_format: u16) -> Result<(), Error> {
        self.accepts.accepts(content_format)
    }

    pub(crate) fn check_options(&self) -> Result<(), Error> {
        match self.invalid_option {
            Some(InvalidOption::DuplicateContentFormat) => {
                Err(Error::bad_request(Problem::DuplicateContentFormat))
            }
            Some(InvalidOption::UnknownCritical(number)) => Err(Error::new(
                code::BAD_OPTION,
                Problem::UnknownCriticalOption { number },
            )),
            None => Ok(()),
        }
    }

    fn push_path_segment(&mut self, segment: &str) -> Result<(), Error> {
        if segment.contains('/') {
            return Err(Error::bad_request(Problem::InvalidUriPath));
        }
        self.path
            .push('/')
            .map_err(|_| Error::request_entity_too_large(Problem::UriPathTooLong))?;
        self.path
            .push_str(segment)
            .map_err(|_| Error::request_entity_too_large(Problem::UriPathTooLong))
    }

    pub(crate) fn relative_to(&self, base: &str) -> Option<&str> {
        if base.is_empty() {
            return Some(self.path.as_str());
        }
        if self.path.as_str() == base {
            return Some("");
        }
        let tail = self.path.as_str().strip_prefix(base)?;
        tail.starts_with('/').then_some(tail)
    }
}

#[derive(Debug, Clone, Copy)]
pub(crate) enum InvalidOption {
    DuplicateContentFormat,
    UnknownCritical(u16),
}

impl defmt::Format for RequestParts<'_> {
    fn format(&self, fmt: defmt::Formatter<'_>) {
        defmt::write!(
            fmt,
            "RequestParts {{ code: {=u8}, path: {=str}, accept_present: {=bool}, accept: {=u16}, content_format_present: {=bool}, content_format: {=u16}, payload_len: {=usize} }}",
            self.code,
            self.path.as_str(),
            self.accepts.is_present(),
            self.accepts.first().unwrap_or(0),
            self.content_format.is_some(),
            self.content_format.unwrap_or(0),
            self.payload.len()
        )
    }
}

/// CoAP response data produced by cooperative handlers.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Response<'a> {
    /// CoAP response code.
    pub code: u8,
    /// Optional CoAP Content-Format value.
    pub content_format: Option<u16>,
    /// Response payload.
    pub payload: &'a [u8],
}

impl Response<'_> {
    /// Render this response into a writable CoAP message.
    pub fn write_to<M: MinimalWritableMessage>(
        &self,
        message: &mut M,
    ) -> Result<(), M::UnionError> {
        message.set_code(M::Code::new(self.code).map_err(M::convert_code_error)?);
        if let Some(content_format) = self.content_format {
            message
                .add_option_uint(
                    M::OptionNumber::new(option::CONTENT_FORMAT)
                        .map_err(M::convert_option_number_error)?,
                    content_format,
                )
                .map_err(M::convert_add_option_error)?;
        }
        message
            .set_payload(self.payload)
            .map_err(M::convert_set_payload_error)
    }
}

impl defmt::Format for Response<'_> {
    fn format(&self, fmt: defmt::Formatter<'_>) {
        defmt::write!(
            fmt,
            "Response {{ code: {=u8}, content_format_present: {=bool}, content_format: {=u16}, payload_len: {=usize} }}",
            self.code,
            self.content_format.is_some(),
            self.content_format.unwrap_or(0),
            self.payload.len()
        )
    }
}

/// Handler outcome.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Outcome<'a> {
    /// Request path is outside this handler's route.
    Unhandled,
    /// Request was handled without changing settings.
    Handled(Response<'a>),
    /// Request changed one exact leaf.
    Changed {
        /// Changed Miniconf leaf key.
        key: ChangedKey,
        /// CoAP response to send.
        response: Response<'a>,
    },
}

impl Outcome<'_> {
    /// Return the response, if any.
    pub const fn response(&self) -> Option<Response<'_>> {
        match self {
            Self::Unhandled => None,
            Self::Handled(response) | Self::Changed { response, .. } => Some(*response),
        }
    }
}

impl defmt::Format for Outcome<'_> {
    fn format(&self, fmt: defmt::Formatter<'_>) {
        match self {
            Self::Unhandled => defmt::write!(fmt, "Outcome::Unhandled"),
            Self::Handled(response) => defmt::write!(fmt, "Outcome::Handled({})", response),
            Self::Changed { key, response } => defmt::write!(
                fmt,
                "Outcome::Changed {{ key: {}, response: {} }}",
                key,
                response
            ),
        }
    }
}

/// Problem details preserved in error responses.
#[derive(defmt::Format, Debug, Clone, Copy, PartialEq, Eq)]
pub enum Problem {
    /// URI path option was not valid UTF-8.
    InvalidUriPath,
    /// Accept option could not be decoded as a CoAP uint.
    InvalidAccept,
    /// Content-Format option could not be decoded as a CoAP uint.
    InvalidContentFormat,
    /// More than one Content-Format option was present.
    DuplicateContentFormat,
    /// An unknown critical CoAP option was present.
    UnknownCriticalOption {
        /// CoAP option number.
        number: u16,
    },
    /// URI path had more segments than this handler captured.
    UriPathTooLong,
    /// A response payload exceeded the optional handler adapter buffer.
    PayloadTooLong,
    /// Request had more Accept options than this handler captures.
    TooManyAcceptOptions,
    /// Request path names no static Miniconf resource.
    NotFound {
        /// Depth reached before lookup failed.
        depth: usize,
    },
    /// Request path continues below a leaf.
    TooLong {
        /// Depth of the leaf under which the request continued.
        depth: usize,
    },
    /// Request path names a known branch, but this route handles leaves only.
    NonLeaf {
        /// Depth of the branch resource.
        depth: usize,
    },
    /// Static schema contains the leaf, but runtime state makes it absent.
    Absent {
        /// Depth of the runtime-absent leaf.
        depth: usize,
    },
    /// Runtime access policy denied the operation.
    Access {
        /// Operation being performed.
        op: Operation,
        /// Error text from Miniconf.
        message: &'static str,
    },
    /// Request method is not supported here.
    MethodNotAllowed,
    /// Request `Accept` option does not allow this route's representation.
    NotAcceptable,
    /// Request payload Content-Format is not supported here.
    UnsupportedContentFormat,
    /// Payload could not be decoded.
    BadPayload,
    /// A read-side value serialization failed.
    Serialization,
}

/// CoAP operation being performed.
#[derive(defmt::Format, Debug, Clone, Copy, PartialEq, Eq)]
pub enum Operation {
    /// Read operation.
    Read,
    /// Write operation.
    Write,
}

/// CoAP handler error.
#[derive(defmt::Format, Debug, Clone, Copy, PartialEq, Eq)]
pub struct Error {
    /// CoAP response code.
    pub code: u8,
    /// Machine-readable problem.
    pub problem: Problem,
}

impl Error {
    pub(crate) const fn new(code: u8, problem: Problem) -> Self {
        Self { code, problem }
    }

    pub(crate) const fn bad_request(problem: Problem) -> Self {
        Self::new(code::BAD_REQUEST, problem)
    }

    pub(crate) const fn request_entity_too_large(problem: Problem) -> Self {
        Self::new(code::REQUEST_ENTITY_TOO_LARGE, problem)
    }

    pub(crate) fn response<'a>(self, buf: &'a mut [u8]) -> Response<'a> {
        #[cfg(feature = "json-core")]
        {
            match problem_json(self.problem, buf) {
                Ok(len) => Response {
                    code: self.code,
                    content_format: Some(format::JSON),
                    payload: &buf[..len],
                },
                Err(_) => Response {
                    code: self.code,
                    content_format: None,
                    payload: b"",
                },
            }
        }
        #[cfg(not(feature = "json-core"))]
        {
            let _ = buf;
            Response {
                code: self.code,
                content_format: None,
                payload: b"",
            }
        }
    }

    #[cfg(feature = "json-core")]
    pub(crate) fn write_json_response_to<M: MutableWritableMessage>(
        self,
        message: &mut M,
        max_len: usize,
    ) -> Result<(), M::UnionError> {
        self.write_problem_to(message, format::JSON, max_len, |error, buf| {
            problem_json(error.problem, buf).ok()
        })
    }

    #[cfg(feature = "cbor")]
    pub(crate) fn cbor_response<'a>(self, buf: &'a mut [u8]) -> Response<'a> {
        match problem_cbor(self, buf) {
            Ok(len) => Response {
                code: self.code,
                content_format: Some(format::CONCISE_PROBLEM_CBOR),
                payload: &buf[..len],
            },
            Err(_) => Response {
                code: self.code,
                content_format: None,
                payload: b"",
            },
        }
    }

    #[cfg(feature = "cbor")]
    pub(crate) fn write_cbor_response_to<M: MutableWritableMessage>(
        self,
        message: &mut M,
        max_len: usize,
    ) -> Result<(), M::UnionError> {
        self.write_problem_to(
            message,
            format::CONCISE_PROBLEM_CBOR,
            max_len,
            |error, buf| problem_cbor(error, buf).ok(),
        )
    }

    #[cfg(any(feature = "json-core", feature = "cbor"))]
    fn write_problem_to<M, F>(
        self,
        message: &mut M,
        content_format: u16,
        max_len: usize,
        encode: F,
    ) -> Result<(), M::UnionError>
    where
        M: MutableWritableMessage,
        F: FnOnce(Self, &mut [u8]) -> Option<usize>,
    {
        message.set_code(M::Code::new(self.code).map_err(M::convert_code_error)?);
        message
            .add_option_uint(
                M::OptionNumber::new(option::CONTENT_FORMAT)
                    .map_err(M::convert_option_number_error)?,
                content_format,
            )
            .map_err(M::convert_add_option_error)?;
        let payload = message
            .payload_mut_with_len(max_len)
            .map_err(M::convert_set_payload_error)?;
        let len = encode(self, payload).unwrap_or(0);
        message.truncate(len).map_err(M::convert_set_payload_error)
    }
}

#[cfg(feature = "json-core")]
fn problem_json(problem: Problem, buf: &mut [u8]) -> Result<usize, fmt::Error> {
    let mut out = SliceWriter::new(buf);
    match problem {
        Problem::InvalidUriPath
        | Problem::InvalidAccept
        | Problem::InvalidContentFormat
        | Problem::DuplicateContentFormat
        | Problem::UriPathTooLong
        | Problem::PayloadTooLong
        | Problem::TooManyAcceptOptions
        | Problem::MethodNotAllowed
        | Problem::NotAcceptable
        | Problem::UnsupportedContentFormat
        | Problem::BadPayload
        | Problem::Serialization => write_kind(&mut out, problem_kind(problem))?,
        Problem::UnknownCriticalOption { number } => {
            write!(
                out,
                "{{\"kind\":\"unknown_critical_option\",\"number\":{number}}}"
            )?;
        }
        Problem::NotFound { depth } => write_depth(&mut out, "not_found", depth)?,
        Problem::TooLong { depth } => write_depth(&mut out, "too_long", depth)?,
        Problem::NonLeaf { depth } => write_depth(&mut out, "non_leaf", depth)?,
        Problem::Absent { depth } => write_depth(&mut out, "absent", depth)?,
        Problem::Access { op, message } => {
            write!(
                out,
                "{{\"kind\":\"access\",\"op\":\"{}\",\"message\":",
                operation_name(op)
            )?;
            write_json_string_truncated(&mut out, message, 1)?;
            out.push('}')?;
        }
    }
    Ok(out.len())
}

#[cfg(feature = "cbor")]
fn problem_cbor(error: Error, buf: &mut [u8]) -> Result<usize, encode::Error<EndOfSlice>> {
    const MINICONF_PROBLEM_KEY: &str = "tag:quartiq.de,2026:miniconf";

    let mut cursor = encode::write::Cursor::new(buf);
    {
        let kind = problem_kind(error.problem);
        let mut encoder = CborEncoder::new(&mut cursor);
        encoder
            .map(3)?
            .int(CborInt::from(-1i8))?
            .str(kind)?
            .int(CborInt::from(-4i8))?
            .u8(error.code)?
            .str(MINICONF_PROBLEM_KEY)?
            .map(problem_detail_len(error.problem))?
            .str("kind")?
            .str(kind)?;

        match error.problem {
            Problem::UnknownCriticalOption { number } => {
                encoder.str("number")?.u16(number)?;
            }
            Problem::NotFound { depth }
            | Problem::TooLong { depth }
            | Problem::NonLeaf { depth }
            | Problem::Absent { depth } => {
                encoder.str("depth")?.u64(depth as u64)?;
            }
            Problem::Access { op, message } => {
                encoder
                    .str("op")?
                    .str(operation_name(op))?
                    .str("message")?
                    .str(message)?;
            }
            _ => {}
        }
    }
    Ok(cursor.position())
}

#[cfg(feature = "cbor")]
const fn problem_detail_len(problem: Problem) -> u64 {
    match problem {
        Problem::UnknownCriticalOption { .. }
        | Problem::NotFound { .. }
        | Problem::TooLong { .. }
        | Problem::NonLeaf { .. }
        | Problem::Absent { .. } => 2,
        Problem::Access { .. } => 3,
        _ => 1,
    }
}

#[cfg(any(feature = "json-core", feature = "cbor"))]
const fn problem_kind(problem: Problem) -> &'static str {
    match problem {
        Problem::InvalidUriPath => "invalid_uri_path",
        Problem::InvalidAccept => "invalid_accept",
        Problem::InvalidContentFormat => "invalid_content_format",
        Problem::DuplicateContentFormat => "duplicate_content_format",
        Problem::UnknownCriticalOption { .. } => "unknown_critical_option",
        Problem::UriPathTooLong => "uri_path_too_long",
        Problem::PayloadTooLong => "payload_too_long",
        Problem::TooManyAcceptOptions => "too_many_accept_options",
        Problem::NotFound { .. } => "not_found",
        Problem::TooLong { .. } => "too_long",
        Problem::NonLeaf { .. } => "non_leaf",
        Problem::Absent { .. } => "absent",
        Problem::Access { .. } => "access",
        Problem::MethodNotAllowed => "method_not_allowed",
        Problem::NotAcceptable => "not_acceptable",
        Problem::UnsupportedContentFormat => "unsupported_content_format",
        Problem::BadPayload => "bad_payload",
        Problem::Serialization => "serialization",
    }
}

#[cfg(any(feature = "json-core", feature = "cbor"))]
const fn operation_name(op: Operation) -> &'static str {
    match op {
        Operation::Read => "read",
        Operation::Write => "write",
    }
}

#[cfg(feature = "json-core")]
struct SliceWriter<'a> {
    buf: &'a mut [u8],
    len: usize,
}

#[cfg(feature = "json-core")]
impl<'a> SliceWriter<'a> {
    fn new(buf: &'a mut [u8]) -> Self {
        Self { buf, len: 0 }
    }

    const fn len(&self) -> usize {
        self.len
    }

    fn remaining(&self) -> usize {
        self.buf.len() - self.len
    }

    fn push(&mut self, value: char) -> fmt::Result {
        self.write_char(value)
    }
}

#[cfg(feature = "json-core")]
impl fmt::Write for SliceWriter<'_> {
    fn write_str(&mut self, value: &str) -> fmt::Result {
        let bytes = value.as_bytes();
        let Some(dst) = self.buf.get_mut(self.len..self.len + bytes.len()) else {
            return Err(fmt::Error);
        };
        dst.copy_from_slice(bytes);
        self.len += bytes.len();
        Ok(())
    }
}

#[cfg(feature = "json-core")]
fn write_kind(out: &mut impl fmt::Write, kind: &str) -> fmt::Result {
    write!(out, "{{\"kind\":\"{}\"}}", kind)
}

#[cfg(feature = "json-core")]
fn write_depth(out: &mut impl fmt::Write, kind: &str, depth: usize) -> fmt::Result {
    write!(out, "{{\"kind\":\"{}\",\"depth\":{}}}", kind, depth)
}

#[cfg(feature = "json-core")]
fn write_json_string_truncated(
    out: &mut SliceWriter<'_>,
    value: &str,
    reserve: usize,
) -> fmt::Result {
    out.push('"')?;
    for byte in value.bytes() {
        let escaped = match byte {
            b'"' => "\\\"",
            b'\\' => "\\\\",
            0x08 => "\\b",
            0x0c => "\\f",
            b'\n' => "\\n",
            b'\r' => "\\r",
            b'\t' => "\\t",
            _ => "",
        };
        if !escaped.is_empty() {
            if out.remaining() <= reserve + 1 || escaped.len() > out.remaining() - reserve - 1 {
                break;
            }
            out.write_str(escaped)?;
            continue;
        }
        match byte {
            0x00..=0x1f => {
                if out.remaining() <= reserve + 1 || 6 > out.remaining() - reserve - 1 {
                    break;
                }
                write!(out, "\\u{:04x}", byte)?;
            }
            _ => {
                if out.remaining() <= reserve + 1 {
                    break;
                }
                out.push(char::from(byte))?;
            }
        }
    }
    out.push('"')
}

impl RenderableOnMinimal for Error {
    type Error<IE: RenderableOnMinimal + fmt::Debug> = IE;

    fn render<M: MinimalWritableMessage>(
        self,
        message: &mut M,
    ) -> Result<(), Self::Error<M::UnionError>> {
        let mut buf = [0; 96];
        self.response(&mut buf).write_to(message)
    }
}

impl From<Infallible> for Error {
    fn from(value: Infallible) -> Self {
        match value {}
    }
}