• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

moonbitlang / x / 301

10 Dec 2024 06:19AM UTC coverage: 85.204% (-2.6%) from 87.841%
301

Pull #78

github

web-flow
Merge b830031f4 into 91f0fdf48
Pull Request #78: feat: new package encoding

105 of 161 new or added lines in 3 files covered. (65.22%)

124 existing lines in 29 files now uncovered.

1169 of 1372 relevant lines covered (85.2%)

434.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.71
/fs/fs.mbt
1
// Copyright 2024 International Digital Economy Academy
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15
///|
16
pub(all) type! IOError {
17
  NotFound(String)
18
}
19

20
///|
21
pub impl Show for IOError with output(self, logger) {
22
  logger.write_string(self.to_string())
4✔
23
}
24

25
///|
26
fn IOError::to_string(self : IOError) -> String {
27
  match self {
4✔
28
    IOError::NotFound(path) => "`\{path}` does not exist"
4✔
29
  }
30
}
31

32
///| Writes a string to a file.
33
///
34
/// # Parameters
35
/// - `path`: A `String` representing the file path.
36
/// - `content`: A `String` containing the content to be written to the file.
37
pub fn write_string_to_file(path~ : String, content~ : String) -> Unit {
38
  @ffi.write_string_to_file(path, content)
1✔
39
}
40

41
///| Writes an array of bytes to a file at the specified path.
42
///
43
/// # Parameters
44
///
45
/// - `path` : The path to the file where the bytes will be written.
46
/// - `content` : An array of bytes to be written to the file.
47
pub fn write_bytes_to_file(path~ : String, content~ : Bytes) -> Unit {
48
  @ffi.write_bytes_to_file(path, content)
1✔
49
}
50

51
///| Checks if a path exists.
52
///
53
/// # Parameters
54
/// - `path`: A `String` representing the file path.
55
///
56
/// # Returns
57
/// A boolean indicating whether the path exists.
58
pub fn path_exists(path~ : String) -> Bool {
59
  @ffi.path_exists(path)
22✔
60
}
61

62
///| Reads the entire contents of a file into a string.
63
///
64
/// # Parameters
65
/// - `path`: A `String` representing the file path.
66
///
67
/// # Returns
68
/// A `String` containing the file contents if the file exists, otherwise raises an error.
69
pub fn read_file_to_string(path~ : String) -> String! {
70
  guard path_exists(path~) else { raise IOError::NotFound(path) }
1✔
71
  @ffi.read_file_to_string(path)
1✔
72
}
73

74
///| Reads the content of a file specified by the given path and returns its
75
/// content as an array of bytes. If the file does not exist, an error is raised.
76
///
77
/// # Parameters
78
///
79
/// - `path` : The path to the file to be read.
80
///
81
/// # Returns
82
///
83
/// - An array of bytes representing the content of the file.
84
pub fn read_file_to_bytes(path~ : String) -> Bytes! {
UNCOV
85
  guard path_exists(path~) else { raise IOError::NotFound(path) }
×
86
  @ffi.read_file_to_bytes(path)
1✔
87
}
88

89
///| Reads the contents of a directory and returns an array of filenames.
90
///
91
/// # Parameters
92
///
93
/// - `path` : The path to the directory to be read.
94
///
95
/// # Returns
96
///
97
/// - An array of strings representing the file name and directory name in the directory.
98
pub fn read_dir(path~ : String) -> Array[String]! {
99
  guard path_exists(path~) else { raise IOError::NotFound(path) }
1✔
100
  @ffi.read_dir(path)
1✔
101
}
102

103
///| Creates a directory at the specified path.
104
/// Note: nested directories are not supported for native backend
105
///
106
/// # Parameters
107
///
108
/// - `path` : The path where the directory should be created.
109
pub fn create_dir(path~ : String) -> Unit {
110
  @ffi.create_dir(path)
1✔
111
}
112

113
///| Checks if the given path is a directory.
114
///
115
/// # Parameters
116
///
117
/// - `path` : The string representing the path to be checked.
118
///
119
/// # Returns
120
///
121
/// - `Bool` : `true` if the path is a directory, `false` otherwise.
122
pub fn is_dir(path~ : String) -> Bool! {
UNCOV
123
  guard path_exists(path~) else { raise IOError::NotFound(path) }
×
124
  @ffi.is_dir(path)
1✔
125
}
126

127
///| Check if the given path points to a file.
128
///
129
/// # Parameters
130
///
131
/// - `path` : The string representing the path to be checked.
132
///
133
/// # Returns
134
///
135
/// - `Bool` : `true` if the path points to a file, `false` otherwise.
136
pub fn is_file(path~ : String) -> Bool! {
UNCOV
137
  guard path_exists(path~) else { raise IOError::NotFound(path) }
×
138
  @ffi.is_file(path)
1✔
139
}
140

141
///| Removes a directory at the specified path.
142
///
143
/// # Parameters
144
///
145
/// - `path` : The string path to the directory that needs to be removed.
146
pub fn remove_dir(path~ : String) -> Unit! {
147
  guard path_exists(path~) else { raise IOError::NotFound(path) }
1✔
148
  @ffi.remove_dir(path)
1✔
149
}
150

151
///| Removes a file at the specified path.
152
///
153
/// # Parameters
154
///
155
/// - `path` : The path to the file that needs to be removed.
156
pub fn remove_file(path~ : String) -> Unit! {
157
  guard path_exists(path~) else { raise IOError::NotFound(path) }
1✔
158
  @ffi.remove_file(path)
2✔
159
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc