(config: string)
| 51 | } |
| 52 | |
| 53 | export function parseColorSchemeConfig(config: string): { result: ParsedColorSchemeConfig; error: string | null } { |
| 54 | // Let's first get all "possible" sections of the text. |
| 55 | // We're adding `\n` so the sections "first" word is the |
| 56 | // name of the color scheme. We could remove this and |
| 57 | // skip this in the process of parsing, but because |
| 58 | // the first entry will not have this first '\n' it will |
| 59 | // be more complicated to otherwise just add this '\n' here. |
| 60 | const sections = config.split(`${SEPERATOR }\n\n`); |
| 61 | |
| 62 | const definedColorSchemeNames: Set<string> = new Set(); |
| 63 | let lastDefinedColorSchemeName: string | undefined = ''; |
| 64 | |
| 65 | const definedColorSchemes: ParsedColorSchemeConfig = { |
| 66 | light: {}, |
| 67 | dark: {}, |
| 68 | }; |
| 69 | |
| 70 | // Define the interrupt and error variables. |
| 71 | // Interrupt is to indicate that the parsing should stop. |
| 72 | // But because we cannot break out of a forEach loop, |
| 73 | // we need to use an interrupt variable. |
| 74 | // The error is to indicate that there was an error. |
| 75 | // And also the reason why the parsing failed. |
| 76 | // It will be the first error that is found. |
| 77 | let interrupt = false; |
| 78 | let error: string | null = null; |
| 79 | |
| 80 | const throwError = (message: string) => { |
| 81 | if (!interrupt) { |
| 82 | interrupt = true; |
| 83 | error = message; |
| 84 | } |
| 85 | }; |
| 86 | |
| 87 | // Now we will iterate troughout each section. |
| 88 | // We will always assume bad-faith and make sure to have |
| 89 | // guards in place. As this could also be bad code. |
| 90 | // We shouldn't rely on that the input is correct. |
| 91 | sections.forEach((section) => { |
| 92 | // Check if the interrupt variable is set. |
| 93 | // If it is, we should stop parsing. |
| 94 | if (interrupt) { |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | // First we split the section into lines. |
| 99 | const lines = section.split('\n'); |
| 100 | |
| 101 | // We have to make sure that the first line is a valid color scheme name. |
| 102 | // We will also make sure that the name is not already defined. |
| 103 | const name = lines[0]; |
| 104 | if (!name) { |
| 105 | throwError('No color scheme name was found.'); |
| 106 | return; |
| 107 | } |
| 108 | if (definedColorSchemeNames.has(name)) { |
| 109 | throwError(`The color scheme name "${name}" is already defined.`); |
| 110 | return; |
no test coverage detected