<commit_before>export const HELLO_COMMAND = "HELLO";
export const INIT_COMMAND = "INIT";
export const ERROR_COMMAND = "ERROR";
export const DEBUG_COMMAND = "DEBUG";
export const SUPPORTED_COMMAND = "SUPPORTED";
export const END_SUPPORTED_COMMAND = "ENDSUPPORTED";
export const IDENTITY_COMMAND = "IDENTITY";
export const PANEL_COMMAND = "PANEL";
export const PANEL_SET_COMMAND = "PANEL-SET";
export const PANEL_PULSE_COMMAND = "PANEL-PULSE";
export const DISK_COMMAND = "DISK";
export const DISK_RESET_COMMAND = "DISK-RESET";
export const DISK_STATE_COMMAND = "DISK-STATE";

const DISK_ARG_POSITION = "POS";
const DISK_ARG_DIRECTION = "DIR";
const DISK_ARG_USER = "USER";

export class SerialProtocolCommandParser {
  /**
   * Parse a command string and return its command name and data
   * @param {String} commandString - The serial command to parse
   * @returns {Object} - Two keys: 1. name - the name of the command, 2. data - other data as provided by that command's specific parser
   */
  static parse(commandString) {
    let commandName, commandArgs;
    [commandName, ...commandArgs] = commandString.trim().split(/\s+/);
    commandName = commandName.toUpperCase();

    const parserFunctions = {
      [HELLO_COMMAND]: SerialProtocolCommandParser.parseHelloArguments,
      [INIT_COMMAND]: SerialProtocolCommandParser.parseInitArguments,
      [ERROR_COMMAND]: SerialProtocolCommandParser.parseErrorArguments,
      [DEBUG_COMMAND]: SerialProtocolCommandParser.parseDebugArguments,
      [SUPPORTED_COMMAND]: SerialProtocolCommandParser.parseSupportedArguments,
      [END_SUPPORTED_COMMAND]: SerialProtocolCommandParser.parseEndSupportedArguments,
      [IDENTITY_COMMAND]: SerialProtocolCommandParser.parseIdentityArguments,
      [PANEL_COMMAND]: SerialProtocolCommandParser.parsePanelArguments,
      [PANEL_SET_COMMAND]: SerialProtocolCommandParser.parsePanelSetArguments,
      [PANEL_PULSE_COMMAND]: SerialProtocolCommandParser.parsePanelPulseArguments,
      [DISK_COMMAND]: SerialProtocolCommandParser.parseDiskArguments,
      [DISK_RESET_COMMAND]: SerialProtocolCommandParser.parseDiskResetArguments,
      [DISK_STATE_COMMAND]: SerialProtocolCommandParser.parseDiskStateArguments
    };

    const parserFunction = parserFunctions[commandName];
    if (!parserFunction) {
      throw new Error(`Unrecognized command name '${commandName}'`);
    }

    return {
      name: commandName,
      data: parserFunction(commandArgs)
    };
  }

  static parseHelloArguments(args) {
    return {supportedGames: args};
  }

  static parseInitArguments() {
    return {};
  }

  static parseErrorArguments(args) {
    return {message: args.join(" ") || ""};
  }

  static parseDebugArguments(args) {
    return {message: args.join(" ") || ""};
  }

  static parseSupportedArguments() {
    return {};
  }

  static parseEndSupportedArguments() {
    return {};
  }

  static parseIdentityArguments(args) {
    return {identity: args[0]};
  }

  static parsePanelArguments(args) {
    return {stripId: args[0], panelId: args[1], pressed: args[2]};
  }

  static parsePanelSetArguments(args) {
    const [stripId, panelId, intensity, color, easing, duration] = args;
    return {stripId, panelId, intensity, color, easing, duration};
  }

  static parsePanelPulseArguments(args) {
    const [stripId, panelId, intensity, color, easing, duration] = args;
    return {stripId, panelId, intensity, color, easing, duration};
  }

