From d2a27c8b6a5c5235b4432283f56da8146ac0c2f3 Mon Sep 17 00:00:00 2001 From: axoineo Date: Sun, 15 Nov 2026 12:18:09 +0530 Subject: [PATCH] Add phone profile and state files (P1-04) Add the profile module to verone-core. A Profile (phone.toml) holds what the user chose: id, name, engine, Android version, creation time, display, graphics mode and adb port. A State (state.toml) holds what Verone observed, currently the graphics mode of the last successful boot. Both are read and written as TOML. Values are validated, unknown settings are rejected, and a file whose schema number is newer than this build understands is refused with an upgrade hint before the rest of it is interpreted. Files are written through a temporary file so a crash cannot leave a partial one. Phone ids can now be read from and written to files. Co-Authored-By: Claude Fable 5.2 --- CHANGELOG.md | 2 + Cargo.lock | 78 ++++ crates/verone-core/Cargo.toml | 2 + crates/verone-core/src/lib.rs | 1 + crates/verone-core/src/profile.rs | 701 ++++++++++++++++++++++++++++++ crates/verone-core/src/storage.rs | 19 +- 6 files changed, 802 insertions(+), 1 deletion(-) create mode 100644 crates/verone-core/src/profile.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 291390b..3ab9fa2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,3 +20,5 @@ All notable changes to Verone are recorded here. The format follows - Storage layout: the data home (`VERONE_HOME`, then `XDG_DATA_HOME/verone`, then `~/.local/share/verone`), one private folder per phone, and phone id rules. - Per-phone lock so two commands cannot work on the same phone at once. +- Phone profile (`phone.toml`) and state (`state.toml`) files: reading, writing and + validation, with files from a newer Verone refused and an upgrade hint given. diff --git a/Cargo.lock b/Cargo.lock index 9d74a58..c3d94dc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -157,6 +157,12 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6184e33543162437515c2e2b48714794e37845ec9851711914eec9d308f6ebe8" +[[package]] +name = "equivalent" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00d174d5400e5e8fd687ad1049e2f578285fa914201b1af7e8b112a4546bd826" + [[package]] name = "errno" version = "0.3.15" @@ -193,12 +199,28 @@ dependencies = [ "r-efi", ] +[[package]] +name = "hashbrown" +version = "0.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a" + [[package]] name = "heck" version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" +[[package]] +name = "indexmap" +version = "2.14.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc4e190f5d26ca7051642629da2c52fc03bde85a03197c99408dcd291734c855" +dependencies = [ + "equivalent", + "hashbrown", +] + [[package]] name = "is_terminal_polyfill" version = "1.70.2" @@ -395,6 +417,15 @@ dependencies = [ "zmij", ] +[[package]] +name = "serde_spanned" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c187e0a369471c550af028cba56d4f4fa2a489ad28151ae95a4d340a2a8b782" +dependencies = [ + "serde_core", +] + [[package]] name = "strsim" version = "0.11.1" @@ -451,6 +482,45 @@ dependencies = [ "syn", ] +[[package]] +name = "toml" +version = "1.2.1+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d5755c51863cdac711f1956baf8eb9588e060755f7d3c4c979e70ea1e1f32cd7" +dependencies = [ + "indexmap", + "serde_core", + "serde_spanned", + "toml_datetime", + "toml_parser", + "toml_writer", + "winnow", +] + +[[package]] +name = "toml_datetime" +version = "1.1.3+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "173f25c883cd2176a2d8212155638c0a60d484a9d84546d270866f40f56b6e49" +dependencies = [ + "serde_core", +] + +[[package]] +name = "toml_parser" +version = "1.1.5+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baa693a8032d7e1cada7d0041e96126df243179ff061456783ac7f12bda4744c" +dependencies = [ + "winnow", +] + +[[package]] +name = "toml_writer" +version = "1.2.0+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "009dbb7700f11c55ef83aebbe4c0e9cf1f1ac0844bd8d6f00dc38f302026e13a" + [[package]] name = "unicode-ident" version = "1.0.27" @@ -480,8 +550,10 @@ dependencies = [ name = "verone-core" version = "0.1.0" dependencies = [ + "serde", "tempfile", "thiserror", + "toml", ] [[package]] @@ -515,6 +587,12 @@ dependencies = [ "windows-link", ] +[[package]] +name = "winnow" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c3b93199df0ab874cdf0c6a94f0a0dc3423fe41f1cafb62b8bd7b1deb277aef" + [[package]] name = "zmij" version = "1.0.23" diff --git a/crates/verone-core/Cargo.toml b/crates/verone-core/Cargo.toml index fac1d00..355e3ef 100644 --- a/crates/verone-core/Cargo.toml +++ b/crates/verone-core/Cargo.toml @@ -11,7 +11,9 @@ repository.workspace = true workspace = true [dependencies] +serde = { version = "1.0.229", features = ["derive"] } thiserror = "2.0.21" +toml = "1.2.1" [dev-dependencies] tempfile = "3.27.0" diff --git a/crates/verone-core/src/lib.rs b/crates/verone-core/src/lib.rs index be77a3c..f2b9a21 100644 --- a/crates/verone-core/src/lib.rs +++ b/crates/verone-core/src/lib.rs @@ -6,6 +6,7 @@ pub mod error; pub mod process; +pub mod profile; pub mod storage; pub use error::{Error, ErrorKind, Result}; diff --git a/crates/verone-core/src/profile.rs b/crates/verone-core/src/profile.rs new file mode 100644 index 0000000..037ceb3 --- /dev/null +++ b/crates/verone-core/src/profile.rs @@ -0,0 +1,701 @@ +//! The two files that describe a phone. +//! +//! `phone.toml` (the [`Profile`]) holds what the user chose and is only changed at +//! the user's request. `state.toml` (the [`State`]) holds what Verone observed and +//! can be deleted without losing anything the user decided. + +use std::fs; +use std::path::Path; +use std::time::{SystemTime, UNIX_EPOCH}; + +use serde::{Deserialize, Serialize}; + +use crate::error::{Error, Result}; +use crate::storage::{PhoneDir, PhoneId}; + +/// The file format version this build reads and writes. +pub const SCHEMA: u32 = 1; + +/// Android version used when the user does not choose one. +pub const DEFAULT_ANDROID: &str = "14"; + +/// Longest allowed display name, in characters. +pub const MAX_NAME_LENGTH: usize = 64; + +/// Allowed range for each side of the screen, in pixels. +pub const SCREEN_SIDE_RANGE: std::ops::RangeInclusive = 240..=4096; + +/// Allowed range for the screen density, in dots per inch. +pub const DPI_RANGE: std::ops::RangeInclusive = 72..=640; + +/// Lowest port a user may publish adb on. Lower ports need administrator rights. +pub const MIN_LOCAL_PORT: u16 = 1024; + +/// What the user chose for a phone. Stored as `phone.toml`. +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct Profile { + /// File format version. + pub schema: u32, + /// The phone's id; always equal to its folder name. + pub id: PhoneId, + /// The name shown to the user. + pub name: String, + /// Which engine runs the phone. + pub engine: EngineKind, + /// Android version, for example `"14"`. + pub android: String, + /// When the phone was created, in seconds since 1970. + pub created_unix: u64, + /// Screen size and density. + #[serde(default)] + pub display: Display, + /// How the screen is drawn. + #[serde(default)] + pub graphics: Graphics, + /// Debug connection settings. + #[serde(default)] + pub adb: Adb, +} + +/// The engines a phone can run on. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "lowercase")] +pub enum EngineKind { + /// Android in a container that shares the host kernel. + Container, +} + +/// Screen size and density. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct Display { + /// Width in pixels. + pub width: u32, + /// Height in pixels. + pub height: u32, + /// Density in dots per inch. + pub dpi: u32, +} + +impl Default for Display { + fn default() -> Self { + Self { + width: 720, + height: 1280, + dpi: 320, + } + } +} + +/// How the screen is drawn. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct Graphics { + /// The mode the user asked for. + pub mode: GraphicsMode, +} + +/// The graphics mode the user asked for. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)] +#[serde(rename_all = "lowercase")] +pub enum GraphicsMode { + /// Use the GPU if a usable one exists, otherwise software. + #[default] + Auto, + /// Always draw in software. + Software, + /// Always use the GPU; fail if none is usable. + Gpu, +} + +/// Debug connection settings. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct Adb { + /// Port on `127.0.0.1` where adb is published. `0` means not published. + pub local_port: u16, +} + +impl Profile { + /// A profile with default settings, created now. The display name is the id + /// until the user gives one. + pub fn new(id: PhoneId) -> Self { + Self { + schema: SCHEMA, + name: id.to_string(), + id, + engine: EngineKind::Container, + android: DEFAULT_ANDROID.to_string(), + created_unix: now_unix(), + display: Display::default(), + graphics: Graphics::default(), + adb: Adb::default(), + } + } + + /// Checks every value. Problems are usage errors: the user asked for something + /// that is not allowed. + pub fn validate(&self) -> Result<()> { + if self.schema != SCHEMA { + return Err(schema_error("phone profile", self.schema)); + } + + let name_length = self.name.chars().count(); + if self.name.trim().is_empty() { + return Err(invalid("the name is empty", "Give the phone a name.")); + } + if name_length > MAX_NAME_LENGTH { + return Err(invalid( + format!("the name is longer than {MAX_NAME_LENGTH} characters"), + "Use a shorter name.", + )); + } + if self.name.chars().any(char::is_control) { + return Err(invalid( + "the name contains a control character such as a line break", + "Use plain text for the name.", + )); + } + + let android_ok = !self.android.is_empty() + && self.android.chars().all(|c| c.is_ascii_digit() || c == '.') + && self.android.starts_with(|c: char| c.is_ascii_digit()); + if !android_ok { + return Err(invalid( + format!("'{}' is not an Android version", self.android), + "Use a version number such as 14.", + )); + } + + for (side, value) in [ + ("width", self.display.width), + ("height", self.display.height), + ] { + if !SCREEN_SIDE_RANGE.contains(&value) { + return Err(invalid( + format!("the screen {side} is {value}"), + format!( + "Use a {side} from {} to {} pixels.", + SCREEN_SIDE_RANGE.start(), + SCREEN_SIDE_RANGE.end() + ), + )); + } + } + if !DPI_RANGE.contains(&self.display.dpi) { + return Err(invalid( + format!("the screen density is {} dpi", self.display.dpi), + format!( + "Use a density from {} to {} dpi.", + DPI_RANGE.start(), + DPI_RANGE.end() + ), + )); + } + + let port = self.adb.local_port; + if port != 0 && port < MIN_LOCAL_PORT { + return Err(invalid( + format!("the adb port is {port}"), + format!("Use 0 for no port, or a port from {MIN_LOCAL_PORT} to 65535."), + )); + } + Ok(()) + } + + /// Reads a profile from TOML text and checks it. + pub fn from_toml(text: &str) -> Result { + check_schema(text, "phone profile")?; + let profile: Self = toml::from_str(text) + .map_err(|error| Error::failed(format!("the phone profile is not valid: {error}")))?; + profile.validate()?; + Ok(profile) + } + + /// Writes the profile as TOML text. + pub fn to_toml(&self) -> Result { + toml::to_string(self) + .map_err(|error| Error::failed(format!("could not write the phone profile: {error}"))) + } + + /// Loads a phone's `phone.toml`. The id inside must match the folder. + pub fn load(phone: &PhoneDir) -> Result { + let path = phone.profile_path(); + let text = read(&path)?; + let profile = Self::from_toml(&text).map_err(|error| in_file(error, &path))?; + if &profile.id != phone.id() { + return Err(Error::failed(format!( + "{} says its id is '{}', but it is in the folder of '{}'", + path.display(), + profile.id, + phone.id() + )) + .with_hint("Set id in the file to match the folder name.")); + } + Ok(profile) + } + + /// Checks the profile and saves it as the phone's `phone.toml`. + pub fn save(&self, phone: &PhoneDir) -> Result<()> { + self.validate()?; + if &self.id != phone.id() { + return Err(Error::failed(format!( + "cannot save the profile of '{}' into the folder of '{}'", + self.id, + phone.id() + ))); + } + write_atomically(&phone.profile_path(), &self.to_toml()?) + } +} + +/// What Verone observed about a phone. Stored as `state.toml`. +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct State { + /// File format version. + pub schema: u32, + /// The last successful boot, if there has been one. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub last_boot: Option, +} + +/// Facts about the last successful boot. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct LastBoot { + /// The graphics mode the phone actually booted in. + pub graphics: GraphicsUsed, + /// True if GPU mode was tried first and the phone had to fall back. + pub fell_back_to_software: bool, +} + +/// The graphics mode a phone actually ran in. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "lowercase")] +pub enum GraphicsUsed { + /// Drawn in software. + Software, + /// Drawn by the GPU. + Gpu, +} + +impl Default for State { + fn default() -> Self { + Self { + schema: SCHEMA, + last_boot: None, + } + } +} + +impl State { + /// Reads state from TOML text. + pub fn from_toml(text: &str) -> Result { + check_schema(text, "phone state")?; + toml::from_str(text) + .map_err(|error| Error::failed(format!("the phone state is not valid: {error}"))) + } + + /// Writes the state as TOML text. + pub fn to_toml(&self) -> Result { + toml::to_string(self) + .map_err(|error| Error::failed(format!("could not write the phone state: {error}"))) + } + + /// Loads a phone's `state.toml`. A phone that has never been started has no + /// such file, which gives the default state. + pub fn load(phone: &PhoneDir) -> Result { + let path = phone.state_path(); + if !path.exists() { + return Ok(Self::default()); + } + let text = read(&path)?; + Self::from_toml(&text).map_err(|error| { + in_file(error, &path) + .with_hint("This file only records observations. It is safe to delete it.") + }) + } + + /// Saves the state as the phone's `state.toml`. + pub fn save(&self, phone: &PhoneDir) -> Result<()> { + write_atomically(&phone.state_path(), &self.to_toml()?) + } +} + +/// Reads only the `schema` value and rejects files this build cannot understand, +/// before the rest of the file is interpreted. +fn check_schema(text: &str, what: &str) -> Result<()> { + let table: toml::Table = toml::from_str(text) + .map_err(|error| Error::failed(format!("the {what} is not valid TOML: {error}")))?; + match table.get("schema").and_then(toml::Value::as_integer) { + Some(schema) if schema == i64::from(SCHEMA) => Ok(()), + Some(schema) => Err(schema_error(what, u32::try_from(schema).unwrap_or(0))), + None => Err(Error::failed(format!("the {what} has no schema number")) + .with_hint(format!("Add the line: schema = {SCHEMA}"))), + } +} + +fn schema_error(what: &str, found: u32) -> Error { + if found > SCHEMA { + Error::failed(format!( + "the {what} uses file format {found}, but this version of Verone only understands format {SCHEMA}" + )) + .with_hint("It was written by a newer Verone. Upgrade Verone to use this phone.") + } else { + Error::failed(format!( + "the {what} uses file format {found}, which is not supported (expected {SCHEMA})" + )) + .with_hint(format!( + "Set schema = {SCHEMA} if the file was edited by hand." + )) + } +} + +fn invalid(problem: impl Into, hint: impl Into) -> Error { + Error::usage(format!( + "the phone settings are not valid: {}", + problem.into() + )) + .with_hint(hint) +} + +/// Adds the file path to an error found while reading a file, keeping its hint. +fn in_file(error: Error, path: &Path) -> Error { + let located = Error::failed(format!("{}: {}", path.display(), error.message())); + match error.hint() { + Some(hint) => located.with_hint(hint), + None => located, + } +} + +fn read(path: &Path) -> Result { + fs::read_to_string(path) + .map_err(|error| Error::failed(format!("could not read {}: {error}", path.display()))) +} + +/// Writes a file so that a crash leaves either the old content or the new, never +/// a partial file: the text goes to a temporary file that then replaces the target. +fn write_atomically(path: &Path, text: &str) -> Result<()> { + let failed = |error: std::io::Error| { + Error::failed(format!("could not write {}: {error}", path.display())) + }; + let mut temporary = path.as_os_str().to_os_string(); + temporary.push(".tmp"); + let temporary = Path::new(&temporary); + fs::write(temporary, text).map_err(failed)?; + fs::rename(temporary, path).map_err(failed) +} + +fn now_unix() -> u64 { + SystemTime::now() + .duration_since(UNIX_EPOCH) + .map(|elapsed| elapsed.as_secs()) + .unwrap_or(0) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::error::ErrorKind; + use crate::storage::Storage; + + const EXAMPLE: &str = r#" +schema = 1 +id = "work" +name = "Work phone" +engine = "container" +android = "14" +created_unix = 1794700000 + +[display] +width = 720 +height = 1280 +dpi = 320 + +[graphics] +mode = "auto" + +[adb] +local_port = 0 +"#; + + fn id(text: &str) -> PhoneId { + PhoneId::new(text).unwrap() + } + + fn profile() -> Profile { + Profile::new(id("work")) + } + + fn phone_dir(temp: &tempfile::TempDir, name: &str) -> PhoneDir { + Storage::new(temp.path()).create_phone(&id(name)).unwrap() + } + + // ---- defaults and round trips ---- + + #[test] + fn a_new_profile_has_the_documented_defaults() { + let profile = profile(); + assert_eq!(profile.schema, 1); + assert_eq!(profile.name, "work"); + assert_eq!(profile.engine, EngineKind::Container); + assert_eq!(profile.android, "14"); + assert_eq!( + profile.display, + Display { + width: 720, + height: 1280, + dpi: 320 + } + ); + assert_eq!(profile.graphics.mode, GraphicsMode::Auto); + assert_eq!(profile.adb.local_port, 0); + assert!(profile.created_unix > 1_700_000_000); + profile.validate().unwrap(); + } + + #[test] + fn the_documented_example_is_read_correctly() { + let profile = Profile::from_toml(EXAMPLE).unwrap(); + assert_eq!(profile.id, id("work")); + assert_eq!(profile.name, "Work phone"); + assert_eq!(profile.created_unix, 1_794_700_000); + assert_eq!(profile.graphics.mode, GraphicsMode::Auto); + } + + #[test] + fn a_profile_survives_writing_and_reading() { + let mut original = profile(); + original.name = "Tëst phone ✓".to_string(); + original.display = Display { + width: 1080, + height: 2400, + dpi: 420, + }; + original.graphics.mode = GraphicsMode::Software; + original.adb.local_port = 5599; + + let text = original.to_toml().unwrap(); + assert_eq!(Profile::from_toml(&text).unwrap(), original); + } + + #[test] + fn sections_left_out_of_the_file_take_their_defaults() { + let text = "schema = 1\nid = \"a\"\nname = \"A\"\nengine = \"container\"\nandroid = \"14\"\ncreated_unix = 1\n"; + let profile = Profile::from_toml(text).unwrap(); + assert_eq!(profile.display, Display::default()); + assert_eq!(profile.graphics, Graphics::default()); + assert_eq!(profile.adb, Adb::default()); + } + + #[test] + fn a_profile_is_saved_to_and_loaded_from_its_folder() { + let temp = tempfile::tempdir().unwrap(); + let phone = phone_dir(&temp, "work"); + let original = profile(); + original.save(&phone).unwrap(); + + assert!(phone.profile_path().is_file()); + assert!(!phone.path().join("phone.toml.tmp").exists()); + assert_eq!(Profile::load(&phone).unwrap(), original); + } + + // ---- validation ---- + + fn assert_invalid(change: impl FnOnce(&mut Profile), expected: &str) { + let mut profile = profile(); + change(&mut profile); + let error = profile.validate().unwrap_err(); + assert_eq!(error.kind(), ErrorKind::Usage, "{error}"); + assert!(error.message().contains(expected), "{error}"); + assert!(error.hint().is_some()); + } + + #[test] + fn each_invalid_value_is_rejected() { + assert_invalid(|p| p.name = String::new(), "name is empty"); + assert_invalid(|p| p.name = " ".to_string(), "name is empty"); + assert_invalid(|p| p.name = "x".repeat(65), "longer than 64"); + assert_invalid(|p| p.name = "two\nlines".to_string(), "control character"); + assert_invalid(|p| p.android = String::new(), "not an Android version"); + assert_invalid( + |p| p.android = "latest".to_string(), + "not an Android version", + ); + assert_invalid(|p| p.android = ".14".to_string(), "not an Android version"); + assert_invalid(|p| p.display.width = 239, "width is 239"); + assert_invalid(|p| p.display.width = 4097, "width is 4097"); + assert_invalid(|p| p.display.height = 0, "height is 0"); + assert_invalid(|p| p.display.dpi = 71, "density is 71"); + assert_invalid(|p| p.display.dpi = 641, "density is 641"); + assert_invalid(|p| p.adb.local_port = 80, "adb port is 80"); + assert_invalid(|p| p.adb.local_port = 1023, "adb port is 1023"); + } + + #[test] + fn values_at_the_edges_of_the_ranges_are_accepted() { + let mut profile = profile(); + profile.name = "x".repeat(64); + profile.display = Display { + width: 240, + height: 4096, + dpi: 72, + }; + profile.adb.local_port = 1024; + profile.android = "8.1".to_string(); + profile.validate().unwrap(); + + profile.display.dpi = 640; + profile.adb.local_port = 65535; + profile.validate().unwrap(); + } + + #[test] + fn invalid_values_in_a_file_are_rejected_on_reading() { + let text = EXAMPLE.replace("dpi = 320", "dpi = 9000"); + let error = Profile::from_toml(&text).unwrap_err(); + assert!(error.message().contains("density is 9000"), "{error}"); + } + + #[test] + fn an_invalid_id_in_a_file_is_rejected() { + let text = EXAMPLE.replace("id = \"work\"", "id = \"Not Valid\""); + let error = Profile::from_toml(&text).unwrap_err(); + assert!(error.message().contains("not a valid phone id"), "{error}"); + } + + #[test] + fn unknown_settings_and_values_are_rejected() { + let extra = EXAMPLE.replace("android = \"14\"", "android = \"14\"\ncolour = \"red\""); + let error = Profile::from_toml(&extra).unwrap_err(); + assert!(error.message().contains("colour"), "{error}"); + + let mode = EXAMPLE.replace("mode = \"auto\"", "mode = \"turbo\""); + let error = Profile::from_toml(&mode).unwrap_err(); + assert!(error.message().contains("turbo"), "{error}"); + + let engine = EXAMPLE.replace("engine = \"container\"", "engine = \"magic\""); + assert!(Profile::from_toml(&engine).is_err()); + } + + #[test] + fn text_that_is_not_toml_is_rejected() { + let error = Profile::from_toml("this is not toml").unwrap_err(); + assert!(error.message().contains("not valid TOML"), "{error}"); + } + + // ---- schema ---- + + #[test] + fn a_file_from_a_newer_verone_is_refused_with_an_upgrade_hint() { + let text = EXAMPLE.replace("schema = 1", "schema = 2"); + let error = Profile::from_toml(&text).unwrap_err(); + assert_eq!(error.kind(), ErrorKind::Failed); + assert!(error.message().contains("file format 2"), "{error}"); + assert!(error.hint().unwrap().contains("Upgrade Verone")); + } + + #[test] + fn a_newer_file_is_refused_even_if_its_other_settings_are_unknown() { + let text = "schema = 7\nsomething_new = true\n"; + let error = Profile::from_toml(text).unwrap_err(); + assert!(error.message().contains("file format 7"), "{error}"); + } + + #[test] + fn schema_zero_and_a_missing_schema_are_refused() { + let zero = EXAMPLE.replace("schema = 1", "schema = 0"); + let error = Profile::from_toml(&zero).unwrap_err(); + assert!(error.message().contains("not supported"), "{error}"); + + let missing = EXAMPLE.replace("schema = 1\n", ""); + let error = Profile::from_toml(&missing).unwrap_err(); + assert!(error.message().contains("no schema number"), "{error}"); + } + + #[test] + fn a_profile_with_the_wrong_schema_cannot_be_saved() { + let temp = tempfile::tempdir().unwrap(); + let phone = phone_dir(&temp, "work"); + let mut profile = profile(); + profile.schema = 2; + assert!(profile.save(&phone).is_err()); + assert!(!phone.profile_path().exists()); + } + + // ---- files ---- + + #[test] + fn loading_names_the_file_in_its_error() { + let temp = tempfile::tempdir().unwrap(); + let phone = phone_dir(&temp, "work"); + fs::write( + phone.profile_path(), + EXAMPLE.replace("schema = 1", "schema = 2"), + ) + .unwrap(); + let error = Profile::load(&phone).unwrap_err(); + assert!(error.message().contains("phone.toml"), "{error}"); + assert!(error.hint().unwrap().contains("Upgrade Verone")); + } + + #[test] + fn loading_a_missing_profile_fails() { + let temp = tempfile::tempdir().unwrap(); + let phone = phone_dir(&temp, "work"); + let error = Profile::load(&phone).unwrap_err(); + assert!(error.message().contains("could not read"), "{error}"); + } + + #[test] + fn a_profile_in_the_wrong_folder_is_refused() { + let temp = tempfile::tempdir().unwrap(); + let phone = phone_dir(&temp, "other"); + fs::write(phone.profile_path(), EXAMPLE).unwrap(); + let error = Profile::load(&phone).unwrap_err(); + assert!(error.message().contains("says its id is 'work'"), "{error}"); + + assert!(profile().save(&phone).is_err()); + } + + // ---- state ---- + + #[test] + fn a_phone_that_never_started_has_the_default_state() { + let temp = tempfile::tempdir().unwrap(); + let phone = phone_dir(&temp, "work"); + assert_eq!(State::load(&phone).unwrap(), State::default()); + assert_eq!(State::default().to_toml().unwrap().trim(), "schema = 1"); + } + + #[test] + fn state_survives_saving_and_loading() { + let temp = tempfile::tempdir().unwrap(); + let phone = phone_dir(&temp, "work"); + let state = State { + schema: SCHEMA, + last_boot: Some(LastBoot { + graphics: GraphicsUsed::Software, + fell_back_to_software: true, + }), + }; + state.save(&phone).unwrap(); + assert_eq!(State::load(&phone).unwrap(), state); + } + + #[test] + fn a_newer_or_broken_state_file_is_refused_and_called_safe_to_delete() { + let temp = tempfile::tempdir().unwrap(); + let phone = phone_dir(&temp, "work"); + + fs::write(phone.state_path(), "schema = 3\n").unwrap(); + let error = State::load(&phone).unwrap_err(); + assert!(error.message().contains("file format 3"), "{error}"); + assert!(error.hint().unwrap().contains("safe to delete")); + + fs::write(phone.state_path(), "schema = 1\nsurprise = 1\n").unwrap(); + assert!(State::load(&phone).is_err()); + } +} diff --git a/crates/verone-core/src/storage.rs b/crates/verone-core/src/storage.rs index 11ce43b..ad4f225 100644 --- a/crates/verone-core/src/storage.rs +++ b/crates/verone-core/src/storage.rs @@ -42,7 +42,10 @@ const DATA_DIR: &str = "data"; /// /// Lowercase letters, digits and hyphens; 1 to 31 characters; starts with a letter /// or digit. -#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[derive( + Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize, serde::Deserialize, +)] +#[serde(try_from = "String", into = "String")] pub struct PhoneId(String); impl PhoneId { @@ -86,6 +89,20 @@ impl fmt::Display for PhoneId { } } +impl TryFrom for PhoneId { + type Error = Error; + + fn try_from(id: String) -> Result { + Self::new(&id) + } +} + +impl From for String { + fn from(id: PhoneId) -> Self { + id.0 + } +} + impl FromStr for PhoneId { type Err = Error;