chore: lsp + format

This commit is contained in:
2026-08-04 10:21:18 -04:00
parent fa35c3c326
commit 4533c7f61f
30 changed files with 1151 additions and 678 deletions
+39 -21
View File
@@ -1,5 +1,6 @@
// @lua-module fs FsLib
// @lua-const MAX_READ_BYTES integer 65536 Largest portable whole-file or line read.
// @lua-const MAX_READ_BYTES integer 65536 Largest portable whole-file or line
// read.
#include "../helpers.h"
@@ -11,7 +12,8 @@ constexpr int32_t MAX_READ_BYTES = 65536;
int32_t checkReadLimit(lua_State* state, int index) {
const int32_t maxBytes = checkInt(state, index);
luaL_argcheck(state, maxBytes >= 0 && maxBytes <= MAX_READ_BYTES, index, "exceeds fs.MAX_READ_BYTES");
luaL_argcheck(state, maxBytes >= 0 && maxBytes <= MAX_READ_BYTES, index,
"exceeds fs.MAX_READ_BYTES");
return maxBytes;
}
@@ -25,7 +27,8 @@ int fileSize(lua_State* state) {
const std::string path = checkString(state, 1);
int32_t size = 0;
const Status status = Runtime::from(state)->fs().fileSize(path, size);
if (!status.ok) return pushError(state, status.error);
if (!status.ok)
return pushError(state, status.error);
lua_pushinteger(state, size);
return 1;
}
@@ -34,8 +37,10 @@ int list(lua_State* state, bool directories) {
const std::string path = checkString(state, 1);
std::vector<std::string> names;
const FsProvider& fs = Runtime::from(state)->fs();
const Status status = directories ? fs.listDirs(path, names) : fs.listFiles(path, names);
if (!status.ok) return pushError(state, status.error);
const Status status =
directories ? fs.listDirs(path, names) : fs.listFiles(path, names);
if (!status.ok)
return pushError(state, status.error);
pushStrings(state, names);
return 1;
}
@@ -52,8 +57,10 @@ int readFile(lua_State* state) {
const int32_t maxBytes = checkReadLimit(state, 2);
const std::string path = checkString(state, 1);
std::string content;
const Status status = Runtime::from(state)->fs().readFile(path, maxBytes, content);
if (!status.ok) return pushError(state, status.error);
const Status status =
Runtime::from(state)->fs().readFile(path, maxBytes, content);
if (!status.ok)
return pushError(state, status.error);
pushString(state, content);
return 1;
}
@@ -67,7 +74,8 @@ int readLineAt(lua_State* state) {
bool found = false;
int32_t nextOffset = 0;
std::string line;
const Status status = Runtime::from(state)->fs().readLineAt(path, offset, maxBytes, found, line, nextOffset);
const Status status = Runtime::from(state)->fs().readLineAt(
path, offset, maxBytes, found, line, nextOffset);
if (!status.ok) {
lua_pushnil(state);
lua_pushnil(state);
@@ -96,7 +104,8 @@ int removeTree(lua_State* state) {
int rename(lua_State* state) {
const std::string source = checkString(state, 1);
const std::string destination = checkString(state, 2);
return pushStatus(state, Runtime::from(state)->fs().rename(source, destination));
return pushStatus(state,
Runtime::from(state)->fs().rename(source, destination));
}
int writeFile(lua_State* state) {
@@ -107,7 +116,8 @@ int writeFile(lua_State* state) {
const luaL_Reg FUNCTIONS[] = {
// --- Whether a path exists.
// @param path string Absolute SD-card path; traversal components are rejected.
// @param path string Absolute SD-card path; traversal components are
// rejected.
// @return boolean
{"exists", exists},
// --- Returns the size of a file in bytes.
@@ -132,35 +142,43 @@ const luaL_Reg FUNCTIONS[] = {
{"mkdir", mkdir},
// --- Reads a whole file.
// @param path string Absolute file path.
// @param maxBytes integer Maximum bytes to allocate, up to fs.MAX_READ_BYTES; oversized files fail rather than truncate.
// @param maxBytes integer Maximum bytes to allocate, up to
// fs.MAX_READ_BYTES; oversized files fail rather than truncate.
// @return string|nil content
// @return string|nil error
{"readFile", readFile},
// --- Reads one line starting at a byte offset.
// @param path string Absolute file path.
// @param offset integer Zero-based byte offset; a mid-line offset advances to the next line.
// @param maxBytes integer Maximum line bytes to allocate, up to fs.MAX_READ_BYTES.
// @param offset integer Zero-based byte offset; a mid-line offset advances
// to the next line.
// @param maxBytes integer Maximum line bytes to allocate, up to
// fs.MAX_READ_BYTES.
// @return string|nil line Nil at end of file or on failure.
// @return integer|nil nextOffset Byte offset of the following line.
// @return string|nil error Present when the file cannot be read or the line exceeds maxBytes.
// @return string|nil error Present when the file cannot be read or the line
// exceeds maxBytes.
{"readLineAt", readLineAt},
// --- Removes a file.
// @param path string Absolute file path. Firmware-protected roots cannot be removed.
// @param path string Absolute file path. Firmware-protected roots cannot be
// removed.
// @return true|nil ok
// @return string|nil error
{"remove", remove},
// --- Removes a directory and everything below it.
// @param path string Absolute directory path. Firmware-protected roots cannot be removed.
// @param path string Absolute directory path. Firmware-protected roots
// cannot be removed.
// @return true|nil ok
// @return string|nil error
{"removeTree", removeTree},
// --- Renames a file or directory.
// @param source string Absolute source path.
// @param destination string Absolute destination path, which must not exist.
// @param destination string Absolute destination path, which must not
// exist.
// @return true|nil ok
// @return string|nil error
{"rename", rename},
// --- Atomically replaces the destination or leaves its previous contents intact.
// --- Atomically replaces the destination or leaves its previous contents
// intact.
// @param path string Absolute file path.
// @param content string
// @return true|nil ok
@@ -169,7 +187,7 @@ const luaL_Reg FUNCTIONS[] = {
{nullptr, nullptr},
};
} // namespace
} // namespace
void registerFs(lua_State* state) {
luaL_newlib(state, FUNCTIONS);
@@ -178,5 +196,5 @@ void registerFs(lua_State* state) {
lua_setglobal(state, "fs");
}
} // namespace bindings
} // namespace esp32lua
} // namespace bindings
} // namespace esp32lua