bacnet-objects 0.13.0

BACnet object model: traits, database, and standard object types
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
//! File (type 10) object per ASHRAE 135-2020 Clause 12.13.
//!
//! Backs AtomicReadFile and AtomicWriteFile services. Supports both
//! stream-access and record-access modes.

use bacnet_types::enums::{
    ErrorClass, ErrorCode, FileAccessMethod, ObjectType, PropertyIdentifier,
};
use bacnet_types::error::Error;
use bacnet_types::primitives::{Date, ObjectIdentifier, PropertyValue, Time};
use std::borrow::Cow;
use std::sync::Arc;

use crate::clock::ClockReader;
use crate::common::{self, read_identity_properties};
use crate::traits::BACnetObject;

mod metadata;
mod resize;

// ---------------------------------------------------------------------------
// File storage (the Clause 14 service data behind a File object)
// ---------------------------------------------------------------------------

/// Default growth cap, in octets, for network writes to one File object.
///
/// Clause 14.2 requires a write whose 'File Start Position' exceeds the
/// file size to extend the file to that size, and the position is a signed
/// 32-bit INTEGER, so an unbounded implementation would zero-fill up to
/// 2 GiB from one small request. Clause 18 uses FILE_FULL for reaching a
/// File object's configured capacity, distinct from exhausting storage
/// space or memory.
pub const DEFAULT_MAX_FILE_SIZE: u64 = 1_048_576;

/// Default growth cap, in records, for network writes to one record-access
/// File object.
///
/// Bounds the record vector independently of its payload octets, which
/// [`DEFAULT_MAX_FILE_SIZE`] bounds: extending to a far record index costs
/// a `Vec` header per intervening empty record even when no octet is
/// stored. The value is at most `bacnet_services::common::MAX_DECODED_ITEMS`
/// — today exactly that ceiling — the largest SEQUENCE OF the workspace
/// decoders accept in one ACK, so a record file read back at the cap still
/// decodes on the client side.
pub const DEFAULT_MAX_RECORD_COUNT: u64 = 10_000;

/// Ceiling for [`FileObject::set_max_record_count`]; see
/// [`DEFAULT_MAX_RECORD_COUNT`] for why it is the decoder's limit.
const MAX_RECORD_CAP: u64 = DEFAULT_MAX_RECORD_COUNT;

/// Trusted local configuration for the built-in in-memory File payload.
///
/// This narrow capability is separate from [`FileStorage`]: configuration may
/// preload data regardless of `Read_Only` or the later network-write growth
/// caps, while AtomicWriteFile continues to use `FileStorage` and enforce both.
/// Payload operations reject a shape that does not match the active
/// `File_Access_Method` before mutating the object.
#[doc(hidden)]
pub trait FileConfiguration: Send + Sync {
    /// Select the active File access method without converting either payload.
    fn set_access_method(&mut self, method: FileAccessMethod);

    /// Replace the stream payload through the File object's accounting path.
    fn set_stream_data(&mut self, data: Vec<u8>) -> Result<(), Error>;

    /// Borrow the stream payload when stream access is active.
    fn stream_data(&self) -> Result<&[u8], Error>;

    /// Replace the record payload through the File object's accounting path.
    fn set_record_data(&mut self, records: Vec<Vec<u8>>) -> Result<(), Error>;

    /// Borrow the record payload when record access is active.
    fn record_data(&self) -> Result<&[Vec<u8>], Error>;

    /// Set and return the effective octet growth cap.
    fn set_max_file_size(&mut self, max_octets: u64) -> u64;

    /// Set and return the effective record-count growth cap.
    fn set_max_record_count(&mut self, max_records: u64) -> u64;
}

/// One stream-access read window, as AtomicReadFile returns it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FileStreamRead {
    /// The octets read: never more than requested, fewer when the file
    /// ends first (Clause 14.1 Service Procedure).
    pub data: Vec<u8>,
    /// Clause 14.1.3.1 'End Of File'. The built-in empty file reports EOF at
    /// its only valid start; an empty window in a non-empty file does not.
    pub end_of_file: bool,
}

/// One record-access read window, as AtomicReadFile returns it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FileRecordRead {
    /// The records read. Its length is the ACK's 'Returned Record Count'
    /// (Clause 14.1.3.3), which may be less than the requested count.
    pub records: Vec<Vec<u8>>,
    /// 'End Of File' per the Clause 14.1 Service Procedure. The built-in
    /// empty record file reports EOF at its only valid start.
    pub end_of_file: bool,
}

