Skip to content

Commit 889a2a4

Browse files
committed
implemented deserialisation of IdStrategy from String
* addsuffix=blah , to add suffix * addprefix=blah, to add prefix * updateversion, to add or increment a version suffix (v1,v2,v3,etc) * randomsuffix, to set a random suffix (nanoid) * replace=blah , to just set a static String * replacerandom=blahprefix;blahsuffix , to set a prefix, random component (nanoid) and suffix
1 parent fd8cf6c commit 889a2a4

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

src/store.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1856,11 +1856,17 @@ pub fn generate_id(prefix: &str, suffix: &str) -> String {
18561856

18571857
#[derive(Clone, Debug)]
18581858
pub enum IdStrategy {
1859+
/// The new ID is formed by adding a static suffix to the old ID
18591860
AddSuffix(String),
1861+
/// The new ID is formed by adding random suffix (nanoid) to the old ID
18601862
AddRandomSuffix,
1863+
/// The new ID is formed by adding a static prefix to the old ID
18611864
AddPrefix(String),
1865+
/// The new ID is formed by adding or incrementing a version suffix (v1,v2,v3) to the old ID
18621866
UpdateVersion,
1867+
/// The new ID is formed by simply replacing the old ID with a static new one
18631868
Replace(String),
1869+
/// The new ID is formed by simply replacing the old ID with a prefix, a random component (nanoid), and a suffix
18641870
ReplaceRandom { prefix: String, suffix: String },
18651871
}
18661872

@@ -1899,3 +1905,45 @@ pub fn regenerate_id<'a>(id: &'a str, strategy: &'a IdStrategy) -> String {
18991905
}
19001906
}
19011907
}
1908+
1909+
impl TryFrom<String> for IdStrategy {
1910+
type Error = StamError;
1911+
fn try_from(value: String) -> Result<Self, Self::Error> {
1912+
if let Some(pos) = value.find("=") {
1913+
let strategy = &value[0..pos - 1];
1914+
if pos + 1 >= value.len() {
1915+
Err(StamError::DeserializationError(format!(
1916+
"IdStrategy expects value after = "
1917+
)))
1918+
} else {
1919+
let value = &value[pos + 1..];
1920+
match strategy {
1921+
"suffix" | "addsuffix" => Ok(Self::AddSuffix(value.to_string())),
1922+
"prefix" | "addprefix" => Ok(Self::AddPrefix(value.to_string())),
1923+
"replace" => Ok(Self::Replace(value.to_string())),
1924+
"replacerandom" => {
1925+
let (prefix, suffix) = if let Some(pos) = value.find(";") {
1926+
(value[0..pos - 1].to_string(), value[pos + 1..].to_string())
1927+
} else {
1928+
(value.to_string(), String::new())
1929+
};
1930+
Ok(Self::ReplaceRandom { prefix, suffix })
1931+
}
1932+
_ => Err(StamError::DeserializationError(format!(
1933+
"Invalid IdStrategy: {}",
1934+
strategy
1935+
))),
1936+
}
1937+
}
1938+
} else {
1939+
match value.as_str() {
1940+
"version" | "updateversion" => Ok(Self::UpdateVersion),
1941+
"random" | "randomsuffix" => Ok(Self::AddRandomSuffix),
1942+
_ => Err(StamError::DeserializationError(format!(
1943+
"Invalid IdStrategy: {}",
1944+
value
1945+
))),
1946+
}
1947+
}
1948+
}
1949+
}

0 commit comments

Comments
 (0)