<commit_before>use std::mem;
use std::collections::HashMap;

use edit_types::{BufferEvent, EventDomain};

/// A container that manages and holds all recordings for the current editing session
pub(crate) struct Recorder {
    active_recording: Option<String>,
    recordings: HashMap<String, Recording>,
}

impl Recorder {
    pub(crate) fn new() -> Recorder {
        Recorder {
            active_recording: None,
            recordings: HashMap::new(),
        }
    }

    pub(crate) fn is_recording(&self) -> bool {
        self.active_recording.is_some()
    }

    /// Starts or stops the specified recording.
    ///
    ///
    /// There are three outcome behaviors:
    /// - If the current recording name is specified, the active recording is saved
    /// - If no recording name is specified, the currently active recording is saved
    /// - If a recording name other than the active recording is specified,
    /// the current recording will be thrown out and will be switched to the new name
    ///
    /// In addition to the above:
    /// - If the recording was saved, there is no active recording
    /// - If the recording was switched, there will be a new active recording
    pub(crate) fn toggle_recording(&mut self, recording_name: Option<String>) {
        let is_recording = self.is_recording();
        let last_recording = self.active_recording.take();

        match (is_recording, &last_recording, &recording_name) {
            (true, Some(last_recording), None) => self.filter_recording(last_recording),
            (true, Some(last_recording), Some(recording_name)) => {
                if last_recording != recording_name {
                    self.clear(last_recording);
                } else {
                    self.filter_recording(last_recording);
                    return;
                }
            }
            _ => {}
        }

        mem::replace(&mut self.active_recording, recording_name);
    }

    /// Saves an event into the currently active recording.
    /// If no recording is active, the event passed in is ignored.
    pub(crate) fn record(&mut self, cmd: EventDomain) {
        if !self.is_recording() {
            warn!("Recorder not active-- ignoring event {:?}", cmd);
            return;
        }<commit_msg>Add copyright notice<commit_after>// Copyright 2018 The xi-editor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::mem;
use std::collections::HashMap;

use edit_types::{BufferEvent, EventDomain};

/// A container that manages and holds all recordings for the current editing session
pub(crate) struct Recorder {
    active_recording: Option<String>,
    recordings: HashMap<String, Recording>,
}

impl Recorder {
    pub(crate) fn new() -> Recorder {
        Recorder {
            active_recording: None,
            recordings: HashMap::new(),
        }
    }

    pub(crate) fn is_recording(&self) -> bool {
        self.active_recording.is_some()
    }

    /// Starts or stops the specified recording.
    ///
    ///
    /// There are three outcome behaviors:
    /// - If the current recording name is specified, the active recording is saved
    /// - If no recording name is specified, the currently active recording is saved
    /// - If a recording name other than the active recording is specified,
    /// the current recording will be thrown out and will be switched to the new name
    ///
    /// In addition to the above:
    /// - If the recording was saved, there is no active recording
    /// - If the recording was switched, there will be a new active recording
    pub(crate) fn toggle_recording(&mut self, recording_name: Option<String>) {
        let is_recording = self.is_recording();
        let last_recording = self.active_recording.take();

        match (is_recording, &last_recording, &recording_name) {
            (true, Some(last_recording), None) => self.filter_recording(last_recording),
            (true, Some(last_recording), Some(recording_name)) => {
                if last_recording != recording_name {
                    self.clear(last_recording);
                } else {
                    self.filter_recording(last_recording);
                    return;
                }
            },
            _ => {}
        }

        mem::replace(&mut self.active_recording, recording_name);
    }

    /// Saves an event into the currently active recording.
    /// If no recording is active, the event passed in is ignored.
    pub(crate) fn record(&mut self, cmd: EventDomain) {
        if !self.is_recording() {
            warn!("Recorder not active-- ignoring event {:?}", cmd);
            return;
        }
{'fraction_non_alphanumeric': 0.10003869969040248, 'fraction_numerical': 0.0015479876160990713, 'mean_word_length': 2.3391472868217056, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, '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': '6248782', 'n_tokens_mistral': 1365, 'n_tokens_neox': 1346, 'n_words': 484}