/// Where an AtomicWriteFile write starts.
///
/// Clauses 14.2.2.2 and 14.2.2.3 give 'File Start Position' and 'File
/// Start Record' the special value -1 to append at the end of the file;
/// every other value is an offset from the beginning of the file.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FileWriteStart {
    /// Write at this octet or record offset, extending the file first if
    /// the offset is past the current end (Clause 14.2 Service Procedure).
    At(u64),
    /// Write at the current end of the file.
    Append,
}

/// The octet- and record-addressed storage behind a File object.
///
/// This is the **internal** channel AtomicReadFile and AtomicWriteFile use
/// to reach file contents (Clauses 14.1 and 14.2). It is deliberately not a
/// property: Table 12-16 (Clause 12.13) defines no File Data property, so
/// file contents have no network route other than the File Access Services,
/// and nothing here appears in `Property_List`.
///
/// The server reaches an implementation through
/// [`BACnetObject::file_storage_internal`] and
/// [`BACnetObject::file_storage_internal_mut`]. Every method has a default
/// that refuses with SERVICES / FILE_ACCESS_DENIED — there is no
/// implementation behind it, making the file inaccessible under
/// Clause 18 — and a stream-only implementation overrides only the
/// two stream methods.
///
/// Error contract, using the Clause 14 pairs:
///
/// - SERVICES / INVALID_FILE_START_POSITION when a read starts past the
///   current end (Clause 14.1 Service Procedure). Reading *at* the end is
///   legal and yields an empty window. The built-in empty file reports
///   `end_of_file` TRUE there; non-empty files report FALSE for such a window.
/// - OBJECT / FILE_FULL when a write would grow the file past the
///   most this implementation can hold (Clause 14.2.4.1; Clause 18).
/// - SERVICES / INVALID_FILE_ACCESS_METHOD when the method does not match
///   the object's `File_Access_Method`; only a genuine mismatch, never a
///   missing implementation, reports this.
/// - SERVICES / FILE_ACCESS_DENIED from the defaults above.
///
/// A write that returns `Err` must leave the storage unchanged: the service
/// fails as a whole (Clause 14.2.4), and the server encodes no ACK on
/// the error path. Resolved write positions must fit the ACK's INTEGER, so
/// implementations keep their limit at or below `i32::MAX`.
pub trait FileStorage: Send + Sync {
    /// Read up to `count` octets starting `start` octets into the file.
    fn read_stream(&self, _start: u64, _count: u64) -> Result<FileStreamRead, Error> {
        Err(file_access_denied())
    }

    /// Write `data` at `start`, extending the file first when `start` is
    /// past the end; intervening octets are a local matter. Returns the
    /// octet offset the data was written at, which for
    /// [`FileWriteStart::Append`] is the previous file size.
    fn write_stream(&mut self, _start: FileWriteStart, _data: &[u8]) -> Result<u64, Error> {
        Err(file_access_denied())
    }

    /// Read up to `count` records starting at record `start`.
    fn read_records(&self, _start: u64, _count: u64) -> Result<FileRecordRead, Error> {
        Err(file_access_denied())
    }

    /// Write `records` starting at record `start`, replacing records in
    /// place and extending the file first when `start` is past the end.
    /// Returns the record index the data was written at.
    fn write_records(
        &mut self,
        _start: FileWriteStart,
        _records: &[Vec<u8>],
    ) -> Result<u64, Error> {
        Err(file_access_denied())
    }
}

fn invalid_file_access_method() -> Error {
    common::protocol_error(ErrorClass::SERVICES, ErrorCode::INVALID_FILE_ACCESS_METHOD)
}

fn file_access_denied() -> Error {
    common::protocol_error(ErrorClass::SERVICES, ErrorCode::FILE_ACCESS_DENIED)
}

fn invalid_file_start_position() -> Error {
    common::protocol_error(ErrorClass::SERVICES, ErrorCode::INVALID_FILE_START_POSITION)
}

fn file_full() -> Error {
    common::protocol_error(ErrorClass::OBJECT, ErrorCode::FILE_FULL)
}

