// UTF-8 ↔ MoonBit String bridge.
//
// OS clipboard tools deliver UTF-8 byte buffers (Windows itself uses
// UTF-16 internally, but PowerShell's `Get-Clipboard` re-encodes to
// UTF-8 when piped). MoonBit's String is UTF-16, so we translate at the
// subprocess boundary.

///|
/// Decode UTF-8 bytes into a MoonBit String. `@utf8.decode` raises a
/// `@utf8.Malformed` on invalid input; we re-raise it as
/// `ClipboardError::NotUtf8` so the rest of the library deals with a
/// single error type. Only the byte length is propagated — the raw
/// bytes are deliberately dropped so the error can never carry
/// clipboard contents through to a log sink.
fn decode_utf8(bytes : Bytes) -> String raise ClipboardError {
  @utf8.decode(bytes) catch {
    _ => raise NotUtf8(bytes.length())
  }
}

///|
/// Encode a MoonBit String as UTF-8 bytes. MoonBit Strings are always
/// valid UTF-16, so this never raises.
fn encode_utf8(s : String) -> Bytes {
  @utf8.encode(s)
}