diff --git a/Sources/SWONMacros/Decoding.swift b/Sources/SWONMacros/Decoding.swift index dd54bf4..fcff0f2 100644 --- a/Sources/SWONMacros/Decoding.swift +++ b/Sources/SWONMacros/Decoding.swift @@ -164,14 +164,15 @@ struct SWONDecodeMacro: MemberMacro { """) } } + let accessLevel = declaration.modifiers.accessLevel return [ DeclSyntax(stringLiteral: """ - init(fromSWON root: swon_t) throws { + \(accessLevel) init(fromSWON root: swon_t) throws { \(assignments.joined(separator: "\n")) } """), DeclSyntax(stringLiteral: """ - init(fromJSON json: String) throws { + \(accessLevel) init(fromJSON json: String) throws { var root = swon_t() defer { swon_free(&root) } guard swon_parse(&root, json) == SWONResultValid else { diff --git a/Sources/SWONMacros/Encoding.swift b/Sources/SWONMacros/Encoding.swift index 22c1ef9..093f95c 100644 --- a/Sources/SWONMacros/Encoding.swift +++ b/Sources/SWONMacros/Encoding.swift @@ -207,9 +207,10 @@ struct SWONEncodeMacro: MemberMacro { // message: SWONMessage(message: "Assignments: \(assignments)") // ) // ) + let accessLevel = declaration.modifiers.accessLevel return [ DeclSyntax(stringLiteral: """ - func toSWON() throws -> swon_t { + \(accessLevel) func toSWON() throws -> swon_t { var root = swon_t() guard swon_create_object(&root) else { throw SWONError.invalid("Unable to create root") @@ -223,7 +224,7 @@ struct SWONEncodeMacro: MemberMacro { } """), DeclSyntax(stringLiteral: """ - func toJSON() throws -> String { + \(accessLevel) func toJSON() throws -> String { var item = try toSWON() defer { swon_free(&item) } guard let cjson = swon_encode(item) else { diff --git a/Sources/SWONMacros/Helpers.swift b/Sources/SWONMacros/Helpers.swift index 5f5f3f4..0e7bec3 100644 --- a/Sources/SWONMacros/Helpers.swift +++ b/Sources/SWONMacros/Helpers.swift @@ -79,6 +79,26 @@ extension EnumDeclSyntax { } } +extension DeclModifierListSyntax { + var accessLevel: String { + for modifier in self { + switch modifier.name.tokenKind { + case .keyword(.public): + return "public" + case .keyword(.internal): + return "internal" + case .keyword(.fileprivate): + return "fileprivate" + case .keyword(.private): + return "private" + default: + continue + } + } + return "" + } +} + extension PatternBindingSyntax { var isComputed: Bool { accessorBlock != nil