/// Resolved positions travel in the ACK as a BACnet INTEGER; caps above
/// `i32::MAX` could produce one that does not fit.
const MAX_REPRESENTABLE: u64 = i32::MAX as u64;

fn octet_total(records: &[Vec<u8>]) -> u64 {
    records.iter().map(|r| r.len() as u64).sum()
}

// ---------------------------------------------------------------------------
// FileObject (type 10)
// ---------------------------------------------------------------------------

/// BACnet File object.
///
/// Represents a file accessible via AtomicReadFile / AtomicWriteFile services.
/// The `file_access_method` determines whether the file is accessed as a
/// byte stream ([`FileAccessMethod::STREAM_ACCESS`]) or as a sequence of
/// fixed-length records ([`FileAccessMethod::RECORD_ACCESS`]).
pub struct FileObject {
    oid: ObjectIdentifier,
    name: String,
    description: String,
    file_type: String,
    file_size: u64,
    modification_date: (Date, Time),
    archive: bool,
    read_only: bool,
    /// Raw `BACnetFileAccessMethod` value; named in [`FileAccessMethod`]
    /// (record-access = 0, stream-access = 1 per the Clause 21 production).
    file_access_method: u32,
    /// Record count (only meaningful for record-access files).
    record_count: Option<u64>,
    /// Stream data (used when file_access_method == STREAM_ACCESS).
    data: Vec<u8>,
    /// Record data (used when file_access_method == RECORD_ACCESS).
    records: Vec<Vec<u8>>,
    /// Growth cap in octets for network writes; not a BACnet property.
    max_file_size: u64,
    /// Growth cap in records for network writes; not a BACnet property.
    max_record_count: u64,
    /// Database-owned coherent clock source used at successful mutations.
    clock: Option<Arc<dyn ClockReader>>,
}

impl FileObject {
    /// Create a new File object.
    ///
    /// Defaults to stream access (file_access_method = STREAM_ACCESS), empty
    /// data, not read-only, archive = false.
    pub fn new(
        instance: u32,
        name: impl Into<String>,
        file_type: impl Into<String>,
    ) -> Result<Self, Error> {
        let oid = ObjectIdentifier::new(ObjectType::FILE, instance)?;
        Ok(Self {
            oid,
            name: name.into(),
            description: String::new(),
            file_type: file_type.into(),
            file_size: 0,
            modification_date: crate::clock::UNSPECIFIED_DATETIME,
            archive: false,
            read_only: false,
            file_access_method: FileAccessMethod::STREAM_ACCESS.to_raw(),
            record_count: None,
            data: Vec::new(),
            records: Vec::new(),
            max_file_size: DEFAULT_MAX_FILE_SIZE,
            max_record_count: DEFAULT_MAX_RECORD_COUNT,
            clock: None,
        })
    }

    /// Set the description.
    pub fn set_description(&mut self, desc: impl Into<String>) {
        self.description = desc.into();
    }

    /// Set the file type string.
    pub fn set_file_type(&mut self, ft: impl Into<String>) {
        self.file_type = ft.into();
    }

    /// Set stream data; File_Size follows it unless the object is
    /// RECORD_ACCESS (any other access-method value, recognised or not,
    /// is the stream channel, as in
    /// [`set_file_access_method`](Self::set_file_access_method)).
    pub fn set_data(&mut self, data: Vec<u8>) {
        self.data = data;
        if self.file_access_method != FileAccessMethod::RECORD_ACCESS.to_raw() {
            self.file_size = self.data.len() as u64;
        }
        self.mark_modified();
    }

    /// Get a reference to the stream data.
    pub fn data(&self) -> &[u8] {
        &self.data
    }

    /// Set the file access method. Accepts the raw `BACnetFileAccessMethod`
    /// value: [`FileAccessMethod::STREAM_ACCESS`] (1) or
    /// [`FileAccessMethod::RECORD_ACCESS`] (0).
    ///
    /// File_Size and Record_Count are recomputed from the channel the
    /// object switches to: Table 12-16 footnote 2 has Record_Count present
    /// only under RECORD_ACCESS, and File_Size counts that channel's octets.
    pub fn set_file_access_method(&mut self, method: u32) {
        let old_file_size = self.file_size;
        self.file_access_method = method;
        if method == FileAccessMethod::RECORD_ACCESS.to_raw() {
            self.record_count = Some(self.records.len() as u64);
            self.file_size = octet_total(&self.records);
        } else {
            self.record_count = None;
            self.file_size = self.data.len() as u64;
        }
        if self.file_size != old_file_size {
            self.mark_modified();
        }
    }

