chore: clear clippy warnings and tighten the build guard

- base64_encode uses div_ceil plus a slice loop (clippy: manual_div_ceil,
  chunks_exact_to_as_chunks); add known-vector tests.
- macOS-only UserEvent variants no longer warn on Windows.
- needless borrow, needless bool, and a collapsible match arm in the
  Destroyed handler.
- build.ps1 no longer treats the word "powershell" as an updater marker;
  it now checks Start-Process / Invoke-WebRequest instead.
This commit is contained in:
2026-11-03 22:11:03 +08:00
parent c783a6dc56
commit 8221eb5908
2 changed files with 31 additions and 13 deletions

View File

@@ -1,4 +1,4 @@
#requires -Version 5.1 #requires -Version 5.1
<# <#
MD Preview 开发构建(DSH workspace-write / Low 完整性可运行) MD Preview 开发构建(DSH workspace-write / Low 完整性可运行)
- cargo test + cargo build --release - cargo test + cargo build --release
@@ -72,7 +72,9 @@ $hash = (Get-FileHash $built -Algorithm SHA256).Hash
Step 'check removed update strings' Step 'check removed update strings'
$bytes = [IO.File]::ReadAllBytes($built) $bytes = [IO.File]::ReadAllBytes($built)
$ascii = [Text.Encoding]::ASCII.GetString($bytes) $ascii = [Text.Encoding]::ASCII.GetString($bytes)
$forbidden = @('api.github', 'btn-update', 'Check for Updates', 'powershell', 'update-check-result', 'open-url') # Guard the removed updater. 'powershell' used to be on this list, but that word
# can legitimately show up in user-facing text; the updater itself needs these.
$forbidden = @('api.github', 'btn-update', 'Check for Updates', 'Start-Process', 'Invoke-WebRequest', 'update-check-result', 'open-url')
$bad = @() $bad = @()
foreach ($needle in $forbidden) { foreach ($needle in $forbidden) {
$count = ([regex]::Matches($ascii, [regex]::Escape($needle), 'IgnoreCase')).Count $count = ([regex]::Matches($ascii, [regex]::Escape($needle), 'IgnoreCase')).Count

View File

@@ -33,6 +33,9 @@ struct SelfWriteRecord {
content: String, content: String,
} }
/// Menu-only events exist for macOS; on Windows the page drives the same
/// actions over IPC, so those variants are never constructed here.
#[cfg_attr(not(target_os = "macos"), allow(dead_code))]
#[derive(Debug)] #[derive(Debug)]
enum UserEvent { enum UserEvent {
NewFile(WindowId), NewFile(WindowId),
@@ -322,7 +325,7 @@ fn confirm_discard_unsaved(error: &str) -> bool {
rfd::MessageDialog::new() rfd::MessageDialog::new()
.set_level(rfd::MessageLevel::Warning) .set_level(rfd::MessageLevel::Warning)
.set_title("Could Not Save") .set_title("Could Not Save")
.set_description(&format!( .set_description(format!(
"{error}\n\nThe file may be read-only or locked by another program. Close the window and discard the unsaved changes?" "{error}\n\nThe file may be read-only or locked by another program. Close the window and discard the unsaved changes?"
)) ))
.set_buttons(rfd::MessageButtons::OkCancel) .set_buttons(rfd::MessageButtons::OkCancel)
@@ -610,17 +613,20 @@ fn image_mime_type(path: &Path) -> Option<&'static str> {
fn base64_encode(bytes: &[u8]) -> String { fn base64_encode(bytes: &[u8]) -> String {
const TABLE: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; const TABLE: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
let mut out = String::with_capacity(((bytes.len() + 2) / 3) * 4); let mut out = String::with_capacity(bytes.len().div_ceil(3) * 4);
let mut chunks = bytes.chunks_exact(3); let mut index = 0;
for chunk in &mut chunks { while index + 3 <= bytes.len() {
let n = ((chunk[0] as u32) << 16) | ((chunk[1] as u32) << 8) | chunk[2] as u32; let n = ((bytes[index] as u32) << 16)
| ((bytes[index + 1] as u32) << 8)
| bytes[index + 2] as u32;
out.push(TABLE[((n >> 18) & 0x3f) as usize] as char); out.push(TABLE[((n >> 18) & 0x3f) as usize] as char);
out.push(TABLE[((n >> 12) & 0x3f) as usize] as char); out.push(TABLE[((n >> 12) & 0x3f) as usize] as char);
out.push(TABLE[((n >> 6) & 0x3f) as usize] as char); out.push(TABLE[((n >> 6) & 0x3f) as usize] as char);
out.push(TABLE[(n & 0x3f) as usize] as char); out.push(TABLE[(n & 0x3f) as usize] as char);
index += 3;
} }
match chunks.remainder() { match &bytes[index..] {
[a] => { [a] => {
let n = (*a as u32) << 16; let n = (*a as u32) << 16;
out.push(TABLE[((n >> 18) & 0x3f) as usize] as char); out.push(TABLE[((n >> 18) & 0x3f) as usize] as char);
@@ -2797,6 +2803,15 @@ mod tests {
assert!(html.contains("<hr />")); assert!(html.contains("<hr />"));
} }
#[test]
fn base64_matches_known_vectors() {
assert_eq!(base64_encode(b""), "");
assert_eq!(base64_encode(b"f"), "Zg==");
assert_eq!(base64_encode(b"fo"), "Zm8=");
assert_eq!(base64_encode(b"foo"), "Zm9v");
assert_eq!(base64_encode(b"foobar"), "Zm9vYmFy");
}
#[test] #[test]
fn only_markdown_extensions_are_supported_documents() { fn only_markdown_extensions_are_supported_documents() {
for name in ["note.md", "note.MARKDOWN", "note.mdown", "note.mkd"] { for name in ["note.md", "note.MARKDOWN", "note.mdown", "note.mkd"] {
@@ -4334,6 +4349,7 @@ fn build_startup_page(strings: &Strings) -> String {
) )
} }
#[allow(clippy::too_many_arguments)] // shared window plumbing; split with the webview module
fn build_webview( fn build_webview(
window: &Window, window: &Window,
web_context: &mut WebContext, web_context: &mut WebContext,
@@ -4365,10 +4381,8 @@ fn build_webview(
false, false,
)); ));
false false
} else if url.starts_with("file:") {
false
} else { } else {
true !url.starts_with("file:")
} }
}) })
.with_ipc_handler(move |msg| { .with_ipc_handler(move |msg| {
@@ -5509,9 +5523,11 @@ fn main() {
window_id, window_id,
.. ..
} => { } => {
if app.windows.contains_key(&window_id) { // The window may already be gone (closed by CloseRequested).
close_window(&mut app, window_id, control_flow); if !app.windows.contains_key(&window_id) {
return;
} }
close_window(&mut app, window_id, control_flow);
} }
_ => {} _ => {}
} }