Skip to content

Commit

Permalink
chore: improve initialisation flow
Browse files Browse the repository at this point in the history
  • Loading branch information
a-kenji committed Sep 3, 2024
1 parent 7ec2993 commit 2e0f075
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 44 deletions.
15 changes: 15 additions & 0 deletions src/bin/flake-edit/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::path::PathBuf;
use crate::cli::CliArgs;
use crate::error::FeError;
use crate::root::Root;
use rnix::tokenizer::Tokenizer;
use ropey::Rope;

#[derive(Debug, Default)]
Expand All @@ -31,6 +32,20 @@ impl FlakeEdit {
pub fn root(&self) -> &FlakeBuf {
&self.root
}

pub fn text(&self) -> String {
self.root().text().to_string()
}

pub fn create_editor(&self) -> Result<crate::edit::FlakeEdit, FeError> {
let text = self.root().text().to_string();
let (_node, errors) = rnix::parser::parse(Tokenizer::new(&text));
if !errors.is_empty() {
tracing::error!("There are errors in the root document.");
}
Ok(crate::edit::FlakeEdit::from_text(&text)?)

}
}

#[derive(Debug, Default)]
Expand Down
18 changes: 5 additions & 13 deletions src/bin/flake-edit/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use flake_edit::lock::FlakeLock;
use flake_edit::update::Updater;
use nix_uri::urls::UrlWrapper;
use nix_uri::{FlakeRef, NixUriResult};
use rnix::tokenizer::Tokenizer;