    /// Set the records; Record_Count and File_Size follow them while the
    /// object is RECORD_ACCESS (Table 12-16 footnote 2).
    pub fn set_records(&mut self, records: Vec<Vec<u8>>) {
        self.records = records;
        if self.file_access_method == FileAccessMethod::RECORD_ACCESS.to_raw() {
            self.record_count = Some(self.records.len() as u64);
            self.file_size = octet_total(&self.records);
        }
        self.mark_modified();
    }

    /// Get a reference to the records.
    pub fn records(&self) -> &[Vec<u8>] {
        &self.records
    }

    /// Set the modification date.
    pub fn set_modification_date(&mut self, date: Date, time: Time) {
        let modification_date = (date, time);
        if self.modification_date != modification_date {
            self.modification_date = modification_date;
            self.archive = false;
        }
    }

    /// Set the archive flag.
    pub fn set_archive(&mut self, archive: bool) {
        self.archive = archive;
    }

    /// Set the read-only flag.
    pub fn set_read_only(&mut self, read_only: bool) {
        self.read_only = read_only;
    }

    /// Get the file size in bytes.
    pub fn file_size(&self) -> u64 {
        self.file_size
    }

    /// Get the archive flag.
    pub fn archive(&self) -> bool {
        self.archive
    }

    /// Get the read-only flag.
    pub fn read_only(&self) -> bool {
        self.read_only
    }

    /// Set the growth cap, in octets, for network writes to this file.
    ///
    /// The cap bounds what AtomicWriteFile can add; it never invalidates
    /// contents preloaded through [`set_data`](Self::set_data), which may
    /// exceed it and still be overwritten in place. Clamped to `i32::MAX`
    /// so every resolved write position fits the ACK.
    pub fn set_max_file_size(&mut self, max_octets: u64) {
        self.max_file_size = max_octets.min(MAX_REPRESENTABLE);
    }

    /// The growth cap, in octets, for network writes to this file.
    pub fn max_file_size(&self) -> u64 {
        self.max_file_size
    }

    /// Set the growth cap, in records, for network writes to this file.
    ///
    /// Same growth-only semantics as
    /// [`set_max_file_size`](Self::set_max_file_size): records preloaded
    /// through [`set_records`](Self::set_records) are never refused. Clamped
    /// to [`DEFAULT_MAX_RECORD_COUNT`], the decoder ceiling, so a file grown
    /// by network writes stays at or under what one AtomicReadFile-ACK
    /// carries; a larger preloaded file reads back in windows of at most
    /// that many records.
    pub fn set_max_record_count(&mut self, max_records: u64) {
        self.max_record_count = max_records.min(MAX_RECORD_CAP).min(MAX_REPRESENTABLE);
    }

    /// The growth cap, in records, for network writes to this file.
    pub fn max_record_count(&self) -> u64 {
        self.max_record_count
    }

    /// The handler gate is normative; this keeps `data` and `records` from
    /// both filling up when a non-handler caller reaches the storage.
    fn require_access(&self, method: FileAccessMethod) -> Result<(), Error> {
        if self.file_access_method == method.to_raw() {
            Ok(())
        } else {
            Err(invalid_file_access_method())
        }
    }

    fn mark_modified(&mut self) {
        self.modification_date = crate::clock::stamp_datetime(self.clock.as_deref());
        self.archive = false;
    }
}

impl FileStorage for FileObject {
    fn read_stream(&self, start: u64, count: u64) -> Result<FileStreamRead, Error> {
        self.require_access(FileAccessMethod::STREAM_ACCESS)?;
        let len = self.data.len() as u64;
        if start > len {
            return Err(invalid_file_start_position());
        }
        let end = start.saturating_add(count).min(len);
        Ok(FileStreamRead {
            data: self.data[start as usize..end as usize].to_vec(),
            end_of_file: len == 0 || (end >= len && end > start),
        })
    }