  static parseDiskArguments(args) {<commit_msg>bugfix: PANEL-SET and PANEL-PULSE from the Arduino are only sent during initialization and pass a regexp string as parameter<commit_after>export const HELLO_COMMAND = "HELLO";
export const INIT_COMMAND = "INIT";
export const ERROR_COMMAND = "ERROR";
export const DEBUG_COMMAND = "DEBUG";
export const SUPPORTED_COMMAND = "SUPPORTED";
export const END_SUPPORTED_COMMAND = "ENDSUPPORTED";
export const IDENTITY_COMMAND = "IDENTITY";
export const PANEL_COMMAND = "PANEL";
export const PANEL_SET_COMMAND = "PANEL-SET";
export const PANEL_PULSE_COMMAND = "PANEL-PULSE";
export const DISK_COMMAND = "DISK";
export const DISK_RESET_COMMAND = "DISK-RESET";
export const DISK_STATE_COMMAND = "DISK-STATE";

const DISK_ARG_POSITION = "POS";
const DISK_ARG_DIRECTION = "DIR";
const DISK_ARG_USER = "USER";

export class SerialProtocolCommandParser {
  /**
   * Parse a command string and return its command name and data
   * @param {String} commandString - The serial command to parse
   * @returns {Object} - Two keys: 1. name - the name of the command, 2. data - other data as provided by that command's specific parser
   */
  static parse(commandString) {
    let commandName, commandArgs;
    [commandName, ...commandArgs] = commandString.trim().split(/\s+/);
    commandName = commandName.toUpperCase();

    const parserFunctions = {
      [HELLO_COMMAND]: SerialProtocolCommandParser.parseHelloArguments,
      [INIT_COMMAND]: SerialProtocolCommandParser.parseInitArguments,
      [ERROR_COMMAND]: SerialProtocolCommandParser.parseErrorArguments,
      [DEBUG_COMMAND]: SerialProtocolCommandParser.parseDebugArguments,
      [SUPPORTED_COMMAND]: SerialProtocolCommandParser.parseSupportedArguments,
      [END_SUPPORTED_COMMAND]: SerialProtocolCommandParser.parseEndSupportedArguments,
      [IDENTITY_COMMAND]: SerialProtocolCommandParser.parseIdentityArguments,
      [PANEL_COMMAND]: SerialProtocolCommandParser.parsePanelArguments,
      [PANEL_SET_COMMAND]: SerialProtocolCommandParser.parsePanelSetArguments,
      [PANEL_PULSE_COMMAND]: SerialProtocolCommandParser.parsePanelPulseArguments,
      [DISK_COMMAND]: SerialProtocolCommandParser.parseDiskArguments,
      [DISK_RESET_COMMAND]: SerialProtocolCommandParser.parseDiskResetArguments,
      [DISK_STATE_COMMAND]: SerialProtocolCommandParser.parseDiskStateArguments
    };

    const parserFunction = parserFunctions[commandName];
    if (!parserFunction) {
      throw new Error(`Unrecognized command name '${commandName}'`);
    }

    return {
      name: commandName,
      data: parserFunction(commandArgs)
    };
  }

  static parseHelloArguments(args) {
    return {supportedGames: args};
  }

  static parseInitArguments() {
    return {};
  }

  static parseErrorArguments(args) {
    return {message: args.join(" ") || ""};
  }

  static parseDebugArguments(args) {
    return {message: args.join(" ") || ""};
  }

  static parseSupportedArguments() {
    return {};
  }

  static parseEndSupportedArguments() {
    return {};
  }

  static parseIdentityArguments(args) {
    return {identity: args[0]};
  }

  static parsePanelArguments(args) {
    return {stripId: args[0], panelId: args[1], pressed: args[2]};
  }

  // Only sent during initialization (args is regexp)
  static parsePanelSetArguments(args) {
    return args.join(' ');
  }

  // Only sent during initialization (args is regexp)
  static parsePanelPulseArguments(args) {
    return args.join(' ');
  }

  static parseDiskArguments(args) {
{'fraction_non_alphanumeric': 0.1003877638948729, 'fraction_numerical': 0.0017233950883239983, 'mean_word_length': 4.760132340777502, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4311534', 'n_tokens_mistral': 2006, 'n_tokens_neox': 2014, 'n_words': 472}