Skip to content

Commit

Permalink
fix lint (mlemgroup#1080)
Browse files Browse the repository at this point in the history
  • Loading branch information
EricBAndrews authored Jun 2, 2024
1 parent 01a6ae2 commit bd042ea
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 10 deletions.
3 changes: 2 additions & 1 deletion Mlem/API/APIClient/APIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ extension APIClientError: CustomStringConvertible {
case .invalidSession:
return "Invalid session"
case let .decoding(data, error):
guard let string = String(data: data, encoding: .utf8) else {
let string = String(decoding: data, as: UTF8.self)
guard !string.isEmpty else {
return localizedDescription
}

Expand Down
5 changes: 4 additions & 1 deletion Mlem/Extensions/String/String+WithEscapedCharacters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ extension String {
func withEscapedCharacters() -> String? {
do {
let jsonRepresentation = try JSONEncoder().encode(self)
return String(data: jsonRepresentation, encoding: .utf8)
let ret = String(decoding: jsonRepresentation, as: UTF8.self)
// slightly awkward but preserves contract
if !ret.isEmpty { return ret }
return nil
} catch {
return nil
}
Expand Down
3 changes: 2 additions & 1 deletion Mlem/Logic/InstanceMetadataParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ struct InstanceMetadataParser {
// MARK: - Public Methods

static func parse(from data: Data) throws -> [InstanceMetadata] {
guard let string = String(data: data, encoding: .utf8), !string.isEmpty else {
let string = String(decoding: data, as: UTF8.self)
guard !string.isEmpty else {
throw ParsingError.invalidData
}

Expand Down
4 changes: 2 additions & 2 deletions Mlem/Repositories/PictrsRespository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ class PictrsRespository {
print("Upload failed (2): \(error)")
switch error {
case let APIClientError.decoding(data, _):
let text = String(data: data, encoding: .utf8)
if text?.contains("413 Request Entity Too Large") ?? false {
let text = String(decoding: data, as: UTF8.self)
if text.contains("413 Request Entity Too Large") {
imageModel.state = .failed("Image too large")
} else {
imageModel.state = .failed(text)
Expand Down
3 changes: 2 additions & 1 deletion Mlem/Views/Shared/Instance/InstanceView+Logic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ extension InstanceView {
}
} catch let APIClientError.decoding(data, error) {
withAnimation(.easeOut(duration: 0.2)) {
if let content = String(data: data, encoding: .utf8) {
let content = String(decoding: data, as: UTF8.self)
if !content.isEmpty {
if content.contains("<div class=\"kbin-container\">") {
errorDetails = ErrorDetails(
title: "KBin Instance",
Expand Down
8 changes: 4 additions & 4 deletions MlemTests/Parsers/InstanceMetadataParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import XCTest
final class InstanceMetadataParserTests: XCTestCase {
func testParserHandlesExpectedData() throws {
// construct some test data
let data = """
let data = Data("""
Instance,NU,NC,Fed,Adult,↓V,Users,BI,BB,UT,MO,Version
[Lemmy.world](https://lemmy.world),Yes,Yes,Yes,Yes,Yes,18431,53,3,97%,2,0.18.4
[lemm.ee](https://lemm.ee),Yes,No,Yes,No,Yes,3710,34,0,100%,2,0.1.2
""".data(using: .utf8)!
""".utf8)

// ask the parser to parse it
let metadata = try InstanceMetadataParser.parse(from: data)
Expand Down Expand Up @@ -57,11 +57,11 @@ final class InstanceMetadataParserTests: XCTestCase {

func testParserIsNotReliantOnHeaderFieldOrder() throws {
// construct some test data with some the fields moved around
let data = """
let data = Data("""
Users,NU,NC,Fed,Version,Adult,↓V,BI,BB,UT,MO,Instance
18431,Yes,Yes,Yes,0.18.4,Yes,Yes,53,3,97%,2,[Lemmy.world](https://lemmy.world)
3710,Yes,No,Yes,0.1.2,No,Yes,34,0,100%,2,[lemm.ee](https://lemm.ee)
""".data(using: .utf8)!
""".utf8)

// ask the parser to parse it
let metadata = try InstanceMetadataParser.parse(from: data)
Expand Down

0 comments on commit bd042ea

Please sign in to comment.