    fn write_stream(&mut self, start: FileWriteStart, data: &[u8]) -> Result<u64, Error> {
        self.require_access(FileAccessMethod::STREAM_ACCESS)?;
        let len = self.data.len() as u64;
        let start = match start {
            FileWriteStart::At(offset) => offset,
            FileWriteStart::Append => len,
        };
        let end = start.checked_add(data.len() as u64).ok_or_else(file_full)?;
        if end > self.max_file_size.max(len) {
            return Err(file_full());
        }
        let (start_idx, end_idx) = (
            usize::try_from(start).map_err(|_| file_full())?,
            usize::try_from(end).map_err(|_| file_full())?,
        );
        let mut updated = std::mem::take(&mut self.data);
        if end_idx > updated.len() {
            updated.resize(end_idx, 0);
        }
        updated[start_idx..end_idx].copy_from_slice(data);
        self.set_data(updated);
        Ok(start)
    }

    fn read_records(&self, start: u64, count: u64) -> Result<FileRecordRead, Error> {
        self.require_access(FileAccessMethod::RECORD_ACCESS)?;
        let len = self.records.len() as u64;
        if start > len {
            return Err(invalid_file_start_position());
        }
        let end = start.saturating_add(count).min(len);
        Ok(FileRecordRead {
            records: self.records[start as usize..end as usize].to_vec(),
            end_of_file: len == 0 || (end >= len && end > start),
        })
    }

    fn write_records(&mut self, start: FileWriteStart, records: &[Vec<u8>]) -> Result<u64, Error> {
        self.require_access(FileAccessMethod::RECORD_ACCESS)?;
        let len = self.records.len() as u64;
        let start = match start {
            FileWriteStart::At(index) => index,
            FileWriteStart::Append => len,
        };
        let end = start
            .checked_add(records.len() as u64)
            .ok_or_else(file_full)?;
        if end > self.max_record_count.max(len) {
            return Err(file_full());
        }
        let (start_idx, end_idx) = (
            usize::try_from(start).map_err(|_| file_full())?,
            usize::try_from(end).map_err(|_| file_full())?,
        );
        // Octet cap on the projected total: existing octets, minus those
        // in the records being replaced, plus the incoming payload.
        let existing = octet_total(&self.records);
        let replaced = octet_total(
            &self.records[start_idx.min(self.records.len())..end_idx.min(self.records.len())],
        );
        let projected = existing - replaced + octet_total(records);
        if projected > self.max_file_size.max(existing) {
            return Err(file_full());
        }
        let mut updated = std::mem::take(&mut self.records);
        if end_idx > updated.len() {
            updated.resize(end_idx, Vec::new());
        }
        updated[start_idx..end_idx].clone_from_slice(records);
        self.set_records(updated);
        Ok(start)
    }
}

impl FileConfiguration for FileObject {
    fn set_access_method(&mut self, method: FileAccessMethod) {
        FileObject::set_file_access_method(self, method.to_raw());
    }

    fn set_stream_data(&mut self, data: Vec<u8>) -> Result<(), Error> {
        self.require_access(FileAccessMethod::STREAM_ACCESS)?;
        FileObject::set_data(self, data);
        Ok(())
    }

    fn stream_data(&self) -> Result<&[u8], Error> {
        self.require_access(FileAccessMethod::STREAM_ACCESS)?;
        Ok(FileObject::data(self))
    }

    fn set_record_data(&mut self, records: Vec<Vec<u8>>) -> Result<(), Error> {
        self.require_access(FileAccessMethod::RECORD_ACCESS)?;
        FileObject::set_records(self, records);
        Ok(())
    }

    fn record_data(&self) -> Result<&[Vec<u8>], Error> {
        self.require_access(FileAccessMethod::RECORD_ACCESS)?;
        Ok(FileObject::records(self))
    }

    fn set_max_file_size(&mut self, max_octets: u64) -> u64 {
        FileObject::set_max_file_size(self, max_octets);
        FileObject::max_file_size(self)
    }

    fn set_max_record_count(&mut self, max_records: u64) -> u64 {
        FileObject::set_max_record_count(self, max_records);
        FileObject::max_record_count(self)
    }
}

impl BACnetObject for FileObject {
    fn object_identifier(&self) -> ObjectIdentifier {
        self.oid
    }

    fn object_name(&self) -> &str {
        &self.name
    }

