example.rs

  1use std::{
  2    borrow::Cow,
  3    env,
  4    fmt::{self, Display},
  5    fs,
  6    io::Write,
  7    mem,
  8    path::{Path, PathBuf},
  9};
 10
 11use anyhow::{Context as _, Result};
 12use clap::ValueEnum;
 13use gpui::http_client::Url;
 14use pulldown_cmark::CowStr;
 15use serde::{Deserialize, Serialize};
 16
 17const CURSOR_POSITION_HEADING: &str = "Cursor Position";
 18const EDIT_HISTORY_HEADING: &str = "Edit History";
 19const EXPECTED_PATCH_HEADING: &str = "Expected Patch";
 20const EXPECTED_EXCERPTS_HEADING: &str = "Expected Excerpts";
 21const REPOSITORY_URL_FIELD: &str = "repository_url";
 22const REVISION_FIELD: &str = "revision";
 23
 24#[derive(Debug)]
 25pub struct NamedExample {
 26    pub name: String,
 27    pub example: Example,
 28}
 29
 30#[derive(Debug, Serialize, Deserialize)]
 31pub struct Example {
 32    pub repository_url: String,
 33    pub revision: String,
 34    pub cursor_path: PathBuf,
 35    pub cursor_position: String,
 36    pub edit_history: Vec<String>,
 37    pub expected_patch: String,
 38    pub expected_excerpts: Vec<ExpectedExcerpt>,
 39}
 40
 41#[derive(Debug, Serialize, Deserialize)]
 42pub struct ExpectedExcerpt {
 43    path: PathBuf,
 44    text: String,
 45}
 46
 47#[derive(ValueEnum, Debug, Clone)]
 48pub enum ExampleFormat {
 49    Json,
 50    Toml,
 51    Md,
 52}
 53
 54impl NamedExample {
 55    pub fn load(path: impl AsRef<Path>) -> Result<Self> {
 56        let path = path.as_ref();
 57        let content = std::fs::read_to_string(path)?;
 58        let ext = path.extension();
 59
 60        match ext.and_then(|s| s.to_str()) {
 61            Some("json") => Ok(Self {
 62                name: path.file_name().unwrap_or_default().display().to_string(),
 63                example: serde_json::from_str(&content)?,
 64            }),
 65            Some("toml") => Ok(Self {
 66                name: path.file_name().unwrap_or_default().display().to_string(),
 67                example: toml::from_str(&content)?,
 68            }),
 69            Some("md") => Self::parse_md(&content),
 70            Some(_) => {
 71                anyhow::bail!("Unrecognized example extension: {}", ext.unwrap().display());
 72            }
 73            None => {
 74                anyhow::bail!(
 75                    "Failed to determine example type since the file does not have an extension."
 76                );
 77            }
 78        }
 79    }
 80
 81    pub fn parse_md(input: &str) -> Result<Self> {
 82        use pulldown_cmark::{CodeBlockKind, Event, HeadingLevel, Parser, Tag, TagEnd};
 83
 84        let parser = Parser::new(input);
 85
 86        let mut named = NamedExample {
 87            name: String::new(),
 88            example: Example {
 89                repository_url: String::new(),
 90                revision: String::new(),
 91                cursor_path: PathBuf::new(),
 92                cursor_position: String::new(),
 93                edit_history: Vec::new(),
 94                expected_patch: String::new(),
 95                expected_excerpts: Vec::new(),
 96            },
 97        };
 98
 99        let mut text = String::new();
100        let mut current_section = String::new();
101        let mut block_info: CowStr = "".into();
102
103        for event in parser {
104            match event {
105                Event::Text(line) => {
106                    text.push_str(&line);
107
108                    if !named.name.is_empty()
109                        && current_section.is_empty()
110                        // in h1 section
111                        && let Some((field, value)) = line.split_once('=')
112                    {
113                        match field.trim() {
114                            REPOSITORY_URL_FIELD => {
115                                named.example.repository_url = value.trim().to_string();
116                            }
117                            REVISION_FIELD => {
118                                named.example.revision = value.trim().to_string();
119                            }
120                            _ => {
121                                eprintln!("Warning: Unrecognized field `{field}`");
122                            }
123                        }
124                    }
125                }
126                Event::End(TagEnd::Heading(HeadingLevel::H1)) => {
127                    if !named.name.is_empty() {
128                        anyhow::bail!(
129                            "Found multiple H1 headings. There should only be one with the name of the example."
130                        );
131                    }
132                    named.name = mem::take(&mut text);
133                }
134                Event::End(TagEnd::Heading(HeadingLevel::H2)) => {
135                    current_section = mem::take(&mut text);
136                }
137                Event::End(TagEnd::Heading(level)) => {
138                    anyhow::bail!("Unexpected heading level: {level}");
139                }
140                Event::Start(Tag::CodeBlock(kind)) => {
141                    match kind {
142                        CodeBlockKind::Fenced(info) => {
143                            block_info = info;
144                        }
145                        CodeBlockKind::Indented => {
146                            anyhow::bail!("Unexpected indented codeblock");
147                        }
148                    };
149                }
150                Event::Start(_) => {
151                    text.clear();
152                    block_info = "".into();
153                }
154                Event::End(TagEnd::CodeBlock) => {
155                    if current_section.eq_ignore_ascii_case(EDIT_HISTORY_HEADING) {
156                        named.example.edit_history.push(mem::take(&mut text));
157                    } else if current_section.eq_ignore_ascii_case(CURSOR_POSITION_HEADING) {
158                        let path = PathBuf::from(block_info.trim());
159                        named.example.cursor_path = path;
160                        named.example.cursor_position = mem::take(&mut text);
161                    } else if current_section.eq_ignore_ascii_case(EXPECTED_PATCH_HEADING) {
162                        named.example.expected_patch = mem::take(&mut text);
163                    } else if current_section.eq_ignore_ascii_case(EXPECTED_EXCERPTS_HEADING) {
164                        let path = PathBuf::from(block_info.trim());
165                        named.example.expected_excerpts.push(ExpectedExcerpt {
166                            path,
167                            text: mem::take(&mut text),
168                        });
169                    } else {
170                        eprintln!("Warning: Unrecognized section `{current_section:?}`")
171                    }
172                }
173                _ => {}
174            }
175        }
176
177        if named.example.cursor_path.as_path() == Path::new("")
178            || named.example.cursor_position.is_empty()
179        {
180            anyhow::bail!("Missing cursor position codeblock");
181        }
182
183        Ok(named)
184    }
185
186    pub fn write(&self, format: ExampleFormat, mut out: impl Write) -> Result<()> {
187        match format {
188            ExampleFormat::Json => Ok(serde_json::to_writer(out, &self.example)?),
189            ExampleFormat::Toml => {
190                Ok(out.write_all(toml::to_string_pretty(&self.example)?.as_bytes())?)
191            }
192            ExampleFormat::Md => Ok(write!(out, "{}", self)?),
193        }
194    }
195
196    #[allow(unused)]
197    pub async fn setup_worktree(&self) -> Result<PathBuf> {
198        let worktrees_dir = env::current_dir()?.join("target").join("zeta-worktrees");
199        let repos_dir = env::current_dir()?.join("target").join("zeta-repos");
200        fs::create_dir_all(&repos_dir)?;
201        fs::create_dir_all(&worktrees_dir)?;
202
203        let (repo_owner, repo_name) = self.repo_name()?;
204
205        let repo_dir = repos_dir.join(repo_owner.as_ref()).join(repo_name.as_ref());
206        if !repo_dir.is_dir() {
207            fs::create_dir_all(&repo_dir)?;
208            run_git(&repo_dir, &["init"]).await?;
209            run_git(
210                &repo_dir,
211                &["remote", "add", "origin", &self.example.repository_url],
212            )
213            .await?;
214        }
215
216        run_git(
217            &repo_dir,
218            &["fetch", "--depth", "1", "origin", &self.example.revision],
219        )
220        .await?;
221
222        let worktree_path = worktrees_dir.join(&self.name);
223
224        if worktree_path.is_dir() {
225            run_git(&worktree_path, &["clean", "--force", "-d"]).await?;
226            run_git(&worktree_path, &["reset", "--hard", "HEAD"]).await?;
227            run_git(&worktree_path, &["checkout", &self.example.revision]).await?;
228        } else {
229            let worktree_path_string = worktree_path.to_string_lossy();
230            run_git(
231                &repo_dir,
232                &[
233                    "worktree",
234                    "add",
235                    "-f",
236                    &worktree_path_string,
237                    &self.example.revision,
238                ],
239            )
240            .await?;
241        }
242
243        Ok(worktree_path)
244    }
245
246    #[allow(unused)]
247    fn repo_name(&self) -> Result<(Cow<'_, str>, Cow<'_, str>)> {
248        // git@github.com:owner/repo.git
249        if self.example.repository_url.contains('@') {
250            let (owner, repo) = self
251                .example
252                .repository_url
253                .split_once(':')
254                .context("expected : in git url")?
255                .1
256                .split_once('/')
257                .context("expected / in git url")?;
258            Ok((
259                Cow::Borrowed(owner),
260                Cow::Borrowed(repo.trim_end_matches(".git")),
261            ))
262        // http://github.com/owner/repo.git
263        } else {
264            let url = Url::parse(&self.example.repository_url)?;
265            let mut segments = url.path_segments().context("empty http url")?;
266            let owner = segments
267                .next()
268                .context("expected owner path segment")?
269                .to_string();
270            let repo = segments
271                .next()
272                .context("expected repo path segment")?
273                .trim_end_matches(".git")
274                .to_string();
275            assert!(segments.next().is_none());
276
277            Ok((owner.into(), repo.into()))
278        }
279    }
280}
281
282async fn run_git(repo_path: &Path, args: &[&str]) -> Result<String> {
283    let output = smol::process::Command::new("git")
284        .current_dir(repo_path)
285        .args(args)
286        .output()
287        .await?;
288
289    anyhow::ensure!(
290        output.status.success(),
291        "`git {}` within `{}` failed with status: {}\nstderr:\n{}\nstdout:\n{}",
292        args.join(" "),
293        repo_path.display(),
294        output.status,
295        String::from_utf8_lossy(&output.stderr),
296        String::from_utf8_lossy(&output.stdout),
297    );
298    Ok(String::from_utf8(output.stdout)?.trim().to_string())
299}
300
301impl Display for NamedExample {
302    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
303        write!(f, "# {}\n\n", self.name)?;
304        write!(
305            f,
306            "{REPOSITORY_URL_FIELD} = {}\n",
307            self.example.repository_url
308        )?;
309        write!(f, "{REVISION_FIELD} = {}\n\n", self.example.revision)?;
310
311        write!(
312            f,
313            "## {CURSOR_POSITION_HEADING}\n\n`````{}\n{}`````\n",
314            self.example.cursor_path.display(),
315            self.example.cursor_position
316        )?;
317        write!(f, "## {EDIT_HISTORY_HEADING}\n\n")?;
318
319        if !self.example.edit_history.is_empty() {
320            write!(f, "`````diff\n")?;
321            for item in &self.example.edit_history {
322                write!(f, "{item}")?;
323            }
324            write!(f, "`````\n")?;
325        }
326
327        if !self.example.expected_patch.is_empty() {
328            write!(
329                f,
330                "\n## {EXPECTED_PATCH_HEADING}\n\n`````diff\n{}`````\n",
331                self.example.expected_patch
332            )?;
333        }
334
335        if !self.example.expected_excerpts.is_empty() {
336            write!(f, "\n## {EXPECTED_EXCERPTS_HEADING}\n\n")?;
337
338            for excerpt in &self.example.expected_excerpts {
339                write!(
340                    f,
341                    "`````{}{}\n{}`````\n\n",
342                    excerpt
343                        .path
344                        .extension()
345                        .map(|ext| format!("{} ", ext.to_string_lossy()))
346                        .unwrap_or_default(),
347                    excerpt.path.display(),
348                    excerpt.text
349                )?;
350            }
351        }
352
353        Ok(())
354    }
355}