mod app;
mod cache;
Expand All @@ -32,14 +31,7 @@ fn main() -> eyre::Result<()> {
tracing::debug!("Cli args: {args:?}");

let app = FlakeEdit::init(&args)?;

let (_node, errors) = rnix::parser::parse(Tokenizer::new(&app.root().text().to_string()));
if !errors.is_empty() {
tracing::error!("There are errors in the root document.");
}

let text = app.root().text().to_string();
let mut editor = edit::FlakeEdit::from(&text)?;
let mut editor = app.create_editor()?;
let mut change = Change::None;

match args.subcommand() {
Expand Down Expand Up @@ -136,7 +128,7 @@ fn main() -> eyre::Result<()> {
}

if args.diff() {
let old = text.clone();
let old = app.text();
let new = resulting_change;
let diff = Diff::new(&old, &new);
diff.compare();
Expand Down Expand Up @@ -230,11 +222,11 @@ fn main() -> eyre::Result<()> {
}
buf.push_str(input.id());
}
let mut updater = Updater::new(app.root().text().clone(), inputs.clone());
let mut updater = Updater::new(app.text().into(), inputs.clone());
updater.update_all_inputs_to_latest_semver(id.clone(), *init);
let change = updater.get_changes();
if args.diff() {
let old = text.clone();
let old = app.text();
let new = change;
let diff = Diff::new(&old, &new);
diff.compare();
Expand Down Expand Up @@ -272,7 +264,7 @@ fn main() -> eyre::Result<()> {
let change = updater.get_changes();

if args.diff() {
let old = text.clone();
let old = app.text();
let new = change;
let diff = Diff::new(&old, &new);
diff.compare();
Expand Down
2 changes: 1 addition & 1 deletion src/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl FlakeEdit {
Self { changes, walker }
}

pub fn from(stream: &str) -> Result<Self, FlakeEditError> {
pub fn from_text(stream: &str) -> Result<Self, FlakeEditError> {
let walker = Walker::new(stream);
Ok(Self::new(Vec::new(), walker))
}
Expand Down
60 changes: 30 additions & 30 deletions tests/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn root_load() {
#[test]
fn root_edit_list() {
let (flake, _lock) = load_fixtures("root");
let mut flake_edit = FlakeEdit::from(&flake).unwrap();
let mut flake_edit = FlakeEdit::from_text(&flake).unwrap();
let info = Info::new("".into(), vec![]);
insta::with_settings!({sort_maps => true, info => &info}, {
insta::assert_yaml_snapshot!(flake_edit.list());
Expand All @@ -40,7 +40,7 @@ fn root_edit_list() {
#[test]
fn root_add_toplevel_id_uri() {
let (flake, _lock) = load_fixtures("root");
let mut flake_edit = FlakeEdit::from(&flake).unwrap();
let mut flake_edit = FlakeEdit::from_text(&flake).unwrap();
let change = Change::Add {
id: Some("vmsh".to_owned()),
uri: Some("github:mic92/vmsh".to_owned()),
Expand All @@ -54,7 +54,7 @@ fn root_add_toplevel_id_uri() {
#[test]
fn root_add_toplevel_id_uri_no_flake() {
let (flake, _lock) = load_fixtures("root");
let mut flake_edit = FlakeEdit::from(&flake).unwrap();
let mut flake_edit = FlakeEdit::from_text(&flake).unwrap();
let change = Change::Add {
id: Some("not_a_flake".to_owned()),
uri: Some("github:a-kenji/not_a_flake".to_owned()),
Expand Down Expand Up @@ -107,7 +107,7 @@ fn root_remove_toplevel_input_single_nested() {
#[test]
fn root_alt_list() {
let (flake, _lock) = load_fixtures("root_alt");
let mut flake_edit = FlakeEdit::from(&flake).unwrap();
let mut flake_edit = FlakeEdit::from_text(&flake).unwrap();
let info = Info::new("".into(), vec![]);
insta::with_settings!({sort_maps => true, info => &info}, {
insta::assert_yaml_snapshot!(flake_edit.list());
Expand Down Expand Up @@ -173,7 +173,7 @@ fn root_alt_remove_toplevel_input_multiple() {
#[test]
fn root_toplevel_nesting_list() {
let (flake, _lock) = load_fixtures("toplevel_nesting");
let mut flake_edit = FlakeEdit::from(&flake).unwrap();
let mut flake_edit = FlakeEdit::from_text(&flake).unwrap();
let info = Info::new("".into(), vec![]);
insta::with_settings!({sort_maps => true, info => &info}, {
insta::assert_yaml_snapshot!(flake_edit.list());
Expand Down Expand Up @@ -226,7 +226,7 @@ fn root_toplevel_nesting_list() {
#[test]
fn completely_flat_toplevel_list() {
let (flake, _lock) = load_fixtures("completely_flat_toplevel");
let mut flake_edit = FlakeEdit::from(&flake).unwrap();
let mut flake_edit = FlakeEdit::from_text(&flake).unwrap();
let info = Info::new("".into(), vec![]);
insta::with_settings!({sort_maps => true, info => &info}, {
insta::assert_yaml_snapshot!(flake_edit.list());
Expand All @@ -236,7 +236,7 @@ fn completely_flat_toplevel_list() {
#[test]
fn completely_flat_toplevel_alt_list() {
let (flake, _lock) = load_fixtures("completely_flat_toplevel_alt");
let mut flake_edit = FlakeEdit::from(&flake).unwrap();
let mut flake_edit = FlakeEdit::from_text(&flake).unwrap();
let info = Info::new("".into(), vec![]);
insta::with_settings!({sort_maps => true, info => &info}, {
insta::assert_yaml_snapshot!(flake_edit.list());
Expand All @@ -245,7 +245,7 @@ fn completely_flat_toplevel_alt_list() {
#[test]
fn completely_flat_toplevel_add_id_uri() {
let (flake, _lock) = load_fixtures("completely_flat_toplevel");
let mut flake_edit = FlakeEdit::from(&flake).unwrap();
let mut flake_edit = FlakeEdit::from_text(&flake).unwrap();
let change = Change::Add {
id: Some("vmsh".to_owned()),
uri: Some("mic92/vmsh".to_owned()),
Expand All @@ -259,7 +259,7 @@ fn completely_flat_toplevel_add_id_uri() {
#[test]
fn completely_flat_toplevel_add_id_uri_no_flake() {
let (flake, _lock) = load_fixtures("completely_flat_toplevel");
let mut flake_edit = FlakeEdit::from(&flake).unwrap();
let mut flake_edit = FlakeEdit::from_text(&flake).unwrap();
let change = Change::Add {
id: Some("not_a_flake".to_owned()),
uri: Some("github:a-kenji/not_a_flake".to_owned()),
Expand All @@ -273,7 +273,7 @@ fn completely_flat_toplevel_add_id_uri_no_flake() {
#[test]
fn completely_flat_toplevel_rm_toplevel() {
let (flake, _lock) = load_fixtures("completely_flat_toplevel");
let mut flake_edit = FlakeEdit::from(&flake).unwrap();
let mut flake_edit = FlakeEdit::from_text(&flake).unwrap();
let change = Change::Remove {
id: "nixpkgs".to_owned().into(),
};
Expand All @@ -285,7 +285,7 @@ fn completely_flat_toplevel_rm_toplevel() {
#[test]
fn completely_flat_toplevel_rm_toplevel_multiple() {
let (flake, _lock) = load_fixtures("completely_flat_toplevel");
let mut flake_edit = FlakeEdit::from(&flake).unwrap();
let mut flake_edit = FlakeEdit::from_text(&flake).unwrap();
let change = Change::Remove {
id: "crane".to_owned().into(),
};
Expand All @@ -297,7 +297,7 @@ fn completely_flat_toplevel_rm_toplevel_multiple() {
#[test]
fn completely_flat_toplevel_rm_follows_single() {
let (flake, _lock) = load_fixtures("completely_flat_toplevel");
let mut flake_edit = FlakeEdit::from(&flake).unwrap();
let mut flake_edit = FlakeEdit::from_text(&flake).unwrap();
let change = Change::Remove {
id: "crane.rust-overlay".to_owned().into(),
};
Expand All @@ -309,7 +309,7 @@ fn completely_flat_toplevel_rm_follows_single() {
#[test]
fn completely_flat_toplevel_no_flake_rm_single_no_flake() {
let (flake, _lock) = load_fixtures("completely_flat_toplevel_not_a_flake");
let mut flake_edit = FlakeEdit::from(&flake).unwrap();
let mut flake_edit = FlakeEdit::from_text(&flake).unwrap();
let change = Change::Remove {
id: "not-a-flake".to_owned().into(),
};
Expand All @@ -322,7 +322,7 @@ fn completely_flat_toplevel_no_flake_rm_single_no_flake() {
#[should_panic]
fn completely_flat_toplevel_no_flake_rm_single_no_flake_rm_nonexistent() {
let (flake, _lock) = load_fixtures("completely_flat_toplevel_not_a_flake");
let mut flake_edit = FlakeEdit::from(&flake).unwrap();
let mut flake_edit = FlakeEdit::from_text(&flake).unwrap();
let change = Change::Remove {
id: "not-an-input-at-all".to_owned().into(),
};
Expand All @@ -331,7 +331,7 @@ fn completely_flat_toplevel_no_flake_rm_single_no_flake_rm_nonexistent() {
#[test]
fn completely_flat_toplevel_no_flake_list() {
let (flake, _lock) = load_fixtures("completely_flat_toplevel_not_a_flake");
let mut flake_edit = FlakeEdit::from(&flake).unwrap();
let mut flake_edit = FlakeEdit::from_text(&flake).unwrap();
let change = Change::None;
let info = Info::new("".into(), vec![change.clone()]);
insta::with_settings!({sort_maps => true, info => &info}, {
Expand All @@ -341,7 +341,7 @@ fn completely_flat_toplevel_no_flake_list() {
#[test]
fn completely_flat_toplevel_no_flake_rm_single_no_flake_nested() {
let (flake, _lock) = load_fixtures("completely_flat_toplevel_not_a_flake_nested");
let mut flake_edit = FlakeEdit::from(&flake).unwrap();
let mut flake_edit = FlakeEdit::from_text(&flake).unwrap();
let change = Change::Remove {
id: "not-a-flake".to_owned().into(),
};
Expand All @@ -353,7 +353,7 @@ fn completely_flat_toplevel_no_flake_rm_single_no_flake_nested() {
#[test]
fn completely_flat_toplevel_no_flake_nested_list() {
let (flake, _lock) = load_fixtures("completely_flat_toplevel_not_a_flake_nested");
let mut flake_edit = FlakeEdit::from(&flake).unwrap();
let mut flake_edit = FlakeEdit::from_text(&flake).unwrap();
let change = Change::None;
let info = Info::new("".into(), vec![change.clone()]);
insta::with_settings!({sort_maps => true, info => &info}, {
Expand All @@ -363,7 +363,7 @@ fn completely_flat_toplevel_no_flake_nested_list() {
#[test]
fn one_level_nesting_flat_no_flake_rm_single_no_flake_nested() {
let (flake, _lock) = load_fixtures("one_level_nesting_flat_not_a_flake");
let mut flake_edit = FlakeEdit::from(&flake).unwrap();
let mut flake_edit = FlakeEdit::from_text(&flake).unwrap();
let change = Change::Remove {
id: "not-a-flake".to_owned().into(),
};
Expand All @@ -375,7 +375,7 @@ fn one_level_nesting_flat_no_flake_rm_single_no_flake_nested() {
#[test]
fn one_level_nesting_flat_no_flake_nested_list() {
let (flake, _lock) = load_fixtures("one_level_nesting_flat_not_a_flake");
let mut flake_edit = FlakeEdit::from(&flake).unwrap();
let mut flake_edit = FlakeEdit::from_text(&flake).unwrap();
let change = Change::None;
let info = Info::new("".into(), vec![change.clone()]);
insta::with_settings!({sort_maps => true, info => &info}, {
Expand Down Expand Up @@ -424,7 +424,7 @@ fn one_level_nesting_flat() {
#[test]
fn one_level_nesting_flat_remove_single() {
let (flake, _lock) = load_fixtures("one_level_nesting_flat");
let mut flake_edit = FlakeEdit::from(&flake).unwrap();
let mut flake_edit = FlakeEdit::from_text(&flake).unwrap();
let change = Change::Remove {
id: "nixpkgs".to_owned().into(),
};
Expand All @@ -436,7 +436,7 @@ fn one_level_nesting_flat_remove_single() {
#[test]
fn one_level_nesting_flat_remove_multiple() {
let (flake, _lock) = load_fixtures("one_level_nesting_flat");
let mut flake_edit = FlakeEdit::from(&flake).unwrap();
let mut flake_edit = FlakeEdit::from_text(&flake).unwrap();
let change = Change::Remove {
id: "rust-overlay".to_owned().into(),
};
Expand All @@ -448,7 +448,7 @@ fn one_level_nesting_flat_remove_multiple() {
#[test]
fn one_level_nesting_flat_remove_single_nested() {
let (flake, _lock) = load_fixtures("one_level_nesting_flat");
let mut flake_edit = FlakeEdit::from(&flake).unwrap();
let mut flake_edit = FlakeEdit::from_text(&flake).unwrap();
let change = Change::Remove {
id: "rust-overlay.flake-utils".to_owned().into(),
};
Expand All @@ -461,7 +461,7 @@ fn one_level_nesting_flat_remove_single_nested() {
#[test]
fn flat_nested_flat_remove_single() {
let (flake, _lock) = load_fixtures("flat_nested_flat");
let mut flake_edit = FlakeEdit::from(&flake).unwrap();
let mut flake_edit = FlakeEdit::from_text(&flake).unwrap();
let change = Change::Remove {
id: "nixpkgs".to_owned().into(),
};
Expand All @@ -473,7 +473,7 @@ fn flat_nested_flat_remove_single() {
#[test]
fn flat_nested_flat_remove_multiple() {
let (flake, _lock) = load_fixtures("flat_nested_flat");
let mut flake_edit = FlakeEdit::from(&flake).unwrap();
let mut flake_edit = FlakeEdit::from_text(&flake).unwrap();
let change = Change::Remove {
id: "poetry2nix".to_owned().into(),
};
Expand All @@ -485,7 +485,7 @@ fn flat_nested_flat_remove_multiple() {
#[test]
fn flat_nested_flat_add_single() {
let (flake, _lock) = load_fixtures("flat_nested_flat");
let mut flake_edit = FlakeEdit::from(&flake).unwrap();
let mut flake_edit = FlakeEdit::from_text(&flake).unwrap();
let change = Change::Add {
id: Some("vmsh".to_owned()),
uri: Some("mic92/vmsh".to_owned()),
Expand All @@ -499,7 +499,7 @@ fn flat_nested_flat_add_single() {
#[test]
fn flat_nested_flat_add_single_no_flake() {
let (flake, _lock) = load_fixtures("flat_nested_flat");
let mut flake_edit = FlakeEdit::from(&flake).unwrap();
let mut flake_edit = FlakeEdit::from_text(&flake).unwrap();
let change = Change::Add {
id: Some("not_a_flake".to_owned()),
uri: Some("github:a-kenji/not_a_flake".to_owned()),
Expand All @@ -513,7 +513,7 @@ fn flat_nested_flat_add_single_no_flake() {
#[test]
fn first_nested_node_add_single() {
let (flake, _lock) = load_fixtures("first_nested_node");
let mut flake_edit = FlakeEdit::from(&flake).unwrap();
let mut flake_edit = FlakeEdit::from_text(&flake).unwrap();
let change = Change::Add {
id: Some("vmsh".to_owned()),
uri: Some("mic92/vmsh".to_owned()),
Expand All @@ -530,7 +530,7 @@ fn first_nested_node_add_single() {
#[test]
fn first_nested_node_add_single_no_flake() {
let (flake, _lock) = load_fixtures("first_nested_node");
let mut flake_edit = FlakeEdit::from(&flake).unwrap();
let mut flake_edit = FlakeEdit::from_text(&flake).unwrap();
let change = Change::Add {
id: Some("vmsh".to_owned()),
uri: Some("mic92/vmsh".to_owned()),
Expand All @@ -547,7 +547,7 @@ fn first_nested_node_add_single_no_flake() {
#[test]
fn first_nested_node_remove_single() {
let (flake, _lock) = load_fixtures("first_nested_node");
let mut flake_edit = FlakeEdit::from(&flake).unwrap();
let mut flake_edit = FlakeEdit::from_text(&flake).unwrap();
let change = Change::Remove {
id: "utils".to_string().into(),
};
Expand All @@ -562,7 +562,7 @@ fn first_nested_node_remove_single() {
#[test]
fn first_nested_node_remove_multiple() {
let (flake, _lock) = load_fixtures("first_nested_node");
let mut flake_edit = FlakeEdit::from(&flake).unwrap();
let mut flake_edit = FlakeEdit::from_text(&flake).unwrap();
let change = Change::Remove {
id: "naersk".to_string().into(),
};
Expand Down

0 comments on commit 2e0f075

Please sign in to comment.