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
// Copyright (c) 2023 Contributors to the Eclipse Foundation
//
// See the NOTICE file(s) distributed with this work for additional
// information regarding copyright ownership.
//
// This program and the accompanying materials are made available under the
// terms of the Apache Software License 2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
// which is available at https://opensource.org/licenses/MIT.
//
// SPDX-License-Identifier: Apache-2.0 OR MIT

//! Relocatable (inter-process shared memory compatible) [`semantic_string::SemanticString`] implementation for
//! [`FileName`]. All modification operations ensure that never an
//! invalid file or path name can be generated. All strings have a fixed size so that the maximum
//! path or file name length the system supports can be stored.
//!
//! # Example
//!
//! ```
//! use iceoryx2_bb_container::semantic_string::SemanticString;
//! use iceoryx2_bb_system_types::file_name::*;
//!
//! let name = FileName::new(b"some_file.txt");
//!
//! let invalid_name = FileName::new(b"no/path/allowed.txt");
//! assert!(invalid_name.is_err());
//! ```

use iceoryx2_bb_container::semantic_string;
use iceoryx2_pal_configuration::FILENAME_LENGTH;

semantic_string! {
  /// Represents a file name. The restriction are choosen in a way that it is platform independent.
  /// This means characters/strings which would be legal on some platforms are forbidden as well.
  name: FileName,
  capacity: FILENAME_LENGTH,
  invalid_content: |value: &[u8]| {
    matches!(value, b"" | b"." | b"..")
  },
  invalid_characters: |value: &[u8]| {
    for c in value {
        match c {
            // linux & windows
            0 => return true,
            b'/' => return true,
            // windows only
            1..=31 => return true,
            b':' => return true,
            b'\\' => return true,
            b'<' => return true,
            b'>' => return true,
            b'"' => return true,
            b'|' => return true,
            b'?' => return true,
            b'*' => return true,
            _ => (),
        }
    }
    false
  },
  comparision: |lhs: &[u8], rhs: &[u8]| {
      *lhs == *rhs
  }
}