    fn read_property(
        &self,
        property: PropertyIdentifier,
        array_index: Option<u32>,
    ) -> Result<PropertyValue, Error> {
        // Table 12-16 has no Status_Flags, Reliability or Out_Of_Service (#1064).
        if let Some(result) = read_identity_properties!(self, property, array_index) {
            return result;
        }

        match property {
            p if p == PropertyIdentifier::OBJECT_TYPE => {
                Ok(PropertyValue::Enumerated(ObjectType::FILE.to_raw()))
            }
            p if p == PropertyIdentifier::FILE_TYPE => {
                Ok(PropertyValue::CharacterString(self.file_type.clone()))
            }
            p if p == PropertyIdentifier::FILE_SIZE => Ok(PropertyValue::Unsigned(self.file_size)),
            p if p == PropertyIdentifier::MODIFICATION_DATE => Ok(PropertyValue::List(vec![
                PropertyValue::Date(self.modification_date.0),
                PropertyValue::Time(self.modification_date.1),
            ])),
            p if p == PropertyIdentifier::ARCHIVE => Ok(PropertyValue::Boolean(self.archive)),
            p if p == PropertyIdentifier::READ_ONLY => Ok(PropertyValue::Boolean(self.read_only)),
            p if p == PropertyIdentifier::FILE_ACCESS_METHOD => {
                Ok(PropertyValue::Enumerated(self.file_access_method))
            }
            p if p == PropertyIdentifier::RECORD_COUNT => match self.record_count {
                Some(count) => Ok(PropertyValue::Unsigned(count)),
                None => Err(common::unknown_property_error()),
            },
            _ => Err(common::unknown_property_error()),
        }
    }

    fn write_property(
        &mut self,
        property: PropertyIdentifier,
        _array_index: Option<u32>,
        value: PropertyValue,
        _priority: Option<u8>,
    ) -> Result<(), Error> {
        if let Some(result) = common::write_description(&mut self.description, property, &value) {
            return result;
        }

        match property {
            p if p == PropertyIdentifier::ARCHIVE => {
                if let PropertyValue::Boolean(v) = value {
                    self.archive = v;
                    Ok(())
                } else {
                    Err(common::invalid_data_type_error())
                }
            }
            p if p == PropertyIdentifier::FILE_TYPE => {
                if let PropertyValue::CharacterString(s) = value {
                    self.file_type = s;
                    Ok(())
                } else {
                    Err(common::invalid_data_type_error())
                }
            }
            p if p == PropertyIdentifier::READ_ONLY => {
                // Read-only is typically not writable from BACnet, but the
                // application may need it. Deny remote writes.
                Err(common::write_access_denied_error())
            }
            p if p == PropertyIdentifier::FILE_SIZE => resize::write_stream(self, value),
            p if p == PropertyIdentifier::FILE_ACCESS_METHOD => {
                Err(common::write_access_denied_error())
            }
            p if p == PropertyIdentifier::MODIFICATION_DATE => {
                Err(common::write_access_denied_error())
            }
            p if p == PropertyIdentifier::RECORD_COUNT => {
                if _array_index.is_none() && self.record_count.is_none() {
                    Err(common::unknown_property_error())
                } else {
                    resize::write_records(self, value)
                }
            }
            _ => Err(crate::common::unhandled_write_error(
                self.property_metadata().as_ref(),
                property,
                _array_index,
            )),
        }
    }

    fn property_metadata(&self) -> Cow<'_, [crate::property_metadata::PropertyMetadata]> {
        metadata::for_object(self)
    }

    fn property_list(&self) -> Cow<'static, [PropertyIdentifier]> {
        crate::property_metadata::property_list_from_metadata(self.property_metadata().as_ref())
    }

    fn bind_clock_internal(&mut self, clock: Option<Arc<dyn ClockReader>>) {
        self.clock = clock;
    }

    fn file_configuration_internal(&self) -> Option<&dyn FileConfiguration> {
        Some(self)
    }

    fn file_configuration_internal_mut(&mut self) -> Option<&mut dyn FileConfiguration> {
        Some(self)
    }

    fn file_storage_internal(&self) -> Option<&dyn FileStorage> {
        Some(self)
    }

    fn file_storage_internal_mut(&mut self) -> Option<&mut dyn FileStorage> {
        Some(self)
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod resize_tests;
#[cfg(test)]
mod storage_tests;
#[cfg(test)]
mod tests;