summaryrefslogtreecommitdiff
path: root/src/dev/check.rs
blob: 2c48f0e108d1ff52a221971f31903c8eaec6e5e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
use anyhow::{bail, Context, Result};
use std::{
    cmp::Ordering,
    fs::{self, read_dir},
    path::PathBuf,
};

use crate::{
    info_file::{ExerciseInfo, InfoFile},
    CURRENT_FORMAT_VERSION, DEVELOPING_OFFICIAL_RUSTLINGS,
};

fn forbidden_char(input: &str) -> Option<char> {
    input.chars().find(|c| *c != '_' && !c.is_alphanumeric())
}

fn check_info_file_exercises(info_file: &InfoFile) -> Result<hashbrown::HashSet<PathBuf>> {
    let mut names = hashbrown::HashSet::with_capacity(info_file.exercises.len());
    let mut paths = hashbrown::HashSet::with_capacity(info_file.exercises.len());

    for exercise_info in &info_file.exercises {
        if let Some(c) = forbidden_char(&exercise_info.name) {
            bail!(
                "Char `{c}` in the exercise name `{}` is not allowed",
                exercise_info.name,
            );
        }

        if let Some(dir) = &exercise_info.dir {
            if let Some(c) = forbidden_char(dir) {
                bail!("Char `{c}` in the exercise dir `{dir}` is not allowed");
            }
        }

        if !names.insert(exercise_info.name.as_str()) {
            bail!(
                "The exercise name {} is duplicated. Exercise names must all be unique",
                exercise_info.name,
            );
        }

        paths.insert(PathBuf::from(exercise_info.path()));
    }

    Ok(paths)
}

fn check_exercise_dir_files(
    info_file: &InfoFile,
    info_file_paths: hashbrown::HashSet<PathBuf>,
) -> Result<hashbrown::HashSet<String>> {
    let mut names = hashbrown::HashSet::with_capacity(info_file.exercises.len());

    for entry in read_dir("exercises").context("Failed to open the `exercises` directory")? {
        let entry = entry.context("Failed to read the `exercises` directory")?;

        if entry.file_type().unwrap().is_file() {
            let path = entry.path();
            let file_name = path.file_name().unwrap();
            if file_name == "README.md" {
                continue;
            }

            if !info_file_paths.contains(&path) {
                bail!("`{}` is expected to be an exercise file corresponding to some exercise in `info.toml`", path.display());
            }

            let file_name = file_name.to_string_lossy();
            names.insert(file_name[..file_name.len() - 3].to_string());
            continue;
        }

        let dir_path = entry.path();
        for entry in read_dir(&dir_path)
            .with_context(|| format!("Failed to open the directory {}", dir_path.display()))?
        {
            let entry = entry
                .with_context(|| format!("Failed to read the directory {}", dir_path.display()))?;
            let path = entry.path();

            if !entry.file_type().unwrap().is_file() {
                bail!("Found {} but expected only files", path.display());
            }

            let file_name = path.file_name().unwrap();
            if file_name == "README.md" {
                continue;
            }

            if !info_file_paths.contains(&path) {
                bail!("`{}` is expected to be an exercise file corresponding to some exercise in `info.toml`", path.display());
            }

            // The file name must be valid Unicode with the `.rs` extension
            // because it is part of the info file paths.
            let file_name = file_name.to_string_lossy();
            let file_name_without_rs_extension = file_name[..file_name.len() - 3].to_string();
            names.insert(file_name_without_rs_extension);
        }
    }

    Ok(names)
}

fn check_info_file(info_file: &InfoFile) -> Result<()> {
    match info_file.format_version.cmp(&CURRENT_FORMAT_VERSION) {
        Ordering::Less => bail!("`format_version` < {CURRENT_FORMAT_VERSION} (supported version)\nPlease migrate to the latest format version"),
        Ordering::Greater => bail!("`format_version` > {CURRENT_FORMAT_VERSION} (supported version)\nTry updating the Rustlings program"),
        Ordering::Equal => (),
    }

    let info_file_paths = check_info_file_exercises(info_file)?;
    let names_in_exercises_dir = check_exercise_dir_files(info_file, info_file_paths)?;

    // Now, we know that every file has an exercise in `info.toml`.
    // But we need to check that every exercise in `info.toml` has a file.
    if names_in_exercises_dir.len() != info_file.exercises.len() {
        for exercise_info in &info_file.exercises {
            if !names_in_exercises_dir.contains(&exercise_info.name) {
                bail!("The file `{}` is missing", exercise_info.path());
            }
        }
    }

    Ok(())
}

pub fn bins_start_end_ind(cargo_toml: &str) -> Result<(usize, usize)> {
    let start_ind = cargo_toml
        .find("bin = [")
        .context("Failed to find the start of the `bin` list (`bin = [`)")?
        + 7;
    let end_ind = start_ind
        + cargo_toml
            .get(start_ind..)
            .and_then(|slice| slice.as_bytes().iter().position(|c| *c == b']'))
            .context("Failed to find the end of the `bin` list (`]`)")?;

    Ok((start_ind, end_ind))
}

pub fn append_bins(
    buf: &mut Vec<u8>,
    exercise_infos: &[ExerciseInfo],
    exercise_path_prefix: &[u8],
) {
    buf.push(b'\n');
    for exercise_info in exercise_infos {
        buf.extend_from_slice(b"  { name = \"");
        buf.extend_from_slice(exercise_info.name.as_bytes());
        buf.extend_from_slice(b"\", path = \"");
        buf.extend_from_slice(exercise_path_prefix);
        buf.extend_from_slice(b"exercises/");
        if let Some(dir) = &exercise_info.dir {
            buf.extend_from_slice(dir.as_bytes());
            buf.push(b'/');
        }
        buf.extend_from_slice(exercise_info.name.as_bytes());
        buf.extend_from_slice(b".rs\" },\n");
    }
}

fn check_cargo_toml(
    exercise_infos: &[ExerciseInfo],
    current_cargo_toml: &str,
    exercise_path_prefix: &[u8],
) -> Result<()> {
    let (bins_start_ind, bins_end_ind) = bins_start_end_ind(current_cargo_toml)?;

    let old_bins = &current_cargo_toml.as_bytes()[bins_start_ind..bins_end_ind];
    let mut new_bins = Vec::with_capacity(1 << 13);
    append_bins(&mut new_bins, exercise_infos, exercise_path_prefix);

    if old_bins != new_bins {
        bail!("`Cargo.toml` is outdated. Run `rustlings dev update` to update it");
    }

    Ok(())
}

pub fn check() -> Result<()> {
    let info_file = InfoFile::parse()?;
    check_info_file(&info_file)?;

    if DEVELOPING_OFFICIAL_RUSTLINGS {
        check_cargo_toml(
            &info_file.exercises,
            include_str!("../../dev/Cargo.toml"),
            b"../",
        )
        .context("The file `dev/Cargo.toml` is outdated. Please run `cargo run -- dev update` to update it")?;
    } else {
        let current_cargo_toml =
            fs::read_to_string("Cargo.toml").context("Failed to read the file `Cargo.toml`")?;
        check_cargo_toml(&info_file.exercises, &current_cargo_toml, b"").context(
            "The file `Cargo.toml` is outdated. Please run `rustlings dev update` to update it",
        )?;
    }

    println!("\nEverything looks fine!");

    Ok(())
}